-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjson_authors_to_coordinates.py
29 lines (27 loc) · 1.04 KB
/
json_authors_to_coordinates.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
import json
import re
from urllib.request import urlopen
import codecs
json_data = open("data/parsed_book_data.json").read()
data = json.loads(json_data)
reader = codecs.getreader("utf-8")
i=0
for item in data:
print(i)
if(item['author_birthplace']):
location = item['author_birthplace']
if(" " in location):
location = re.sub(" ", "+", location, 0, 0)
if((", ") in location):
location = re.sub(",", "+,", location, 0, 0)
url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + \
location + "&key=AIzaSyDyFuFFfe2jNj_EfDit-kv7PWe1gTdFgPQ"
json_data = reader(urlopen(url))
google_maps_json = json.load(json_data)
print(location)
if(google_maps_json["results"]):
item["author_lat"] = google_maps_json["results"][0]["geometry"]["location"]["lat"]
item["author_lng"] = google_maps_json["results"][0]["geometry"]["location"]["lng"]
i+=1
with open('data/parsed_book_data.json','w') as outfile:
json.dump(data,outfile)