forked from btelle/cruise-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
city_geo.py
35 lines (27 loc) · 883 Bytes
/
city_geo.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
import requests, json, time
api_key = 'AIzaSyAj4QfwThTfPttoh_LaRzIx5U10onljk-Y'
url = 'https://maps.googleapis.com/maps/api/geocode/json'
outfile = open('data/cities.json', 'w')
with open('data/cities.csv', 'r') as fh:
fh.readline()
for city in fh.readlines():
params = {
'address': city.strip(),
'key': api_key
}
out = {
'city': city.strip()
}
resp = requests.get(url, params=params)
obj = resp.json()
if obj['status'] == 'OK':
out['lat'] = obj['results'][0]['geometry']['location']['lat']
out['lon'] = obj['results'][0]['geometry']['location']['lng']
out['id'] = obj['results'][0]['place_id']
for r in obj['results'][0]['address_components']:
if 'country' in r['types']:
out['country_code'] = r['short_name']
out['country'] = r['long_name']
outfile.write(json.dumps(out) + "\n")
time.sleep(2)
outfile.close()