Skip to content

Commit ff97672

Browse files
Merge pull request prathimacode-hub#25 from RandallWert/Earthquakes
Earthquakes
2 parents 01f52f2 + 08a7d69 commit ff97672

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# From the USGS API: All earthquakes in PA or OR in the past 7 days
2+
3+
import requests
4+
import datetime
5+
6+
endpoint = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson'
7+
response = requests.get(endpoint)
8+
9+
status_code = response.status_code
10+
if status_code == 200:
11+
quakes_list = response.json()['features']
12+
for quake_obj in quakes_list:
13+
place_str = quake_obj['properties']['place']
14+
if 'OR' in place_str or 'Oregon' in place_str or 'PA' in place_str or 'Pennsylvania' in place_str:
15+
print('Place: ' + str(quake_obj['properties']['place']))
16+
17+
# Convert timestamp from "milliseconds since the epoch" to UTC
18+
sec = quake_obj['properties']['time'] / 1000.0
19+
date_time_str = datetime.datetime.fromtimestamp(sec).strftime('%Y-%m-%d %H:%M:%S.%f')
20+
print('Date/time (UTC): ' + date_time_str)
21+
print('(subtract 5 hrs for PA, 8 hrs for OR)')
22+
23+
print('Magnitude: ' + str(quake_obj['properties']['mag']))
24+
print('Type: ' + str(quake_obj['properties']['type']))
25+
if quake_obj['properties']['alert'] != None:
26+
print('Alert: ' + str(quake_obj['properties']['alert']))
27+
if quake_obj['properties']['tsunami'] > 0:
28+
print('Possible tsunami warning; see NOAA Tsunami website')
29+
if quake_obj['properties']['felt'] != None:
30+
print(str(quake_obj['properties']['felt']) + ' report(s) that it was felt')
31+
print('--------------------------------------------------')
32+
33+
else:
34+
print(f'Request was unsuccessful. Status code: {status_code}')

APIScripts/Earthquake API/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Earthquakes.py
2+
3+
The Earthquakes.py program displays information about earthquakes registered in Oregon and Pennsylvania in the past seven days. (I chose these two states because that’s where our family lives.) You could easily change the code to display earthquakes in other states. The information comes from an API provided by the [U.S. Geological Survey] (https://www.usgs.gov/products/data-and-tools/apis).
4+
5+
# Output
6+
7+
Output is printed to the console, as shown in this example:
8+
9+
![screenshot](ScreenshotEarthquakes.png "Title")
91.1 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Requirements:
2+
3+
- requests library
4+
- datetime module

0 commit comments

Comments
 (0)