feat: Add multi-source weather data collection supporting OpenWeatherMap, Visual Crossing, and NOAA METAR, and include a utility for checking MGM weather data freshness.

This commit is contained in:
2569718930@qq.com
2026-02-08 18:15:25 +08:00
parent be09c6384f
commit 3bfd1b7c7b
3 changed files with 42 additions and 7 deletions
+7
View File
@@ -325,6 +325,13 @@ def start_bot():
msg_lines.append(f" 🌬️ {dir_str}{wind_dir}° / {mgm_curr.get('wind_speed_ms')} m/s")
if mgm_curr.get("rain_24h") is not None:
msg_lines.append(f" 🌧️ 24h 降水: {mgm_curr.get('rain_24h')}mm")
if mgm_curr.get("time"):
# 处理 MGM 原始时间格式 (例如 2026-02-08 13:00)
obs_time = mgm_curr.get("time")
if " " in obs_time:
obs_time = obs_time.split(" ")[1]
msg_lines.append(f" 🕐 观测: {obs_time} (官方)")
if metar:
icao = metar.get("icao", "")
+23
View File
@@ -0,0 +1,23 @@
import requests
import json
import time
def test_mgm(istno):
# 添加时间戳防止缓存
url = f"https://servis.mgm.gov.tr/web/sondurumlar?istno={istno}&_={int(time.time()*1000)}"
headers = {
"Origin": "https://www.mgm.gov.tr",
"User-Agent": "Mozilla/5.0"
}
try:
resp = requests.get(url, headers=headers)
if resp.status_code == 200:
data = resp.json()
print(json.dumps(data, indent=2, ensure_ascii=False))
else:
print(f"Error: {resp.status_code}")
except Exception as e:
print(f"Exception: {e}")
print("--- Station 17128 (Esenboğa) ---")
test_mgm(17128)
+12 -7
View File
@@ -332,24 +332,29 @@ class WeatherDataCollector:
results = {}
try:
# 1. 实时数据
obs_resp = self.session.get(f"{base_url}/sondurumlar?istno={istno}", headers=headers, timeout=self.timeout)
# 1. 实时数据 (添加时间戳防止 CDN 缓存)
import time
obs_resp = self.session.get(
f"{base_url}/sondurumlar?istno={istno}&_={int(time.time()*1000)}",
headers=headers,
timeout=self.timeout
)
if obs_resp.status_code == 200:
data = obs_resp.json()
if data:
latest = data[0] if isinstance(data, list) else data
# MGM 数据字段映射
# ruzgarHiz 通常为 m/s 或 km/h,根据用户反馈这里使用 m/s 逻辑
ruz_hiz = latest.get("ruzgarHiz", 0)
# ruzgarHiz 实测为 km/h,转为 m/s 需要除以 3.6
ruz_hiz_kmh = latest.get("ruzgarHiz", 0)
results["current"] = {
"temp": latest.get("sicaklik"),
"feels_like": latest.get("hissedilenSicaklik") or latest.get("sicaklik"),
"humidity": latest.get("nem"),
"wind_speed_ms": ruz_hiz,
"wind_speed_kt": round(ruz_hiz * 1.94, 1) if ruz_hiz is not None else None, # 如果是 m/s 转 kt
"wind_speed_ms": round(ruz_hiz_kmh / 3.6, 1) if ruz_hiz_kmh is not None else None,
"wind_speed_kt": round(ruz_hiz_kmh / 1.852, 1) if ruz_hiz_kmh is not None else None,
"wind_dir": latest.get("ruzgarYon"),
"rain_24h": latest.get("toplamYagis"),
"time": latest.get("veriZamani"),
"time": latest.get("veriZamani"), # 观测时间
"station_name": latest.get("istasyonAd") or latest.get("adi") or latest.get("merkezAd") or "Ankara Esenboğa"
}