avant 50
This commit is contained in:
@@ -130,45 +130,63 @@ def fetch_current(base_url: str = STATION_URL) -> dict | None:
|
||||
return None
|
||||
|
||||
|
||||
def _parse_noaa_day_line(parts: list[str]) -> dict | None:
|
||||
"""Parse une ligne de données journalières du fichier NOAA WeeWX.
|
||||
|
||||
Format standard : day mean max hh:mm min hh:mm HDD CDD rain wind_avg wind_max hh:mm dir
|
||||
"""
|
||||
if not parts or not parts[0].isdigit():
|
||||
return None
|
||||
# Format complet avec timestamps hh:mm en positions 3 et 5
|
||||
if len(parts) >= 11 and ":" in parts[3] and ":" in parts[5]:
|
||||
return {
|
||||
"temp_ext": _safe_float(parts[1]),
|
||||
"t_max": _safe_float(parts[2]),
|
||||
"t_min": _safe_float(parts[4]),
|
||||
"pluie_mm": _safe_float(parts[8]),
|
||||
"vent_kmh": _to_kmh(_safe_float(parts[10]), "m/s"),
|
||||
}
|
||||
# Fallback générique (anciens formats sans hh:mm)
|
||||
return {
|
||||
"t_max": _safe_float(parts[1]) if len(parts) > 1 else None,
|
||||
"t_min": _safe_float(parts[2]) if len(parts) > 2 else None,
|
||||
"temp_ext": _safe_float(parts[3]) if len(parts) > 3 else None,
|
||||
"pluie_mm": _safe_float(parts[5]) if len(parts) > 5 else None,
|
||||
"vent_kmh": _safe_float(parts[6]) if len(parts) > 6 else None,
|
||||
}
|
||||
|
||||
|
||||
def fetch_month_summaries(year: int, month: int, base_url: str = STATION_URL) -> dict[int, dict]:
|
||||
"""Récupère tous les résumés journaliers d'un mois depuis le fichier NOAA WeeWX.
|
||||
|
||||
Retourne un dict {numéro_jour: data_dict} pour chaque jour disponible du mois.
|
||||
Un seul appel HTTP par mois — utilisé pour le backfill groupé.
|
||||
"""
|
||||
try:
|
||||
url = f"{base_url.rstrip('/')}/NOAA/NOAA-{year:04d}-{month:02d}.txt"
|
||||
r = httpx.get(url, timeout=15)
|
||||
r.raise_for_status()
|
||||
|
||||
result: dict[int, dict] = {}
|
||||
for line in r.text.splitlines():
|
||||
parts = line.split()
|
||||
if not parts or not parts[0].isdigit():
|
||||
continue
|
||||
data = _parse_noaa_day_line(parts)
|
||||
if data:
|
||||
result[int(parts[0])] = data
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Station fetch_month_summaries({year}-{month:02d}) error: {e}")
|
||||
return {}
|
||||
|
||||
|
||||
def fetch_yesterday_summary(base_url: str = STATION_URL) -> dict | None:
|
||||
"""Récupère le résumé de la veille via le fichier NOAA mensuel de la station WeeWX.
|
||||
|
||||
Retourne un dict avec : temp_ext (moy), t_min, t_max, pluie_mm — ou None.
|
||||
"""
|
||||
yesterday = (datetime.now() - timedelta(days=1)).date()
|
||||
year = yesterday.strftime("%Y")
|
||||
month = yesterday.strftime("%m")
|
||||
day = yesterday.day
|
||||
|
||||
try:
|
||||
url = f"{base_url.rstrip('/')}/NOAA/NOAA-{year}-{month}.txt"
|
||||
r = httpx.get(url, timeout=15)
|
||||
r.raise_for_status()
|
||||
|
||||
for line in r.text.splitlines():
|
||||
parts = line.split()
|
||||
if not parts or not parts[0].isdigit() or int(parts[0]) != day:
|
||||
continue
|
||||
|
||||
# Format WeeWX NOAA (fréquent) :
|
||||
# day mean max hh:mm min hh:mm HDD CDD rain wind_avg wind_max hh:mm dir
|
||||
if len(parts) >= 11 and ":" in parts[3] and ":" in parts[5]:
|
||||
return {
|
||||
"temp_ext": _safe_float(parts[1]),
|
||||
"t_max": _safe_float(parts[2]),
|
||||
"t_min": _safe_float(parts[4]),
|
||||
"pluie_mm": _safe_float(parts[8]),
|
||||
"vent_kmh": _to_kmh(_safe_float(parts[10]), "m/s"),
|
||||
}
|
||||
|
||||
# Fallback générique (anciens formats)
|
||||
return {
|
||||
"t_max": _safe_float(parts[1]) if len(parts) > 1 else None,
|
||||
"t_min": _safe_float(parts[2]) if len(parts) > 2 else None,
|
||||
"temp_ext": _safe_float(parts[3]) if len(parts) > 3 else None,
|
||||
"pluie_mm": _safe_float(parts[5]) if len(parts) > 5 else None,
|
||||
"vent_kmh": _safe_float(parts[6]) if len(parts) > 6 else None,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning(f"Station fetch_yesterday_summary error: {e}")
|
||||
return None
|
||||
month_data = fetch_month_summaries(yesterday.year, yesterday.month, base_url)
|
||||
return month_data.get(yesterday.day)
|
||||
|
||||
Reference in New Issue
Block a user