-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimage_request.py
executable file
·46 lines (34 loc) · 1.61 KB
/
image_request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
import http.client, urllib.parse, json
# **********************************************
# *** Update or verify the following values. ***
# **********************************************
# Replace the subscriptionKey string value with your valid subscription key.
subscriptionKey = "b5d7b6429b404b59b14c9f0ef3dbb59c"
# Verify the endpoint URI. At this writing, only one endpoint is used for Bing
# search APIs. In the future, regional endpoints may be available. If you
# encounter unexpected authorization errors, double-check this value against
# the endpoint for your Bing search instance in your Azure dashboard.
host = "api.cognitive.microsoft.com"
path = "/bing/v7.0/images/search"
term = "puppies"
def BingImageSearch(search):
"Performs a Bing image search and returns the results."
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
conn = http.client.HTTPSConnection(host)
query = urllib.parse.quote(search)
conn.request("GET", path + "?q=" + query, headers=headers)
response = conn.getresponse()
headers = [k + ": " + v for (k, v) in response.getheaders()
if k.startswith("BingAPIs-") or k.startswith("X-MSEdge-")]
return headers, response.read().decode("utf8")
if len(subscriptionKey) == 32:
print('Searching images for: ', term)
headers, result = BingImageSearch(term)
print("\nRelevant HTTP Headers:\n")
print("\n".join(headers))
print("\nJSON Response:\n")
print(json.dumps(json.loads(result), indent=4))
else:
print("Invalid Bing Search API subscription key!")
print("Please paste yours into the source code.")