Spaces:
Sleeping
Sleeping
chen commited on
Update app.py
Browse files14 unmodified lines151617181819206 unmodified lines27282931303132338 unmodified lines4243444645464748495253505152535455566061625758596061656667626364656667686970717273747569707172737778747576771 unmodified line7980818687888283848586878889909192929394959596969714 unmodified lines)
# Initialize the IRIS FDSN client# This is placed in a global scope to be reused across requeststry: client = Client("IRIS") logging.info("Successfully initialized IRIS FDSN client.")6 unmodified lines """ A simple 'Hello World' endpoint. """ return {"Hello": "World!", "message": "Go to /earthquakes to fetch data, or /docs for the API documentation."} return {"Hello": "World!", "message": "Go to /earthquakes to fetch data, or /docs for the API documentation. Dayi"}
@app.get("/earthquakes")async def get_earthquake_catalog():8 unmodified lines )
try: # Define the query parameters starttime = UTCDateTime("2015-01-05") endtime = UTCDateTime("2016-03-05") min_magnitude = 5.0
logging.info(f"Fetching events from {starttime} to {endtime} with min magnitude {min_magnitude}.")
# Fetch the earthquake catalog from the IRIS web service catalog = client.get_events( starttime=starttime, endtime=endtime, minmagnitude=min_magnitude, ) logging.info(f"Found {len(catalog)} events. Now processing.")
logging.info(f"Found {len(catalog)} events.")
# Process the catalog into a more friendly JSON format earthquake_list = [] # --- Key Change Start --- # Loop through each event and process it individually to prevent one bad event from crashing the app. for event in catalog: try: # Ensure the event has a preferred origin and magnitude to avoid errors if not event.preferred_origin() or not event.preferred_magnitude(): continue # Ensure the event has a preferred origin and magnitude if not event.preferred_origin() or not event.preferred_magnitude(): continue # Skip to the next event if essential data is missing
origin = event.preferred_origin() magnitude = event.preferred_magnitude()
origin = event.preferred_origin() magnitude = event.preferred_magnitude() # Extract place description place = "N/A" if event.descriptions: place = event.descriptions[0].text # Safely extract the place description place = "N/A" if event.descriptions: # Check if the descriptions list exists and is not empty place = event.descriptions[0].text
earthquake_list.append( { earthquake_list.append({ "time": origin.time.isoformat(), "latitude": origin.latitude, "longitude": origin.longitude,1 unmodified line "magnitude": magnitude.mag, "magnitude_type": magnitude.magnitude_type, "place": place, } ) }) except Exception as e: # If a single event fails, log it and continue with the others. logging.warning(f"Skipping one event due to a processing error: {e}") continue # --- Key Change End ---
logging.info(f"Successfully processed {len(earthquake_list)} events.") return JSONResponse(content={"earthquakes": earthquake_list})
except Exception as e: logging.error(f"An error occurred while fetching data from IRIS FDSN: {e}") logging.error(f"A major error occurred during the request: {e}") raise HTTPException( status_code=500, # Internal Server Error detail=f"An internal error occurred while fetching data from the external service: {e}", ) detail=f"An internal error occurred: {e}", )app.py+27-26