forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpull.py
More file actions
47 lines (35 loc) · 1.27 KB
/
pull.py
File metadata and controls
47 lines (35 loc) · 1.27 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
33
34
35
36
37
38
39
40
41
42
43
import urllib
import json
# Reads from locations.inp and produces locations.txt
serviceurl = 'http://localhost:8888/tsugi/mod/python-data/data/geojson?'
serviceurl = 'http://pr4e.dr-chuck.com/tsugi/mod/python-data/data/geojson?'
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
fh = open('locations.inp')
locations = dict()
for address in fh:
address = address.strip()
url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
try: js = json.loads(str(data))
except: js = None
if 'status' not in js or js['status'] != 'OK':
print '==== Failure To Retrieve',address
continue
try:
lat = js["results"][0]["geometry"]["location"]["lat"]
lng = js["results"][0]["geometry"]["location"]["lng"]
# print 'lat',lat,'lng',lng
location = js['results'][0]['formatted_address']
# print location
place_id = js['results'][0]['place_id']
print 'Place id', place_id
except:
print "=== Failure to parse",address
contnue
locations[address]= str(data)
f = open('locations.txt', 'w')
f.write(json.dumps(locations, indent=4))
f.close()