|
| 1 | +import urllib.request, urllib.parse, urllib.error |
| 2 | +import http |
| 3 | +import sqlite3 |
| 4 | +import json |
| 5 | +import time |
| 6 | +import ssl |
| 7 | +import sys |
| 8 | + |
| 9 | +api_key = False |
| 10 | +# If you have a Google Places API key, enter it here |
| 11 | +# api_key = 'AIzaSy___IDByT70' |
| 12 | + |
| 13 | +if api_key is False: |
| 14 | + api_key = 42 |
| 15 | + serviceurl = "http://py4e-data.dr-chuck.net/json?" |
| 16 | +else : |
| 17 | + serviceurl = "https://maps.googleapis.com/maps/api/geocode/json?" |
| 18 | + |
| 19 | +# Additional detail for urllib |
| 20 | +# http.client.HTTPConnection.debuglevel = 1 |
| 21 | + |
| 22 | +conn = sqlite3.connect('geodata.sqlite') |
| 23 | +cur = conn.cursor() |
| 24 | + |
| 25 | +cur.execute(''' |
| 26 | +CREATE TABLE IF NOT EXISTS Locations (address TEXT, geodata TEXT)''') |
| 27 | + |
| 28 | +# Ignore SSL certificate errors |
| 29 | +ctx = ssl.create_default_context() |
| 30 | +ctx.check_hostname = False |
| 31 | +ctx.verify_mode = ssl.CERT_NONE |
| 32 | + |
| 33 | +fh = open("where.data") |
| 34 | +count = 0 |
| 35 | +for 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 | + |
| 81 | +print("Run geodump.py to read the data from the database so you can vizualize it on a map.") |
0 commit comments