Spaces:
Sleeping
Sleeping
chen commited on
Update cwa_service.py
Browse files27 unmodified lines28293031323317 unmodified lines51525354555657585960616242 unmodified lines1051061071081091101117 unmodified lines11912012112227 unmodified lines# --- 地震預警 (CWA_ALARM_API) ---def fetch_cwa_alarm_list(limit: int = 5) -> str:try:r = requests.get(CWA_ALARM_API, timeout=10)r.raise_for_status()17 unmodified linesfor it in items[:limit]:mag = _to_float(it.get("magnitudeValue"))depth = _to_float(it.get("depth"))tw_str, utc_str = _parse_cwa_time(it.get("originTime", ""))lines.append(f"事件: {it.get('identifier', '—')} | 類型: {it.get('msgType', '—')}#{it.get('msgNo', '—')}\n"f"震級/深度: M{mag:.1f if mag is not None else '—'} / {depth:.0f if depth is not None else '—'} km\n"f"時間: {tw_str}(台灣)\n"f"預警地區: {it.get('alertAreas') or '—'}")return "\n\n".join(lines).strip()42 unmodified lineslines = [f"🚨 CWA 最新顯著有感地震 (近{days}天內):", "-" * 20]for _, row in df.iterrows():# [修正] 在格式化前,先檢查值是否存在mag_str = f"{row['Magnitude']:.1f}" if pd.notna(row['Magnitude']) else "—"depth_str = f"{row['Depth']:.0f}" if pd.notna(row['Depth']) else "—"7 unmodified linesexcept Exception as e:return f"❌ 顯著地震查詢失敗:{e}"27 unmodified lines2829303132333417 unmodified lines5253545556575859606162636465666768697071727342 unmodified lines1161171181191201217 unmodified lines12913013127 unmodified lines# --- 地震預警 (CWA_ALARM_API) ---def fetch_cwa_alarm_list(limit: int = 5) -> str:"""抓 CWA 地震預警並格式化輸出。"""try:r = requests.get(CWA_ALARM_API, timeout=10)r.raise_for_status()17 unmodified linesfor it in items[:limit]:mag = _to_float(it.get("magnitudeValue"))depth = _to_float(it.get("depth"))tw_str, _ = _parse_cwa_time(it.get("originTime", ""))# [修正] 在格式化前,先對來自 API 的字串進行轉義,避免其中的 { } 字元造成錯誤identifier = str(it.get('identifier', '—')).replace('{', '{{').replace('}', '}}')msg_type = str(it.get('msgType', '—')).replace('{', '{{').replace('}', '}}')msg_no = str(it.get('msgNo', '—')).replace('{', '{{').replace('}', '}}')areas = str(it.get('alertAreas') or '—').replace('{', '{{').replace('}', '}}')mag_str = f"{mag:.1f}" if mag is not None else "—"depth_str = f"{depth:.0f}" if depth is not None else "—"lines.append(f"事件: {identifier} | 類型: {msg_type}#{msg_no}\n"f"震級/深度: M{mag_str} / {depth_str} km\n"f"時間: {tw_str}(台灣)\n"f"預警地區: {areas}")return "\n\n".join(lines).strip()42 unmodified lineslines = [f"🚨 CWA 最新顯著有感地震 (近{days}天內):", "-" * 20]for _, row in df.iterrows():mag_str = f"{row['Magnitude']:.1f}" if pd.notna(row['Magnitude']) else "—"depth_str = f"{row['Depth']:.0f}" if pd.notna(row['Depth']) else "—"7 unmodified linesexcept Exception as e:return f"❌ 顯著地震查詢失敗:{e}"
cwa_service.py+15-6