Spaces:
Sleeping
Sleeping
Create pws_service.py
Browse files- pws_service.py +18 -2
pws_service.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# pws_service.py (Robust Version)
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
-
from config import PWS_API_URL
|
| 5 |
|
| 6 |
def fetch_latest_pws_info() -> str:
|
| 7 |
"""
|
|
@@ -51,4 +51,20 @@ def fetch_latest_pws_info() -> str:
|
|
| 51 |
except requests.exceptions.RequestException as e:
|
| 52 |
return f"❌ PWS 查詢失敗:網路連線錯誤。\n錯誤訊息:{e}"
|
| 53 |
except Exception as e:
|
| 54 |
-
return f"❌ PWS 查詢失敗:發生未預期的錯誤。\n錯誤訊息:{e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|
| 7 |
"""
|
|
|
|
| 51 |
except requests.exceptions.RequestException as e:
|
| 52 |
return f"❌ PWS 查詢失敗:網路連線錯誤。\n錯誤訊息:{e}"
|
| 53 |
except Exception as e:
|
| 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() # 如果請求失敗 (e.g., 404, 500),則引發例外
|
| 61 |
+
|
| 62 |
+
# 假設 API 直接回傳格式化好的純文字
|
| 63 |
+
return response.text
|
| 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}"
|