|
| 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}') |
0 commit comments