Spaces:
Sleeping
Sleeping
Update cwa_service.py
Browse files- cwa_service.py +33 -9
cwa_service.py
CHANGED
|
@@ -14,16 +14,36 @@ def _to_float(x):
|
|
| 14 |
return float(m.group()) if m else None
|
| 15 |
|
| 16 |
def _parse_cwa_time(s: str) -> tuple[str, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if not s: return ("未知", "未知")
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
|
|
|
| 27 |
try:
|
| 28 |
r = requests.get(CWA_ALARM_API, timeout=10)
|
| 29 |
r.raise_for_status()
|
|
@@ -48,8 +68,12 @@ def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
|
| 48 |
msg_type = str(it.get('msgType', '—')).replace('{', '{{').replace('}', '}}')
|
| 49 |
msg_no = str(it.get('msgNo', '—')).replace('{', '{{').replace('}', '}}')
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
areas = areas_str.replace('{', '{{').replace('}', '}}')
|
| 54 |
|
| 55 |
mag_str = f"{mag:.1f}" if mag is not None else "—"
|
|
@@ -58,7 +82,7 @@ def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
|
| 58 |
f"事件: {identifier} | 類型: {msg_type}#{msg_no}\n"
|
| 59 |
f"規模/深度: M{mag_str} / {depth_str} km\n"
|
| 60 |
f"時間: {tw_str}(台灣)\n"
|
| 61 |
-
f"
|
| 62 |
)
|
| 63 |
return "\n\n".join(lines).strip()
|
| 64 |
|
|
|
|
| 14 |
return float(m.group()) if m else None
|
| 15 |
|
| 16 |
def _parse_cwa_time(s: str) -> tuple[str, str]:
|
| 17 |
+
"""
|
| 18 |
+
[修正]
|
| 19 |
+
使其能夠處理兩種 CWA API 可能的時間格式:
|
| 20 |
+
1. ISO 8601 格式 (e.g., '2025-08-19T03:55:47Z')
|
| 21 |
+
2. 本地時間格式 (e.g., '2025-08-19 09:26:11')
|
| 22 |
+
"""
|
| 23 |
if not s: return ("未知", "未知")
|
| 24 |
+
|
| 25 |
+
dt_utc = None
|
| 26 |
+
# 嘗試解析 ISO 8601 格式
|
| 27 |
try:
|
| 28 |
+
dt_utc = datetime.fromisoformat(s.replace("Z", "+00:00"))
|
| 29 |
+
except ValueError:
|
| 30 |
+
# 若失敗,嘗試解析 YYYY-MM-DD HH:MM:SS 格式,並假設其為台北時間
|
| 31 |
+
try:
|
| 32 |
+
dt_local = datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
|
| 33 |
+
dt_local = dt_local.replace(tzinfo=TAIPEI_TZ)
|
| 34 |
+
dt_utc = dt_local.astimezone(timezone.utc)
|
| 35 |
+
except Exception:
|
| 36 |
+
return (s, "未知") # 若兩種格式都失敗,回傳原始字串
|
| 37 |
+
|
| 38 |
+
if dt_utc:
|
| 39 |
+
tw_str = dt_utc.astimezone(TAIPEI_TZ).strftime("%Y-%m-%d %H:%M")
|
| 40 |
+
utc_str = dt_utc.astimezone(timezone.utc).strftime("%Y-%m-%d %H:%M")
|
| 41 |
+
return (tw_str, utc_str)
|
| 42 |
+
|
| 43 |
+
return (s, "未知")
|
| 44 |
|
| 45 |
def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
| 46 |
+
"""抓 CWA 地震預警並格式化輸出。"""
|
| 47 |
try:
|
| 48 |
r = requests.get(CWA_ALARM_API, timeout=10)
|
| 49 |
r.raise_for_status()
|
|
|
|
| 68 |
msg_type = str(it.get('msgType', '—')).replace('{', '{{').replace('}', '}}')
|
| 69 |
msg_no = str(it.get('msgNo', '—')).replace('{', '{{').replace('}', '}}')
|
| 70 |
|
| 71 |
+
# [修正] 將讀取欄位從 alertAreas 改為 locationDesc
|
| 72 |
+
location_desc_list = it.get('locationDesc')
|
| 73 |
+
if isinstance(location_desc_list, list) and location_desc_list:
|
| 74 |
+
areas_str = ", ".join(str(area) for area in location_desc_list)
|
| 75 |
+
else:
|
| 76 |
+
areas_str = "—"
|
| 77 |
areas = areas_str.replace('{', '{{').replace('}', '}}')
|
| 78 |
|
| 79 |
mag_str = f"{mag:.1f}" if mag is not None else "—"
|
|
|
|
| 82 |
f"事件: {identifier} | 類型: {msg_type}#{msg_no}\n"
|
| 83 |
f"規模/深度: M{mag_str} / {depth_str} km\n"
|
| 84 |
f"時間: {tw_str}(台灣)\n"
|
| 85 |
+
f"地點: {areas}" # 標籤改為「地點」更符合 locationDesc 的語意
|
| 86 |
)
|
| 87 |
return "\n\n".join(lines).strip()
|
| 88 |
|