Spaces:
Sleeping
Sleeping
Update cwa_service.py
Browse files- cwa_service.py +16 -9
cwa_service.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# cwa_service.py (
|
| 2 |
import requests
|
| 3 |
import re
|
| 4 |
import pandas as pd
|
|
@@ -70,21 +70,27 @@ def _parse_significant_earthquakes(obj: dict) -> pd.DataFrame:
|
|
| 70 |
quakes = records.get("Earthquake", [])
|
| 71 |
rows = []
|
| 72 |
for q in quakes:
|
|
|
|
|
|
|
|
|
|
| 73 |
ei = q.get("EarthquakeInfo", {})
|
| 74 |
-
epic = ei.get("Epicenter", {})
|
| 75 |
-
mag_info = ei.get("Magnitude", {})
|
| 76 |
-
depth_raw = ei.get("FocalDepth")
|
| 77 |
|
| 78 |
-
# [修正]
|
|
|
|
|
|
|
|
|
|
| 79 |
mag_raw = mag_info.get("MagnitudeValue") or mag_info.get("magnitudeValue") or mag_info.get("Value") or mag_info.get("value")
|
| 80 |
|
| 81 |
rows.append({
|
| 82 |
"ID": q.get("EarthquakeNo"), "Time": ei.get("OriginTime"),
|
| 83 |
-
"Lat": _to_float(epic.get("EpicenterLatitude")),
|
| 84 |
-
"Lon": _to_float(epic.get("EpicenterLongitude")),
|
| 85 |
-
"Depth": _to_float(depth_raw),
|
| 86 |
-
"
|
|
|
|
|
|
|
| 87 |
})
|
|
|
|
| 88 |
df = pd.DataFrame(rows)
|
| 89 |
if not df.empty and "Time" in df.columns:
|
| 90 |
df["Time"] = pd.to_datetime(df["Time"], errors="coerce", utc=True).dt.tz_convert(TAIPEI_TZ)
|
|
@@ -138,3 +144,4 @@ def fetch_latest_significant_earthquake() -> dict | None:
|
|
| 138 |
return latest_eq_data
|
| 139 |
except Exception as e:
|
| 140 |
raise e
|
|
|
|
|
|
| 1 |
+
# cwa_service.py (Final Defensive Parsing Version)
|
| 2 |
import requests
|
| 3 |
import re
|
| 4 |
import pandas as pd
|
|
|
|
| 70 |
quakes = records.get("Earthquake", [])
|
| 71 |
rows = []
|
| 72 |
for q in quakes:
|
| 73 |
+
# [偵錯] 如果需要,可以取消下面這行的註解,它會在 Log 中印出最原始的資料
|
| 74 |
+
# print(f"原始地震資料: {q}")
|
| 75 |
+
|
| 76 |
ei = q.get("EarthquakeInfo", {})
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# [修正] 使用更穩健的方式取得所有資料,檢查所有已知的大小寫和備用名稱
|
| 79 |
+
epic = ei.get("Epicenter") or ei.get("epicenter") or {}
|
| 80 |
+
mag_info = ei.get("Magnitude") or ei.get("magnitude") or ei.get("EarthquakeMagnitude") or {}
|
| 81 |
+
depth_raw = ei.get("FocalDepth") or ei.get("depth") or ei.get("Depth")
|
| 82 |
mag_raw = mag_info.get("MagnitudeValue") or mag_info.get("magnitudeValue") or mag_info.get("Value") or mag_info.get("value")
|
| 83 |
|
| 84 |
rows.append({
|
| 85 |
"ID": q.get("EarthquakeNo"), "Time": ei.get("OriginTime"),
|
| 86 |
+
"Lat": _to_float(epic.get("EpicenterLatitude") or epic.get("epicenterLatitude")),
|
| 87 |
+
"Lon": _to_float(epic.get("EpicenterLongitude") or epic.get("epicenterLongitude")),
|
| 88 |
+
"Depth": _to_float(depth_raw),
|
| 89 |
+
"Magnitude": _to_float(mag_raw),
|
| 90 |
+
"Location": epic.get("Location") or epic.get("location"),
|
| 91 |
+
"URL": q.get("Web") or q.get("ReportURL"),
|
| 92 |
})
|
| 93 |
+
|
| 94 |
df = pd.DataFrame(rows)
|
| 95 |
if not df.empty and "Time" in df.columns:
|
| 96 |
df["Time"] = pd.to_datetime(df["Time"], errors="coerce", utc=True).dt.tz_convert(TAIPEI_TZ)
|
|
|
|
| 144 |
return latest_eq_data
|
| 145 |
except Exception as e:
|
| 146 |
raise e
|
| 147 |
+
|