· 5 years ago · Jun 06, 2020, 09:58 AM
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?'
13else :
14 serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'
15
16# Ignore SSL certificate errors
17ctx = ssl.create_default_context()
18ctx.check_hostname = False
19ctx.verify_mode = ssl.CERT_NONE
20
21while True:
22 address = input('Enter location: ')
23 if len(address) < 1: break
24
25 parms = dict()
26
27 if api_key is not False: parms['key'] = api_key
28 parms['address'] = address
29 url = serviceurl + urllib.parse.urlencode(parms)
30
31 print('Retrieving', url)
32 uh = urllib.request.urlopen(url, context=ctx)
33 data = uh.read().decode()
34 print('Retrieved', len(data), 'characters')
35
36 try:
37 js = json.loads(data)
38 except:
39 js = None
40
41 if not js or 'status' not in js or js['status'] != 'OK':
42 print('==== Failure To Retrieve ====')
43 print(data)
44 continue
45
46 #print(json.dumps(js, indent=4))
47
48 place_id = js['results'][0]['place_id']
49 print('Place_id', place_id)