Spaces:
Sleeping
Sleeping
Update cwa_service.py
Browse files- cwa_service.py +6 -5
cwa_service.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# cwa_service.py (Corrected for tz-naive error)
|
| 2 |
import requests
|
| 3 |
import re
|
| 4 |
import pandas as pd
|
|
@@ -74,7 +74,10 @@ def _parse_significant_earthquakes(obj: dict) -> pd.DataFrame:
|
|
| 74 |
epic = ei.get("Epicenter", {})
|
| 75 |
mag_info = ei.get("Magnitude", {})
|
| 76 |
depth_raw = ei.get("FocalDepth")
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
rows.append({
|
| 79 |
"ID": q.get("EarthquakeNo"), "Time": ei.get("OriginTime"),
|
| 80 |
"Lat": _to_float(epic.get("EpicenterLatitude")),
|
|
@@ -84,7 +87,6 @@ def _parse_significant_earthquakes(obj: dict) -> pd.DataFrame:
|
|
| 84 |
})
|
| 85 |
df = pd.DataFrame(rows)
|
| 86 |
if not df.empty and "Time" in df.columns:
|
| 87 |
-
# [修正] 加入 utc=True 參數,確保所有時間都有 UTC 時區資訊
|
| 88 |
df["Time"] = pd.to_datetime(df["Time"], errors="coerce", utc=True).dt.tz_convert(TAIPEI_TZ)
|
| 89 |
return df
|
| 90 |
|
|
@@ -100,7 +102,7 @@ def fetch_significant_earthquakes(days: int = 7, limit: int = 5) -> str:
|
|
| 100 |
df = _parse_significant_earthquakes(data)
|
| 101 |
if df.empty: return f"✅ 過去 {days} 天內沒有顯著有感地震報告。"
|
| 102 |
df = df.sort_values(by="Time", ascending=False).head(limit)
|
| 103 |
-
lines = [f"🚨 CWA 最新顯著有感地震 (近{days}
|
| 104 |
for _, row in df.iterrows():
|
| 105 |
mag_str = f"{row['Magnitude']:.1f}" if pd.notna(row['Magnitude']) else "—"
|
| 106 |
depth_str = f"{row['Depth']:.0f}" if pd.notna(row['Depth']) else "—"
|
|
@@ -135,5 +137,4 @@ def fetch_latest_significant_earthquake() -> dict | None:
|
|
| 135 |
|
| 136 |
return latest_eq_data
|
| 137 |
except Exception as e:
|
| 138 |
-
# 將錯誤傳遞給上層函式,以便在 LINE 上顯示
|
| 139 |
raise e
|
|
|
|
| 1 |
+
# cwa_service.py (Corrected for Magnitude and tz-naive error)
|
| 2 |
import requests
|
| 3 |
import re
|
| 4 |
import pandas as pd
|
|
|
|
| 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")),
|
|
|
|
| 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)
|
| 91 |
return df
|
| 92 |
|
|
|
|
| 102 |
df = _parse_significant_earthquakes(data)
|
| 103 |
if df.empty: return f"✅ 過去 {days} 天內沒有顯著有感地震報告。"
|
| 104 |
df = df.sort_values(by="Time", ascending=False).head(limit)
|
| 105 |
+
lines = [f"🚨 CWA 最新顯著有感地震 (近{days}天内):", "-" * 20]
|
| 106 |
for _, row in df.iterrows():
|
| 107 |
mag_str = f"{row['Magnitude']:.1f}" if pd.notna(row['Magnitude']) else "—"
|
| 108 |
depth_str = f"{row['Depth']:.0f}" if pd.notna(row['Depth']) else "—"
|
|
|
|
| 137 |
|
| 138 |
return latest_eq_data
|
| 139 |
except Exception as e:
|
|
|
|
| 140 |
raise e
|