Skip to content

Instantly share code, notes, and snippets.

@ayubmetah
Created January 19, 2021 20:10
Show Gist options
  • Save ayubmetah/f2e63d03fd41d449472007c833021651 to your computer and use it in GitHub Desktop.
Save ayubmetah/f2e63d03fd41d449472007c833021651 to your computer and use it in GitHub Desktop.
Write a Python program to 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.
# To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:
# http://py4e-data.dr-chuck.net/json?
# This API uses the same parameter (address) as the Google API. This API also has no rate limit so you can test as often as you like.
# If you visit the URL with no parameters, you get "No address..." response.
# To call the API, you need to include a key= parameter and provide the address that you are requesting as the address= parameter that
# is properly URL encoded using the urllib.parse.urlencode() function as shown in http://www.py4e.com/code3/geojson.py
# Make sure to check that your code is using the API endpoint is as shown above.
# Please run your program to find the place_id for this location: University of Akron
# Make sure to enter the name and case exactly as above and enter the place_id and your Python code below. Hint: The first seven characters of the place_id are "ChIJbye ..."
import urllib.request, urllib.parse, urllib.error
import json
import ssl
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?'
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
while True:
address = input('Enter location: ')
if len(address) < 1: break
parms = dict()
parms['address'] = address
if api_key is not False: parms['key'] = api_key
url = serviceurl + urllib.parse.urlencode(parms)
print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.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('==== Failure To Retrieve ====')
print(data)
continue
# print(json.dumps(js, indent=4))
pid = js['results'][0]['place_id']
print('Place id ',pid)
@anonpython21
Copy link

import urllib.request, urllib.parse import json, ssl

serviceurl = 'https://py4e-data.dr-chuck.net/opengeo?'

ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE

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

address = address.strip()
parms = dict()
parms['q'] = address

url = serviceurl + urllib.parse.urlencode(parms)

print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read().decode()
print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))

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

if not js or 'features' not in js:
    print('==== Download error ===')
    print(data)
    break

if len(js['features']) == 0:
    print('==== Object not found ====')
    print(data)
    break

# print(json.dumps(js, indent=4))

lat = js['features'][0]['properties']['lat']
lon = js['features'][0]['properties']['lon']
print('lat', lat, 'lon', lon)
location = js['features'][0]['properties']['formatted']
print(location)
plus_code = js['features'][0]['properties']['plus_code']
print(plus_code)
Screenshot 2024-08-23 at 2 46 15 PM

could you help me with ' Viswakarma Institute Pune India '

@HananMustafa
Copy link

need plus_code for Universidade de Sao Paulo
anyone can help?

@Tyrone-J
Copy link

import urllib.request, urllib.parse
import json, ssl

serviceurl = 'http://py4e-data.dr-chuck.net/opengeo?'

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

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

parms = dict()
parms['q'] = address
url = serviceurl + urllib.parse.urlencode(parms)

print('Retrieving', url)

uh = urllib.request.urlopen(url, context=ctx)
data = uh.read().decode()
print('Retrieved', len(data), 'characters')

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

if not js or 'features' not in js:
    print('==== Download error ===')
    print(data)
    break

if len(js['features']) == 0:
    print('==== Object not found ===')
    print(data)
    break

plus_code = js['features'][0]['properties']['plus_code']
print('Plus code', plus_code)
break

this works for the plus_code

@MarkedKiler
Copy link

Can anyone help me find plus_code for King Mongkuts University of Technology Thonburi please?

@Shubzzz777
Copy link

could you help me please? i would like the results for ' Viswakarma Institute Pune India ' please!!

7JCMGR9R+J7

@Shubzzz777
Copy link

University of Padua

8FQHCV88+MM

@Shubzzz777
Copy link

King Mongkuts University of Technology Thonburi

7P52MF2W+45

@Shubzzz777
Copy link

Universidade de Sao Paulo

588MC7MP+WX

@Shubzzz777
Copy link

Universidade de Sao Paulo

588MC7MP+WX

@Shubzzz777
Copy link

import urllib.request, urllib.parse
import json, ssl

serviceurl = 'https://py4e-data.dr-chuck.net/opengeo?'

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

while True:
address = input('Enter location: ')
if len(address) < 1:
break
address = address.strip()
parms = dict()
parms['q'] = address

url = serviceurl + urllib.parse.urlencode(parms)

print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read().decode()
print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))

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

if not js or 'features' not in js:
print('==== Download error ===')
print(data)
break

if len(js['features']) == 0:
print('==== Object not found ====')
print(data)
break

print(json.dumps(js, indent=4))

lat = js['features'][0]['properties']['lat']
lon = js['features'][0]['properties']['lon']
print('lat', lat, 'lon', lon)
location = js['features'][0]['properties']['formatted']
print(location)
plus_code = js['features'][0]['properties']['plus_code']
print(plus_code)

@MarkedKiler
Copy link

7P52MF2W+45

thank you kind sir.

@carlinidk
Copy link

import urllib.request, urllib.parse import json, ssl

serviceurl = 'https://py4e-data.dr-chuck.net/opengeo?'

ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE

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

address = address.strip()
parms = dict()
parms['q'] = address

url = serviceurl + urllib.parse.urlencode(parms)

print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read().decode()
print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))

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

if not js or 'features' not in js:
    print('==== Download error ===')
    print(data)
    break

if len(js['features']) == 0:
    print('==== Object not found ====')
    print(data)
    break

# print(json.dumps(js, indent=4))

lat = js['features'][0]['properties']['lat']
lon = js['features'][0]['properties']['lon']
print('lat', lat, 'lon', lon)
location = js['features'][0]['properties']['formatted']
print(location)
plus_code = js['features'][0]['properties']['plus_code']
print(plus_code)
Screenshot 2024-08-23 at 2 46 15 PM

heyo im still stuck with the api key not working can u try Middle East Technical University please?

@minnatshah
Copy link

kindly tell plus code for Universidad de Oriente

@Shubzzz777
Copy link

Middle East Technical University

8GFJVQGP+56

@Shubzzz777
Copy link

Universidad de Oriente

67WV88QH+7M

@minnatshah
Copy link

Thank you so much Shubzzz

@wafos12
Copy link

wafos12 commented Mar 6, 2025

can someone help me find the code for Universitas Gadjah Mada, my code doesn't seem to work as i print out all the info, then input the plus code but its not right

@debbigit
Copy link

debbigit commented Jun 18, 2025

Please help me out: I checked my code and the code above. It all turns out that the Plus code for the location 'Indiana University at Bloomington' is 86MPV5HX+JM. It doesn't starts with 8554M as required in the Hint part. So I could not submit my assignment.
Calling a JSON API

In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/opengeo.py. 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 plus_code from the JSON. An Open Location Code is a textual identifier that is another form of address based on the location of the address.
To complete this assignment, you should use this API endpoint that has a static subset of the Open Street Map Data.
http://py4e-data.dr-chuck.net/opengeo?
Please run your program to find the plus_code for this location: Indiana University at Bloomington
Make sure to enter the name and case exactly as above and enter the plus_code and your Python code below. Hint: The first five characters of the plus_code are "8554M ..."

@Maryam6600
Copy link

Maryam6600 commented Jun 22, 2025

I found this URL https://py4e-data.dr-chuck.net/opengeo?q=Universidad+de+Castilla+La+Mancha
The plus code La Universidad de Castilla
La Mancha is written "8CCRX3WJ+QR" but I have this issue "Your plus_code did not match"
The hint is The first five characters of the plus_code are "8CCRX ..."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment