Skip to content
Merged
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions APIScripts/Earthquake API/Earthquakes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# From the USGS API: All earthquakes in PA or OR in the past 7 days

import requests
import datetime

endpoint = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson'
response = requests.get(endpoint)

status_code = response.status_code
if status_code == 200:
quakes_list = response.json()['features']
for quake_obj in quakes_list:
place_str = quake_obj['properties']['place']
if 'OR' in place_str or 'Oregon' in place_str or 'PA' in place_str or 'Pennsylvania' in place_str:
print('Place: ' + str(quake_obj['properties']['place']))

# Convert timestamp from "milliseconds since the epoch" to UTC
sec = quake_obj['properties']['time'] / 1000.0
date_time_str = datetime.datetime.fromtimestamp(sec).strftime('%Y-%m-%d %H:%M:%S.%f')
print('Date/time (UTC): ' + date_time_str)
print('(subtract 5 hrs for PA, 8 hrs for OR)')

print('Magnitude: ' + str(quake_obj['properties']['mag']))
print('Type: ' + str(quake_obj['properties']['type']))
if quake_obj['properties']['alert'] != None:
print('Alert: ' + str(quake_obj['properties']['alert']))
if quake_obj['properties']['tsunami'] > 0:
print('Possible tsunami warning; see NOAA Tsunami website')
if quake_obj['properties']['felt'] != None:
print(str(quake_obj['properties']['felt']) + ' report(s) that it was felt')
print('--------------------------------------------------')

else:
print(f'Request was unsuccessful. Status code: {status_code}')
9 changes: 9 additions & 0 deletions APIScripts/Earthquake API/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Earthquakes.py

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).

# Output

Output is printed to the console, as shown in this example:

![screenshot](ScreenshotEarthquakes.png "Title")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions APIScripts/Earthquake API/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Requirements:

- requests library
- datetime module