Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,6 @@ app = FastAPI(
|
|
| 15 |
)
|
| 16 |
|
| 17 |
# Initialize the IRIS FDSN client
|
| 18 |
-
# This is placed in a global scope to be reused across requests
|
| 19 |
try:
|
| 20 |
client = Client("IRIS")
|
| 21 |
logging.info("Successfully initialized IRIS FDSN client.")
|
|
@@ -28,7 +27,7 @@ def greet_json():
|
|
| 28 |
"""
|
| 29 |
A simple 'Hello World' endpoint.
|
| 30 |
"""
|
| 31 |
-
return {"Hello": "World!", "message": "Go to /earthquakes to fetch data, or /docs for the API documentation."}
|
| 32 |
|
| 33 |
@app.get("/earthquakes")
|
| 34 |
async def get_earthquake_catalog():
|
|
@@ -43,39 +42,36 @@ async def get_earthquake_catalog():
|
|
| 43 |
)
|
| 44 |
|
| 45 |
try:
|
| 46 |
-
# Define the query parameters
|
| 47 |
starttime = UTCDateTime("2015-01-05")
|
| 48 |
endtime = UTCDateTime("2016-03-05")
|
| 49 |
min_magnitude = 5.0
|
| 50 |
|
| 51 |
logging.info(f"Fetching events from {starttime} to {endtime} with min magnitude {min_magnitude}.")
|
| 52 |
-
|
| 53 |
-
# Fetch the earthquake catalog from the IRIS web service
|
| 54 |
catalog = client.get_events(
|
| 55 |
starttime=starttime,
|
| 56 |
endtime=endtime,
|
| 57 |
minmagnitude=min_magnitude,
|
| 58 |
)
|
|
|
|
| 59 |
|
| 60 |
-
logging.info(f"Found {len(catalog)} events.")
|
| 61 |
-
|
| 62 |
-
# Process the catalog into a more friendly JSON format
|
| 63 |
earthquake_list = []
|
|
|
|
|
|
|
| 64 |
for event in catalog:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
place = "N/A"
|
| 74 |
-
if event.descriptions:
|
| 75 |
-
place = event.descriptions[0].text
|
| 76 |
|
| 77 |
-
|
| 78 |
-
{
|
| 79 |
"time": origin.time.isoformat(),
|
| 80 |
"latitude": origin.latitude,
|
| 81 |
"longitude": origin.longitude,
|
|
@@ -83,14 +79,19 @@ async def get_earthquake_catalog():
|
|
| 83 |
"magnitude": magnitude.mag,
|
| 84 |
"magnitude_type": magnitude.magnitude_type,
|
| 85 |
"place": place,
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
return JSONResponse(content={"earthquakes": earthquake_list})
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
-
logging.error(f"
|
| 93 |
raise HTTPException(
|
| 94 |
status_code=500, # Internal Server Error
|
| 95 |
-
detail=f"An internal error occurred
|
| 96 |
-
)
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
# Initialize the IRIS FDSN client
|
|
|
|
| 18 |
try:
|
| 19 |
client = Client("IRIS")
|
| 20 |
logging.info("Successfully initialized IRIS FDSN client.")
|
|
|
|
| 27 |
"""
|
| 28 |
A simple 'Hello World' endpoint.
|
| 29 |
"""
|
| 30 |
+
return {"Hello": "World!", "message": "Go to /earthquakes to fetch data, or /docs for the API documentation. Dayi"}
|
| 31 |
|
| 32 |
@app.get("/earthquakes")
|
| 33 |
async def get_earthquake_catalog():
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
try:
|
|
|
|
| 45 |
starttime = UTCDateTime("2015-01-05")
|
| 46 |
endtime = UTCDateTime("2016-03-05")
|
| 47 |
min_magnitude = 5.0
|
| 48 |
|
| 49 |
logging.info(f"Fetching events from {starttime} to {endtime} with min magnitude {min_magnitude}.")
|
|
|
|
|
|
|
| 50 |
catalog = client.get_events(
|
| 51 |
starttime=starttime,
|
| 52 |
endtime=endtime,
|
| 53 |
minmagnitude=min_magnitude,
|
| 54 |
)
|
| 55 |
+
logging.info(f"Found {len(catalog)} events. Now processing.")
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
earthquake_list = []
|
| 58 |
+
# --- Key Change Start ---
|
| 59 |
+
# Loop through each event and process it individually to prevent one bad event from crashing the app.
|
| 60 |
for event in catalog:
|
| 61 |
+
try:
|
| 62 |
+
# Ensure the event has a preferred origin and magnitude
|
| 63 |
+
if not event.preferred_origin() or not event.preferred_magnitude():
|
| 64 |
+
continue # Skip to the next event if essential data is missing
|
| 65 |
+
|
| 66 |
+
origin = event.preferred_origin()
|
| 67 |
+
magnitude = event.preferred_magnitude()
|
| 68 |
|
| 69 |
+
# Safely extract the place description
|
| 70 |
+
place = "N/A"
|
| 71 |
+
if event.descriptions: # Check if the descriptions list exists and is not empty
|
| 72 |
+
place = event.descriptions[0].text
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
earthquake_list.append({
|
|
|
|
| 75 |
"time": origin.time.isoformat(),
|
| 76 |
"latitude": origin.latitude,
|
| 77 |
"longitude": origin.longitude,
|
|
|
|
| 79 |
"magnitude": magnitude.mag,
|
| 80 |
"magnitude_type": magnitude.magnitude_type,
|
| 81 |
"place": place,
|
| 82 |
+
})
|
| 83 |
+
except Exception as e:
|
| 84 |
+
# If a single event fails, log it and continue with the others.
|
| 85 |
+
logging.warning(f"Skipping one event due to a processing error: {e}")
|
| 86 |
+
continue
|
| 87 |
+
# --- Key Change End ---
|
| 88 |
+
|
| 89 |
+
logging.info(f"Successfully processed {len(earthquake_list)} events.")
|
| 90 |
return JSONResponse(content={"earthquakes": earthquake_list})
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
+
logging.error(f"A major error occurred during the request: {e}")
|
| 94 |
raise HTTPException(
|
| 95 |
status_code=500, # Internal Server Error
|
| 96 |
+
detail=f"An internal error occurred: {e}",
|
| 97 |
+
)
|