|
1 | | -#Copyright (c) Microsoft Corporation. All rights reserved. |
2 | | -#Licensed under the MIT License. |
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | # -*- coding: utf-8 -*- |
5 | 5 |
|
6 | | -import http.client, urllib.parse, json |
| 6 | +import http.client |
| 7 | +import urllib.parse |
| 8 | +import json |
| 9 | +import os |
| 10 | +from pprint import pprint |
7 | 11 |
|
8 | | -# ********************************************** |
9 | | -# *** Update or verify the following values. *** |
10 | | -# ********************************************** |
| 12 | +''' |
| 13 | +This sample makes a call to the Bing Image Search API with a text query and returns relevant images with data. |
| 14 | +Documentation: https: // docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/ |
| 15 | +''' |
11 | 16 |
|
12 | | -# Add your Bing Search V7 subscription key to your environment variables. |
| 17 | +# Add your Bing Search V7 subscription key and endpoint to your environment variables. |
13 | 18 | subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY'] |
14 | | - |
15 | | -# Add your Bing Search V7 endpoint to your environment variables. |
16 | 19 | host = os.environ['BING_SEARCH_V7_ENDPOINT'] |
| 20 | +host = host.replace('https://', '') |
17 | 21 | path = "/bing/v7.0/images/search" |
18 | 22 |
|
19 | | -term = "puppies" |
20 | | - |
21 | | -def BingImageSearch(search): |
22 | | - "Performs a Bing image search and returns the results." |
23 | | - |
24 | | - headers = {'Ocp-Apim-Subscription-Key': subscriptionKey} |
25 | | - conn = http.client.HTTPSConnection(host) |
26 | | - query = urllib.parse.quote(search) |
27 | | - conn.request("GET", path + "?q=" + query, headers=headers) |
28 | | - response = conn.getresponse() |
29 | | - headers = [k + ": " + v for (k, v) in response.getheaders() |
30 | | - if k.startswith("BingAPIs-") or k.startswith("X-MSEdge-")] |
31 | | - return headers, response.read().decode("utf8") |
32 | | - |
33 | | -if len(subscriptionKey) == 32: |
| 23 | +# Query to search for |
| 24 | +query = "puppies" |
34 | 25 |
|
35 | | - print('Searching images for: ', term) |
| 26 | +# Construct a request |
| 27 | +headers = {'Ocp-Apim-Subscription-Key': subscriptionKey} |
| 28 | +conn = http.client.HTTPSConnection(host) |
| 29 | +query_search = urllib.parse.quote(query) |
| 30 | +conn.request("GET", path + "?q=" + query_search, headers=headers) |
36 | 31 |
|
37 | | - headers, result = BingImageSearch(term) |
38 | | - print("\nRelevant HTTP Headers:\n") |
39 | | - print("\n".join(headers)) |
40 | | - print("\nJSON Response:\n") |
41 | | - print(json.dumps(json.loads(result), indent=4)) |
| 32 | +# Print response |
| 33 | +response = conn.getresponse() |
| 34 | +headers = [k + ": " + v for (k, v) in response.getheaders() |
| 35 | + if k.startswith("BingAPIs-") or k.startswith("X-MSEdge-")] |
42 | 36 |
|
43 | | -else: |
| 37 | +print('Searching images for: ', query) |
44 | 38 |
|
45 | | - print("Invalid Bing Search API subscription key!") |
46 | | - print("Please paste yours into the source code.") |
| 39 | +print("\nRelevant HTTP Headers:\n") |
| 40 | +print("\n".join(headers)) |
| 41 | +print("\nJSON Response:\n") |
| 42 | +pprint(json.loads(response.read())) |
0 commit comments