Spaces:
Sleeping
Sleeping
Update weather_service.py
Browse files- weather_service.py +34 -6
weather_service.py
CHANGED
|
@@ -1,30 +1,54 @@
|
|
| 1 |
# weather_service.py
|
| 2 |
import requests
|
| 3 |
-
from config import CWA_AUTH_KEY
|
| 4 |
|
| 5 |
CWA_WEATHER_API = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/O-A0003-001"
|
| 6 |
|
| 7 |
def fetch_weather_by_location(location_name: str) -> str:
|
| 8 |
"""根據地點名稱查詢即時天氣資訊。"""
|
| 9 |
-
# [新增] 檢查金鑰是否存在
|
| 10 |
if not CWA_AUTH_KEY:
|
| 11 |
return "❌ 天氣查詢失敗:管理者尚未設定 CWA_AUTH_KEY。"
|
| 12 |
|
| 13 |
params = {
|
| 14 |
"Authorization": CWA_AUTH_KEY,
|
| 15 |
"format": "JSON",
|
| 16 |
-
|
| 17 |
"elementName": "TEMP,HUMD,Weather",
|
| 18 |
}
|
| 19 |
try:
|
| 20 |
-
r = requests.get(CWA_WEATHER_API, params=params, timeout=
|
| 21 |
r.raise_for_status()
|
| 22 |
data = r.json()
|
| 23 |
|
| 24 |
if not data.get("records") or not data["records"].get("location"):
|
| 25 |
-
return
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
elements = {elem["elementName"]: elem["elementValue"] for elem in loc_data["weatherElement"]}
|
| 29 |
|
| 30 |
temp = elements.get("TEMP", "未知")
|
|
@@ -38,5 +62,9 @@ def fetch_weather_by_location(location_name: str) -> str:
|
|
| 38 |
f"相對濕度:{humidity:.0f}%"
|
| 39 |
)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
return f"❌ 天氣查詢失敗:{e}"
|
|
|
|
| 1 |
# weather_service.py
|
| 2 |
import requests
|
| 3 |
+
from config import CWA_AUTH_KEY
|
| 4 |
|
| 5 |
CWA_WEATHER_API = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/O-A0003-001"
|
| 6 |
|
| 7 |
def fetch_weather_by_location(location_name: str) -> str:
|
| 8 |
"""根據地點名稱查詢即時天氣資訊。"""
|
|
|
|
| 9 |
if not CWA_AUTH_KEY:
|
| 10 |
return "❌ 天氣查詢失敗:管理者尚未設定 CWA_AUTH_KEY。"
|
| 11 |
|
| 12 |
params = {
|
| 13 |
"Authorization": CWA_AUTH_KEY,
|
| 14 |
"format": "JSON",
|
| 15 |
+
# [修改] 移除 locationName,一次獲取所有測站資料
|
| 16 |
"elementName": "TEMP,HUMD,Weather",
|
| 17 |
}
|
| 18 |
try:
|
| 19 |
+
r = requests.get(CWA_WEATHER_API, params=params, timeout=15)
|
| 20 |
r.raise_for_status()
|
| 21 |
data = r.json()
|
| 22 |
|
| 23 |
if not data.get("records") or not data["records"].get("location"):
|
| 24 |
+
return "無法獲取任何天氣觀測站資料。"
|
| 25 |
|
| 26 |
+
all_locations = data["records"]["location"]
|
| 27 |
+
|
| 28 |
+
# [新增] 遍歷所有測站,尋找符合使用者輸入的地點
|
| 29 |
+
# 處理 "臺" 和 "台" 的異體字,並移除 "市"
|
| 30 |
+
normalized_input = location_name.replace("臺", "台").replace("市", "")
|
| 31 |
+
|
| 32 |
+
target_location = None
|
| 33 |
+
for loc in all_locations:
|
| 34 |
+
normalized_loc_name = loc["locationName"].replace("臺", "台")
|
| 35 |
+
if normalized_input == normalized_loc_name:
|
| 36 |
+
target_location = loc
|
| 37 |
+
break # 找到完全符合的就停止
|
| 38 |
+
|
| 39 |
+
# 如果沒有完全符合的,則找名稱包含的
|
| 40 |
+
if not target_location:
|
| 41 |
+
for loc in all_locations:
|
| 42 |
+
normalized_loc_name = loc["locationName"].replace("臺", "台")
|
| 43 |
+
if normalized_input in normalized_loc_name:
|
| 44 |
+
target_location = loc
|
| 45 |
+
break
|
| 46 |
+
|
| 47 |
+
if not target_location:
|
| 48 |
+
return f"找不到與「{location_name}」相關的即時天氣資訊,請試試看其他縣市或地區。"
|
| 49 |
+
|
| 50 |
+
# --- 找到地點後,格式化輸出 ---
|
| 51 |
+
loc_data = target_location
|
| 52 |
elements = {elem["elementName"]: elem["elementValue"] for elem in loc_data["weatherElement"]}
|
| 53 |
|
| 54 |
temp = elements.get("TEMP", "未知")
|
|
|
|
| 62 |
f"相對濕度:{humidity:.0f}%"
|
| 63 |
)
|
| 64 |
|
| 65 |
+
except requests.exceptions.HTTPError as e:
|
| 66 |
+
if e.response.status_code == 401:
|
| 67 |
+
return "❌ 天氣查詢失敗:CWA 授權金鑰 (API Key) 無效。"
|
| 68 |
+
return f"❌ 天氣查詢失敗:{e}"
|
| 69 |
except Exception as e:
|
| 70 |
return f"❌ 天氣查詢失敗:{e}"
|