Last active
March 28, 2025 14:38
-
-
Save sdg-1/c8c38c4d5caf70be5fa38caaab97fc1c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import pandas as pd | |
from datetime import datetime, timedelta | |
import os | |
def process_earthquake_data(): | |
today_date = datetime.now() | |
timeDelta = timedelta(days=1) | |
fifteen_days_ago = today_date - timedelta(days=15) | |
start_time = fifteen_days_ago.strftime("%Y-%m-%d") | |
end_time = today_date.strftime("%Y-%m-%d") | |
# Define (Follow the link (cmd + click)) | |
url = f"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime={start_time}&endtime={end_time}" | |
response = requests.get(url) | |
earthquakes = [] | |
if response.status_code == 200: | |
data = response.json() | |
features = data["features"] | |
date = today_date.strftime("%Y_%m_%d") | |
filename = f"UPDATE WITH YOUR FILE Name" | |
for feature in features: | |
properties = feature["properties"] | |
geometry = feature["geometry"] | |
earthquake = { | |
"time": properties["time"], | |
"place": properties["place"], | |
"magnitude": properties["mag"], | |
"longitude": geometry["coordinates"][0], | |
"latitude": geometry["coordinates"][1], | |
"depth": geometry["coordinates"][2], | |
"file_name": filename, | |
} | |
earthquakes.append(earthquake) | |
df = pd.DataFrame(earthquakes) | |
if os.path.exists(filename): | |
os.remove(filename) | |
print(f"File {filename}") | |
df.to_csv(filename, index=False) | |
def main(): | |
process_earthquake_data() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment