56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
import requests
|
|
import time
|
|
import json
|
|
import os
|
|
|
|
|
|
def printQuake(properties):
|
|
magnitude = properties["mag"]
|
|
location = properties["place"]
|
|
timestamp = properties["time"] / 1000
|
|
|
|
time_str = time.strftime("%H:%M:%S", time.gmtime(timestamp))
|
|
date_str = time.strftime("%a, %d %b %Y", time.gmtime(timestamp))
|
|
|
|
print(f"At {time_str} on {date_str}: earthquake of magnitude {magnitude} at {location}")
|
|
|
|
|
|
def fetchAndPrintQuakes(start_date, end_date, min_magnitude):
|
|
url = "https://earthquake.usgs.gov/fdsnws/event/1/query"
|
|
|
|
params = {
|
|
"method": "query",
|
|
"format": "geojson",
|
|
"starttime": start_date,
|
|
"endtime": end_date,
|
|
"minmagnitude": min_magnitude
|
|
}
|
|
|
|
response = requests.get(url, params=params)
|
|
data = response.json()
|
|
|
|
for quake in data["features"]:
|
|
printQuake(quake["properties"])
|
|
|
|
|
|
def readAndPrintQuakes(filename):
|
|
script_dir = os.path.dirname(__file__)
|
|
filepath = os.path.join(script_dir, filename)
|
|
|
|
with open(filepath, "r") as f:
|
|
data = json.load(f)
|
|
|
|
for quake in data["features"]:
|
|
printQuake(quake["properties"])
|
|
|
|
|
|
def main():
|
|
print("Earthquakes from API:")
|
|
fetchAndPrintQuakes("2023-10-01", "2023-10-03", 5.0)
|
|
|
|
print("\nEarthquakes from saved JSON:")
|
|
readAndPrintQuakes("load.json")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |