-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
32 lines (29 loc) · 1.16 KB
/
run.py
File metadata and controls
32 lines (29 loc) · 1.16 KB
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
import csv
import json
import time
import requests
# read the csv
f = open('tbl_skpd.csv')
csv_f = csv.reader(f)
# skip the first line
next(csv_f)
# for each row in the csv
for row in csv_f:
time.sleep(3) # first pause (in order not to exceed rate limit or have connection time out)
# if alamat is not empty, then connect to googmle maps API and save the data in a file with the skpdID.json
if row[7] != "":
# create variable for file name
filename = "json/" + row[0] + ".json"
# create the request to google maps api
url = "https://maps.google.com/maps/api/geocode/json?address=" + row[7] + "&key=AIzaSyDmGnhhccwog6j_hFmAo8zg1VaYWE_m7Ak&sensor=false"
#check the url in the terminal to make sure it isok
print(url)
# do the request
loc = requests.get(url)
# save the request result in variable called data and then write down the variable to a file.
data = loc.json()
dataFile = open(filename, 'w')
# write down the variable, but use json.dump with an indent for pretty printing and good format of json
dataFile.write(json.dumps(data, indent=4))
# close the connection to the file and continue the loop
dataFile.close()