Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions 58 ex_13b.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import urllib.request, urllib.parse
import json

'''
Use a GeoLocation lookup API modelled after the Google API to look up some
universities and parse the returned data. The program will prompt for a location,
contact a web service and retrieve JSON for the web service and parse that data,
and retrieve the first place_id from the JSON. A place ID is a textual identifier
that uniquely identifies a place as within Google Maps.

Use this API endpoint that has a static subset of the Google Data:
http://py4e-data.dr-chuck.net/geojson?
'''

loc = input('Enter location: ')
serviceurl = 'http://py4e-data.dr-chuck.net/geojson?'
url = serviceurl + urllib.parse.urlencode({'address': loc}) #Covert mapping object to a percent-encoded ASCII text string.
print('Retrieving', url)

connection = urllib.request.urlopen(url)
data = connection.read().decode() #string from utf-8 to unicode
print('Retrieved:', len(data), 'characters')
js = json.loads(data) #deserialize 'data' to a Python object

placeid = js['results'][0]['place_id']
print('Place id', placeid)
api_key = False

if api_key is False:
api_key = 42
serviceurl = 'http://py4e-data.dr-chuck.net/json?'
else :
serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'

while True:
loc = input('Enter location: ')
if len(loc)<1:break

parms = dict()
parms['address'] = loc
if api_key is not False: parms['key'] = api_key
url = serviceurl + urllib.parse.urlencode(parms)

print('Retrieving', url)

connection = urllib.request.urlopen(url)
data = connection.read().decode()
print('Retrieved:', len(data), 'characters')

try:
js = json.loads(data)
except:
js=None

if not js or 'status' not in js or js['status'] != 'OK':
print('=====Faliure to Retrive=======')
print(data)
continue

placeid = js['results'][0]['place_id']
print('Place id', placeid)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.