Spaces:
Sleeping
Sleeping
Update cwa_service.py
Browse files- cwa_service.py +11 -2
cwa_service.py
CHANGED
|
@@ -29,6 +29,7 @@ def _parse_cwa_time(s: str) -> tuple[str, str]:
|
|
| 29 |
# --- 地震預警 (CWA_ALARM_API) ---
|
| 30 |
def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
| 31 |
"""抓 CWA 地震預警並格式化輸出。"""
|
|
|
|
| 32 |
try:
|
| 33 |
r = requests.get(CWA_ALARM_API, timeout=10)
|
| 34 |
r.raise_for_status()
|
|
@@ -68,26 +69,35 @@ def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
|
| 68 |
|
| 69 |
# --- 顯著有感地震 (E-A0015-001) ---
|
| 70 |
def _parse_significant_earthquakes(obj: dict) -> pd.DataFrame:
|
|
|
|
| 71 |
quakes = obj.get("records", {}).get("Earthquake", [])
|
| 72 |
rows = []
|
| 73 |
for q in quakes:
|
| 74 |
ei = q.get("EarthquakeInfo", {})
|
| 75 |
epic = ei.get("Epicenter", {})
|
| 76 |
mag_info = ei.get("Magnitude", {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
rows.append({
|
| 78 |
"ID": q.get("EarthquakeNo"), "Time": ei.get("OriginTime"),
|
| 79 |
"Lat": _to_float(epic.get("EpicenterLatitude")), "Lon": _to_float(epic.get("EpicenterLongitude")),
|
| 80 |
-
"Depth": _to_float(
|
|
|
|
| 81 |
"Location": epic.get("Location"), "URL": q.get("Web"),
|
| 82 |
})
|
| 83 |
df = pd.DataFrame(rows)
|
| 84 |
if not df.empty and "Time" in df.columns:
|
| 85 |
time_series = pd.to_datetime(df["Time"], errors="coerce")
|
| 86 |
if pd.api.types.is_datetime64_any_dtype(time_series):
|
|
|
|
| 87 |
df["Time"] = time_series.dt.tz_localize("UTC").dt.tz_convert(TAIPEI_TZ)
|
| 88 |
return df
|
| 89 |
|
| 90 |
def fetch_significant_earthquakes(days: int = 7, limit: int = 5) -> str:
|
|
|
|
| 91 |
if not CWA_API_KEY: return "❌ 顯著地震查詢失敗:管理者尚未設定 CWA_API_KEY。"
|
| 92 |
now = datetime.now(timezone.utc)
|
| 93 |
time_from = (now - timedelta(days=days)).strftime("%Y-%m-%d")
|
|
@@ -113,4 +123,3 @@ def fetch_significant_earthquakes(days: int = 7, limit: int = 5) -> str:
|
|
| 113 |
except Exception as e:
|
| 114 |
return f"❌ 顯著地震查詢失敗:{e}"
|
| 115 |
|
| 116 |
-
# [移除] _normalize_cwa_area_name 和 fetch_local_earthquakes 兩個函式
|
|
|
|
| 29 |
# --- 地震預警 (CWA_ALARM_API) ---
|
| 30 |
def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
| 31 |
"""抓 CWA 地震預警並格式化輸出。"""
|
| 32 |
+
# ... (此函式內容不變) ...
|
| 33 |
try:
|
| 34 |
r = requests.get(CWA_ALARM_API, timeout=10)
|
| 35 |
r.raise_for_status()
|
|
|
|
| 69 |
|
| 70 |
# --- 顯著有感地震 (E-A0015-001) ---
|
| 71 |
def _parse_significant_earthquakes(obj: dict) -> pd.DataFrame:
|
| 72 |
+
"""[修改] 採用更穩健的方式解析 E-A0015-001 的 JSON 資料"""
|
| 73 |
quakes = obj.get("records", {}).get("Earthquake", [])
|
| 74 |
rows = []
|
| 75 |
for q in quakes:
|
| 76 |
ei = q.get("EarthquakeInfo", {})
|
| 77 |
epic = ei.get("Epicenter", {})
|
| 78 |
mag_info = ei.get("Magnitude", {})
|
| 79 |
+
|
| 80 |
+
# 嘗試用多種可能的欄位名稱去取得資料
|
| 81 |
+
depth_raw = ei.get("FocalDepth") or ei.get("Depth")
|
| 82 |
+
mag_raw = mag_info.get("MagnitudeValue") or mag_info.get("value") or mag_info.get("Magnitude")
|
| 83 |
+
|
| 84 |
rows.append({
|
| 85 |
"ID": q.get("EarthquakeNo"), "Time": ei.get("OriginTime"),
|
| 86 |
"Lat": _to_float(epic.get("EpicenterLatitude")), "Lon": _to_float(epic.get("EpicenterLongitude")),
|
| 87 |
+
"Depth": _to_float(depth_raw),
|
| 88 |
+
"Magnitude": _to_float(mag_raw),
|
| 89 |
"Location": epic.get("Location"), "URL": q.get("Web"),
|
| 90 |
})
|
| 91 |
df = pd.DataFrame(rows)
|
| 92 |
if not df.empty and "Time" in df.columns:
|
| 93 |
time_series = pd.to_datetime(df["Time"], errors="coerce")
|
| 94 |
if pd.api.types.is_datetime64_any_dtype(time_series):
|
| 95 |
+
# 假設 API 回傳的時間是 UTC 標準時間
|
| 96 |
df["Time"] = time_series.dt.tz_localize("UTC").dt.tz_convert(TAIPEI_TZ)
|
| 97 |
return df
|
| 98 |
|
| 99 |
def fetch_significant_earthquakes(days: int = 7, limit: int = 5) -> str:
|
| 100 |
+
# ... (此函式上半部分不變) ...
|
| 101 |
if not CWA_API_KEY: return "❌ 顯著地震查詢失敗:管理者尚未設定 CWA_API_KEY。"
|
| 102 |
now = datetime.now(timezone.utc)
|
| 103 |
time_from = (now - timedelta(days=days)).strftime("%Y-%m-%d")
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
return f"❌ 顯著地震查詢失敗:{e}"
|
| 125 |
|
|
|