Skip to content

Commit 36ced94

Browse files
authored
Updated structure, fixed bug
1 parent 3266ec0 commit 36ced94

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

python/Search/BingImageSearchv7.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
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.
33

44
# -*- coding: utf-8 -*-
55

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
711

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+
'''
1116

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.
1318
subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
14-
15-
# Add your Bing Search V7 endpoint to your environment variables.
1619
host = os.environ['BING_SEARCH_V7_ENDPOINT']
20+
host = host.replace('https://', '')
1721
path = "/bing/v7.0/images/search"
1822

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"
3425

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)
3631

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-")]
4236

43-
else:
37+
print('Searching images for: ', query)
4438

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

Comments
 (0)