-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2018_12_11_json.py
More file actions
67 lines (53 loc) · 2.13 KB
/
Copy path2018_12_11_json.py
File metadata and controls
67 lines (53 loc) · 2.13 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 11 23:05:28 2018
@author: domenjemec
"""
import urllib.request
import json
def printResults(data):
# Use the json module to load the string data into a dictionary
theJSON = json.loads(data)
# now we can access the contents of the JSON like any other Python object
if "title" in theJSON["metadata"]:
print(theJSON["metadata"]["title"])
# output the number of events, plus the magnitude and each event name
count = theJSON["metadata"]["count"]
print(str(count) + " events recorded")
# for each event, print the place where it occurred
for i in theJSON["features"]:
print(i["properties"]["place"])
print("--------------\n")
# print the events that only have a magnitude greater than 4
for i in theJSON["features"]:
if i["properties"]["mag"] >= 4.0:
print("%2.1f" % i["properties"]["mag"], i["properties"]["place"])
print("--------------\n")
# print only the events where at least 1 person reported feeling something
print("\n\nEvents that were felt:")
for i in theJSON["features"]:
feltReports = i["properties"]["felt"]
if (feltReports is not None):
if (feltReports > 0):
print("%2.1f" % i["properties"]["mag"],
i["properties"]["place"],
" reported " + str(feltReports) + " times")
def main():
# define a variable to hold the source URL
# In this case we'll use the free data feed from the USGS
# This feed lists all earthquakes for the last day larger than Mag 2.5
urlData = ('http://earthquake.usgs.gov/earthquakes/'
'feed/v1.0/summary/2.5_day.geojson')
# Open the URL and read the data
webUrl = urllib.request.urlopen(urlData)
print("result code: " + str(webUrl.getcode()))
if (webUrl.getcode() == 200):
data = webUrl.read()
# print out our customized results
printResults(data)
else:
print("Received an error from server, cannot retrieve results " +
str(webUrl.getcode()))
if __name__ == "__main__":
main()