-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathUsing the GeoJSON API.py
More file actions
42 lines (34 loc) · 1.1 KB
/
Using the GeoJSON API.py
File metadata and controls
42 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import json
import ssl
import urllib.request
import urllib.parse
import urllib.error
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
# Stroring the given parameters
api_key = 42
serviceurl = "http://py4e-data.dr-chuck.net/json?"
data_address = input("Enter location: ")
params = {"address": data_address, "key": api_key}
paramsurl = urllib.parse.urlencode(params)
url = serviceurl.strip() + paramsurl.strip()
print("Retrieving:", url)
# Obtaining and reading the data
try:
data_read = urllib.request.urlopen(url, context=ctx).read()
data = data_read.decode()
print("Retrived", len(data), "characters")
# Parsing the data and looking for location info
jsondata = json.loads(data)
if 'status' not in jsondata or jsondata['status'] != 'OK':
print("Error: Failure to retrieve")
print(data)
# Set and print out location info to the console
place_id = jsondata["results"][0]["place_id"]
print("Place id", place_id)
except:
print("Error. Please try again.")
print("-"*30)
print(data)