· 9 years ago · Dec 03, 2016, 11:24 PM
1print ('Retrieved',len(data),'characters',data[:20].replace('n',' '))
2
3import urllib
4import urllib.request
5import sqlite3
6import json
7import time
8import ssl
9
10# If you are in China use this URL:
11# serviceurl = "http://maps.google.cn/maps/api/geocode/json?"
12serviceurl = "http://maps.googleapis.com/maps/api/geocode/json?"
13
14# Deal with SSL certificate anomalies Python > 2.7
15# scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
16scontext = None
17
18conn = sqlite3.connect('geodata.sqlite')
19cur = conn.cursor()
20
21cur.execute('''
22CREATE TABLE IF NOT EXISTS Locations (address TEXT, geodata TEXT)''')
23
24fh = open("where.data", encoding="utf-8")
25count = 0
26for line in fh:
27 if count > 200 : break
28 address = line.strip()
29 print ('')
30 cur.execute("SELECT geodata FROM Locations WHERE address= ?", ((address), ))
31
32 try:
33 data = cur.fetchone()[0]
34 print ("Found in database ",address)
35 continue
36 except:
37 pass
38
39 print ('Resolving', address)
40 url = serviceurl + urllib.parse.urlencode({"sensor":"false", "address": address})
41 print ('Retrieving', url)
42 uh = urllib.request.urlopen(url, context=scontext)
43 data = uh.read()
44 print ('Retrieved',len(data),'characters',data[:20].replace('n',' '))
45 count = count + 1
46 try:
47 js = json.loads(str(data))
48 # print js # We print in case unicode causes an error
49 except:
50 continue
51
52 if 'status' not in js or (js['status'] != 'OK' and js['status'] != 'ZERO_RESULTS') :
53 print ('==== Failure To Retrieve ====')
54 print (data)
55 break
56
57 cur.execute('''INSERT INTO Locations (address, geodata)
58 VALUES ( ?, ? )''', ( buffer(address),buffer(data) ) )
59 conn.commit()
60 time.sleep(1)
61
62print ("Run geodump.py to read the data from the database so you can visualize it on a map.")