-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.py
78 lines (63 loc) · 2.18 KB
/
fetcher.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
import os
import time
from datetime import datetime
from dotenv import load_dotenv
import requests
load_dotenv()
AZURE_KEY1 = os.environ['AZURE_KEY1']
# AZURE_KEY2 = os.environ['AZURE_KEY2']
data = {}
people = []
print('\nStarting\n')
reqUrl = "http://api.open-notify.org/astros.json"
response = requests.get(reqUrl)
json_response = response.json()
# Only put into json if everything's fine
if (response.status_code == 200) and json_response['message'] == 'success':
print(f"Got data yeay\n")
number = json_response['number'] # original response
message = json_response['message'] # original response
for item in json_response['people']:
name = item['name'] # original response
craft = item['craft'] # original response
print('Adding {}'.format(name))
bingReq = requests.get(
'https://api.bing.microsoft.com/v7.0/images/search',
headers={'Ocp-Apim-Subscription-Key': AZURE_KEY1},
params={
'q': name + ' astroanaut',
'count': 1
})
if bingReq.status_code == 200:
bingResponse = bingReq.json()
people.append({
'name':
name,
'craft':
craft,
'thumbnailUrl':
bingResponse['value'][0]['thumbnailUrl'],
'contentUrl':
bingResponse['value'][0]['contentUrl']
})
else:
raise RuntimeError(f"Status code {bingReq.status_code}")
time.sleep(1) # delay a bit
# rebuild response
data['data'] = {'people': people, 'number': number, 'message': message}
else:
print(f'Failed ({response.status_code})')
raise SystemExit()
fetch_finish = time.strftime("%a, %d %b %Y %H:%M %Z") # format UTC Time
print(f'\nFetching finish at {fetch_finish}')
# writing all location data to file
with open('db.json', 'w') as outfile:
json.dump(data, outfile, indent=2)
print('\nFinish writing to db.json')
log = {}
log['fetcher_last_run'] = fetch_finish
# writing log
with open('public/log.json', 'w') as outfile:
json.dump(log, outfile, indent=2)
print('Log is written to log.json')