Spaces:
Sleeping
Sleeping
Update services/usgs_service.py
Browse files- services/usgs_service.py +37 -1
services/usgs_service.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# usgs_service.py
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
from datetime import datetime, timedelta, timezone
|
|
@@ -76,4 +77,39 @@ def fetch_taiwan_df_this_year(min_mag: float = 5.0) -> pd.DataFrame | str:
|
|
| 76 |
})
|
| 77 |
return pd.DataFrame(rows)
|
| 78 |
except Exception as e:
|
| 79 |
-
return f"❌ 查詢失敗: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# usgs_service.py
|
| 2 |
+
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from datetime import datetime, timedelta, timezone
|
|
|
|
| 77 |
})
|
| 78 |
return pd.DataFrame(rows)
|
| 79 |
except Exception as e:
|
| 80 |
+
return f"❌ 查詢失敗: {e}"
|
| 81 |
+
|
| 82 |
+
# --- ✨ 以下是新增的函式 ---
|
| 83 |
+
def fetch_usgs_earthquakes_by_date(start_time_str: str, end_time_str: str, min_mag: float, limit: int = 20) -> str:
|
| 84 |
+
"""根據指定的日期範圍和最小規模,從 USGS API 查詢全球地震。"""
|
| 85 |
+
params = {
|
| 86 |
+
"format": "geojson",
|
| 87 |
+
"starttime": start_time_str,
|
| 88 |
+
"endtime": end_time_str,
|
| 89 |
+
"minmagnitude": float(min_mag),
|
| 90 |
+
"limit": int(limit),
|
| 91 |
+
"orderby": "time-asc", # 按時間正序排列
|
| 92 |
+
}
|
| 93 |
+
try:
|
| 94 |
+
r = requests.get(USGS_API_BASE_URL, params=params, timeout=20)
|
| 95 |
+
r.raise_for_status()
|
| 96 |
+
features = r.json().get("features", [])
|
| 97 |
+
if not features:
|
| 98 |
+
return f"✅ 在 {start_time_str} 至 {end_time_str} 期間,全球無規模 {min_mag} 以上的地震紀錄。"
|
| 99 |
+
|
| 100 |
+
lines = [f"🔎 {start_time_str} 至 {end_time_str} 全球地震 (M≥{min_mag}):", "-" * 20]
|
| 101 |
+
for f in features:
|
| 102 |
+
p = f["properties"]
|
| 103 |
+
t_utc = datetime.fromtimestamp(p["time"] / 1000, tz=timezone.utc)
|
| 104 |
+
lines.append(
|
| 105 |
+
f"規模: {p['mag']:.1f} | 時間: {t_utc.strftime('%Y-%m-%d %H:%M')} (UTC)\n"
|
| 106 |
+
f"地點: {p.get('place', 'N/A')}\n"
|
| 107 |
+
f"報告: {p.get('url', '無')}"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if len(features) >= limit:
|
| 111 |
+
lines.append(f"\n(注意:結果已達上限 {limit} 筆,可能還有更多地震未顯示)")
|
| 112 |
+
|
| 113 |
+
return "\n\n".join(lines)
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return f"❌ 查詢失敗:{e}"
|