Spaces:
Sleeping
Sleeping
chen commited on
Update weather_service.py
Browse files2 unmodified lines34567892 unmodified lines1213141516171823 unmodified lines424344454647488 unmodified lines575859606162632 unmodified linesfrom config import CWA_AUTH_KEYfrom datetime import datetime# [修改] API 端點更新為 36 小時預報CWA_FORECAST_API = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/F-C0032-001"def _normalize_location_name(loc_name: str) -> str:2 unmodified linesif loc_name.endswith("市") or loc_name.endswith("縣"):return loc_name# 根據 API 文件支援的主要縣市列表進行校正major_cities = "臺北,桃園,新竹,臺中,嘉義,臺南,高雄,基隆".split(',')if any(city in loc_name for city in major_cities):return f"{loc_name}市"23 unmodified lines"Authorization": CWA_AUTH_KEY,"format": "JSON","locationName": normalized_loc,"elementName": "Wx,PoP,MinT,MaxT", # 天氣現象,降雨機率,最低溫,最高溫}try:8 unmodified lineselements = {elem["elementName"]: elem["time"] for elem in loc_data["weatherElement"]}forecasts = []# API 會回傳 3 個時間段的預報for i in range(3):try:start_time = elements["Wx"][i]["startTime"]2 unmodified lines3456782 unmodified lines11121314151623 unmodified lines40414243444546478 unmodified lines5657585960612 unmodified linesfrom config import CWA_AUTH_KEYfrom datetime import datetimeCWA_FORECAST_API = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/F-C0032-001"def _normalize_location_name(loc_name: str) -> str:2 unmodified linesif loc_name.endswith("市") or loc_name.endswith("縣"):return loc_namemajor_cities = "臺北,桃園,新竹,臺中,嘉義,臺南,高雄,基隆".split(',')if any(city in loc_name for city in major_cities):return f"{loc_name}市"23 unmodified lines"Authorization": CWA_AUTH_KEY,"format": "JSON","locationName": normalized_loc,# [修改] 將 elementName 改為列表格式,以符合 API 規範"elementName": ["Wx", "PoP", "MinT", "MaxT"],}try:8 unmodified lineselements = {elem["elementName"]: elem["time"] for elem in loc_data["weatherElement"]}forecasts = []for i in range(3):try:start_time = elements["Wx"][i]["startTime"]
weather_service.py+2-4