cwadayi commited on
Commit
ffd7435
·
verified ·
1 Parent(s): 2edbd99

Update cwa_service.py

Browse files
Files changed (1) hide show
  1. cwa_service.py +2 -58
cwa_service.py CHANGED
@@ -5,7 +5,7 @@ import requests
5
  import re
6
  import pandas as pd
7
  from datetime import datetime, timedelta, timezone
8
- from config import CWA_API_KEY, CWA_ALARM_API, CWA_SIGNIFICANT_API, CWA_LOCAL_EQ_API
9
 
10
  TAIPEI_TZ = timezone(timedelta(hours=8))
11
 
@@ -26,18 +26,6 @@ def _parse_cwa_time(s: str) -> tuple[str, str]:
26
  except Exception:
27
  return (s, "未知")
28
 
29
- def _normalize_cwa_area_name(area_name: str) -> str:
30
- """自動校正縣市名稱,以符合 CWA API 查詢需求"""
31
- area_name = area_name.replace("台", "臺")
32
- if area_name.endswith("市") or area_name.endswith("縣"):
33
- return area_name
34
-
35
- major_cities = "臺北,新北,基隆,桃園,新竹,臺中,嘉義,臺南,高雄".split(',')
36
- if any(city in area_name for city in major_cities):
37
- return f"{area_name}市"
38
- else:
39
- return f"{area_name}縣"
40
-
41
  # --- 地震預警 (CWA_ALARM_API) ---
42
  def fetch_cwa_alarm_list(limit: int = 5) -> str:
43
  """抓 CWA 地震預警並格式化輸出。"""
@@ -125,48 +113,4 @@ def fetch_significant_earthquakes(days: int = 7, limit: int = 5) -> str:
125
  except Exception as e:
126
  return f"❌ 顯著地震查詢失敗:{e}"
127
 
128
- # --- 小區域有感地震 (E-A0016-001) ---
129
- def fetch_local_earthquakes(area_name: str = "", limit: int = 5) -> str:
130
- """從 CWA 獲取小區域有感地震報告。如果 area_name 為空,則查詢全台灣。"""
131
- if not CWA_API_KEY: return "❌ 查詢失敗:管理者尚未設定 CWA_API_KEY。"
132
-
133
- params = {"Authorization": CWA_API_KEY, "format": "JSON", "limit": limit}
134
- title = "🇹🇼 台灣近期小區域有感地震:"
135
-
136
- if area_name:
137
- normalized_area = _normalize_cwa_area_name(area_name)
138
- params["AreaName"] = normalized_area
139
- title = f"🚨 「{normalized_area}」近期小區域有感地震:"
140
-
141
- try:
142
- r = requests.get(CWA_LOCAL_EQ_API, params=params, timeout=15)
143
- r.raise_for_status()
144
- data = r.json()
145
-
146
- earthquakes = data.get("records", {}).get("Earthquake", [])
147
- if not earthquakes:
148
- msg = f"✅ 在「{area_name}」" if area_name else "✅ 台灣"
149
- return f"{msg}近期沒有小區域有感地震報告。"
150
-
151
- lines = [title, "-" * 20]
152
- for eq in earthquakes:
153
- info = eq.get("earthquakeInfo", {})
154
- epi = info.get("epicenter", {})
155
- mag = info.get("magnitude", {})
156
- depth = info.get("depth", {})
157
-
158
- intensity_areas = eq.get("intensity", {}).get("shakingArea", [])
159
- area_strs = [f"{area.get('areaDesc')} {area.get('areaIntensity')}級" for area in intensity_areas if area.get("areaIntensity") and float(area.get("areaIntensity", 0)) > 0]
160
- intensity_str = "、".join(area_strs) if area_strs else "無具體震度回報"
161
-
162
- lines.append(
163
- f"報告: {eq.get('reportContent', '—')}\n"
164
- f"時間: {info.get('originTime', '—')}\n"
165
- f"地點: {epi.get('location', '—')}\n"
166
- f"規模: M{mag.get('magnitudeValue', '—')} | 深度: {depth.get('value', '—')} km\n"
167
- f"主要影響區域: {intensity_str}"
168
- )
169
- return "\n\n".join(lines)
170
-
171
- except Exception as e:
172
- return f"❌ 小區域地震查詢失敗:{e}"
 
5
  import re
6
  import pandas as pd
7
  from datetime import datetime, timedelta, timezone
8
+ from config import CWA_API_KEY, CWA_ALARM_API, CWA_SIGNIFICANT_API
9
 
10
  TAIPEI_TZ = timezone(timedelta(hours=8))
11
 
 
26
  except Exception:
27
  return (s, "未知")
28
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # --- 地震預警 (CWA_ALARM_API) ---
30
  def fetch_cwa_alarm_list(limit: int = 5) -> str:
31
  """抓 CWA 地震預警並格式化輸出。"""
 
113
  except Exception as e:
114
  return f"❌ 顯著地震查詢失敗:{e}"
115
 
116
+ # [移除] _normalize_cwa_area_name fetch_local_earthquakes 兩個函式