· 6 years ago · Apr 22, 2019, 03:14 PM
1import urllib.request, urllib.parse, urllib.error
2import http
3import sqlite3
4import json
5import time
6import ssl
7import sys
8
9api_key = False
10# If you have a Google Places API key, enter it here
11# api_key = 'AIzaSy___IDByT70'
12
13if api_key is False:
14 api_key = 42
15 serviceurl = "http://py4e-data.dr-chuck.net/json?"
16else :
17 serviceurl = "https://maps.googleapis.com/maps/api/geocode/json?"
18
19# Additional detail for urllib
20# http.client.HTTPConnection.debuglevel = 1
21
22conn = sqlite3.connect('geodata.sqlite')
23cur = conn.cursor()
24
25cur.execute('''
26CREATE TABLE IF NOT EXISTS Locations (address TEXT, geodata TEXT)''')
27
28# Ignore SSL certificate errors
29ctx = ssl.create_default_context()
30ctx.check_hostname = False
31ctx.verify_mode = ssl.CERT_NONE
32
33fh = open("where.data")
34count = 0
35for line in fh:
36 if count > 200 :
37 print('Retrieved 200 locations, restart to retrieve more')
38 break
39
40 address = line.strip()
41 print('')
42 cur.execute("SELECT geodata FROM Locations WHERE address= ?",
43 (memoryview(address.encode()), ))
44
45 try:
46 data = cur.fetchone()[0]
47 print("Found in database ",address)
48 continue
49 except:
50 pass
51
52 parms = dict()
53 parms["address"] = address
54 if api_key is not False: parms['key'] = api_key
55 url = serviceurl + urllib.parse.urlencode(parms)
56
57 print('Retrieving', url)
58 uh = urllib.request.urlopen(url, context=ctx)
59 data = uh.read().decode()
60 print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))
61 count = count + 1
62
63 try:
64 js = json.loads(data)
65 except:
66 print(data) # We print in case unicode causes an error
67 continue
68
69 if 'status' not in js or (js['status'] != 'OK' and js['status'] != 'ZERO_RESULTS') :
70 print('==== Failure To Retrieve ====')
71 print(data)
72 break
73
74 cur.execute('''INSERT INTO Locations (address, geodata)
75 VALUES ( ?, ? )''', (memoryview(address.encode()), memoryview(data.encode()) ) )
76 conn.commit()
77 if count % 10 == 0 :
78 print('Pausing for a bit...')
79 time.sleep(5)
80
81print("Run geodump.py to read the data from the database so you can vizualize it on a map.")