-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetPicture.py
49 lines (36 loc) · 1.55 KB
/
GetPicture.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
47
48
49
import config
import json
import random
import http.client, urllib.parse
BING_KEY = '02053fab192a404e82363a70bacbfc7b'
def GetPicture(city, condition, logger, chat_id):
host = "api.cognitive.microsoft.com"
path = "/bing/v7.0/images/search/"
condition = config.NEAREST_WEATHER_STATES_FOR_PICTURES[condition]
# condition = ' '.join(condition.split('-'))
term = '{} weather in {}'.format(condition, city)
def BingWebSearch(search):
"Performs a Bing Web search and returns the results."
headers = {'Ocp-Apim-Subscription-Key': BING_KEY}
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(BING_KEY) == 32:
logger.info('Requset from {}: Searching for picture: {}'.format(chat_id, term))
headers, result = BingWebSearch(term)
result = json.loads(result)
try:
img_url = result['value'][random.randrange(5)]['contentUrl']
return img_url
except Exception:
print('no picture for {}'.format(term))
else:
print("Invalid Bing Search API subscription key!")
print("Please paste yours into the source code.")
return None
if __name__ == '__main__':
GetPicture('London', 'overcast and snow')