cwadayi commited on
Commit
8deab79
·
verified ·
1 Parent(s): 299f8ee

Update weather_service.py

Browse files
Files changed (1) hide show
  1. weather_service.py +2 -4
weather_service.py CHANGED
@@ -3,7 +3,6 @@ import requests
3
  from config import CWA_AUTH_KEY
4
  from datetime import datetime
5
 
6
- # [修改] API 端點更新為 36 小時預報
7
  CWA_FORECAST_API = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/F-C0032-001"
8
 
9
  def _normalize_location_name(loc_name: str) -> str:
@@ -12,7 +11,6 @@ def _normalize_location_name(loc_name: str) -> str:
12
  if loc_name.endswith("市") or loc_name.endswith("縣"):
13
  return loc_name
14
 
15
- # 根據 API 文件支援的主要縣市列表進行校正
16
  major_cities = "臺北,桃園,新竹,臺中,嘉義,臺南,高雄,基隆".split(',')
17
  if any(city in loc_name for city in major_cities):
18
  return f"{loc_name}市"
@@ -42,7 +40,8 @@ def fetch_forecast_by_location(location_name: str) -> str:
42
  "Authorization": CWA_AUTH_KEY,
43
  "format": "JSON",
44
  "locationName": normalized_loc,
45
- "elementName": "Wx,PoP,MinT,MaxT", # 天氣現象,降雨機率,最低溫,最高溫
 
46
  }
47
 
48
  try:
@@ -57,7 +56,6 @@ def fetch_forecast_by_location(location_name: str) -> str:
57
  elements = {elem["elementName"]: elem["time"] for elem in loc_data["weatherElement"]}
58
 
59
  forecasts = []
60
- # API 會回傳 3 個時間段的預報
61
  for i in range(3):
62
  try:
63
  start_time = elements["Wx"][i]["startTime"]
 
3
  from config import CWA_AUTH_KEY
4
  from datetime import datetime
5
 
 
6
  CWA_FORECAST_API = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/F-C0032-001"
7
 
8
  def _normalize_location_name(loc_name: str) -> str:
 
11
  if loc_name.endswith("市") or loc_name.endswith("縣"):
12
  return loc_name
13
 
 
14
  major_cities = "臺北,桃園,新竹,臺中,嘉義,臺南,高雄,基隆".split(',')
15
  if any(city in loc_name for city in major_cities):
16
  return f"{loc_name}市"
 
40
  "Authorization": CWA_AUTH_KEY,
41
  "format": "JSON",
42
  "locationName": normalized_loc,
43
+ # [修改] 將 elementName 改為列表格式,以符合 API 規範
44
+ "elementName": ["Wx", "PoP", "MinT", "MaxT"],
45
  }
46
 
47
  try:
 
56
  elements = {elem["elementName"]: elem["time"] for elem in loc_data["weatherElement"]}
57
 
58
  forecasts = []
 
59
  for i in range(3):
60
  try:
61
  start_time = elements["Wx"][i]["startTime"]