· 5 years ago · May 31, 2020, 01:54 PM
1import urllib.request, urllib.parse, urllib.error
2import json
3import ssl
4
5api_key = False
6# If you have a Google Places API key, enter it here
7# api_key = 'AIzaSy___IDByT70'
8# https://developers.google.com/maps/documentation/geocoding/intro
9
10if api_key is False:
11 api_key = 42
12 serviceurl = 'http://py4e-data.dr-chuck.net/json?'
13
14
15# Ignore SSL certificate errors
16ctx = ssl.create_default_context()
17ctx.check_hostname = False
18ctx.verify_mode = ssl.CERT_NONE
19
20while True:
21 address = input('Enter location: ')
22 if len(address) < 1: break
23
24 parms = dict()
25 parms['address'] = address
26 if api_key is not False: parms['key'] = api_key
27 url = serviceurl + urllib.parse.urlencode(parms)
28
29 print('Retrieving', url)
30 uh = urllib.request.urlopen(url, context=ctx)
31 data = uh.read().decode()
32 print('Retrieved', len(data), 'characters')
33
34 try:
35 js = json.loads(data)
36 except:
37 js = None
38
39 if not js or 'status' not in js or js['status'] != 'OK':
40 print('==== Failure To Retrieve ====')
41 print(data)
42 continue
43
44 #print(json.dumps(js, indent=4))
45
46 pid = js['results'][0]['place_id']
47 print(pid)