Spaces:
Sleeping
Sleeping
Update pws_service.py
Browse files- pws_service.py +66 -4
pws_service.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# pws_service.py (Robust Version)
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
| 4 |
from config import PWS_API_URL, CWA_PWS_EARTHQUAKE_API
|
| 5 |
|
| 6 |
def fetch_latest_pws_info() -> str:
|
|
@@ -54,17 +55,78 @@ def fetch_latest_pws_info() -> str:
|
|
| 54 |
return f"❌ PWS 查詢失敗:發生未預期的錯誤。\n錯誤訊息:{e}"
|
| 55 |
|
| 56 |
def fetch_cwa_pws_earthquake_info() -> str:
|
| 57 |
-
"""從指定的 API 端點擷取最新的 CWA PWS
|
| 58 |
try:
|
| 59 |
response = requests.get(CWA_PWS_EARTHQUAKE_API, timeout=15)
|
| 60 |
-
response.raise_for_status()
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
except requests.exceptions.Timeout:
|
| 66 |
return "❌ PWS 地震訊息查詢失敗:連線超時。"
|
| 67 |
except requests.exceptions.RequestException as e:
|
| 68 |
return f"❌ PWS 地震訊息查詢失敗:網路連線錯誤。\n錯誤訊息:{e}"
|
|
|
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"❌ PWS 地震訊息查詢失敗:發生未預期的錯誤。\n錯誤訊息:{e}"
|
|
|
|
| 1 |
# pws_service.py (Robust Version)
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
from config import PWS_API_URL, CWA_PWS_EARTHQUAKE_API
|
| 6 |
|
| 7 |
def fetch_latest_pws_info() -> str:
|
|
|
|
| 55 |
return f"❌ PWS 查詢失敗:發生未預期的錯誤。\n錯誤訊息:{e}"
|
| 56 |
|
| 57 |
def fetch_cwa_pws_earthquake_info() -> str:
|
| 58 |
+
"""從指定的 API 端點擷取最新的 CWA PWS 地震訊息並格式化輸出。"""
|
| 59 |
try:
|
| 60 |
response = requests.get(CWA_PWS_EARTHQUAKE_API, timeout=15)
|
| 61 |
+
response.raise_for_status()
|
| 62 |
|
| 63 |
+
data = response.json()
|
| 64 |
+
|
| 65 |
+
source = data.get("source", "未知來源")
|
| 66 |
+
last_updated_raw = data.get("last_updated")
|
| 67 |
+
|
| 68 |
+
# 格式化資料更新時間
|
| 69 |
+
if last_updated_raw:
|
| 70 |
+
try:
|
| 71 |
+
last_updated_dt = datetime.fromisoformat(last_updated_raw)
|
| 72 |
+
last_updated_str = last_updated_dt.strftime('%Y-%m-%d %H:%M')
|
| 73 |
+
except (ValueError, TypeError):
|
| 74 |
+
last_updated_str = last_updated_raw
|
| 75 |
+
else:
|
| 76 |
+
last_updated_str = "未知"
|
| 77 |
+
|
| 78 |
+
# 建立訊息標頭
|
| 79 |
+
lines = [
|
| 80 |
+
f"📢 最新地震PWS警報 ({source})",
|
| 81 |
+
f" (資料更新時間: {last_updated_str})",
|
| 82 |
+
"─" * 25
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
alerts = data.get("alerts", [])
|
| 86 |
+
if not alerts:
|
| 87 |
+
lines.append("\n✅ 目前無最新的地震PWS警報。")
|
| 88 |
+
return "\n".join(lines)
|
| 89 |
+
|
| 90 |
+
# 逐條解析並格式化警報
|
| 91 |
+
for i, alert in enumerate(alerts):
|
| 92 |
+
title = alert.get("title", "無標題")
|
| 93 |
+
publish_time_raw = alert.get("publish_time")
|
| 94 |
+
|
| 95 |
+
# 格式化警報發布時間
|
| 96 |
+
if publish_time_raw:
|
| 97 |
+
try:
|
| 98 |
+
publish_time_dt = datetime.fromisoformat(publish_time_raw)
|
| 99 |
+
publish_time_str = publish_time_dt.strftime('%Y-%m-%d %H:%M')
|
| 100 |
+
except (ValueError, TypeError):
|
| 101 |
+
publish_time_str = publish_time_raw
|
| 102 |
+
else:
|
| 103 |
+
publish_time_str = "未知"
|
| 104 |
+
|
| 105 |
+
summary = alert.get("summary", "無摘要。").strip()
|
| 106 |
+
affected_counties = alert.get("affected_counties", [])
|
| 107 |
+
affected_str = ", ".join(affected_counties) if affected_counties else "未提供"
|
| 108 |
+
|
| 109 |
+
# 組合單條警報訊息
|
| 110 |
+
alert_lines = [
|
| 111 |
+
f"🚨 {title} ({publish_time_str})",
|
| 112 |
+
f"🗺️ 影響地區: {affected_str}",
|
| 113 |
+
f"💬 內容: {summary}"
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
# 將單條警報加入主訊息列表
|
| 117 |
+
lines.append("\n".join(alert_lines))
|
| 118 |
+
|
| 119 |
+
# 在警報之間加入分隔線
|
| 120 |
+
if i < len(alerts) - 1:
|
| 121 |
+
lines.append("-" * 15)
|
| 122 |
+
|
| 123 |
+
return "\n\n".join(lines)
|
| 124 |
|
| 125 |
except requests.exceptions.Timeout:
|
| 126 |
return "❌ PWS 地震訊息查詢失敗:連線超時。"
|
| 127 |
except requests.exceptions.RequestException as e:
|
| 128 |
return f"❌ PWS 地震訊息查詢失敗:網路連線錯誤。\n錯誤訊息:{e}"
|
| 129 |
+
except json.JSONDecodeError:
|
| 130 |
+
return "❌ PWS 地震訊息查詢失敗:無法解析伺服器回傳的資料格式。"
|
| 131 |
except Exception as e:
|
| 132 |
return f"❌ PWS 地震訊息查詢失敗:發生未預期的錯誤。\n錯誤訊息:{e}"
|