Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
289 lignes
12 KiB
Python
289 lignes
12 KiB
Python
#!/usr/bin/env python3
|
|
"""Collecte WeatherLink v2 (/current) -> JSON public filtre (public/data/meteo.json).
|
|
|
|
Python standard uniquement. Les cles sont lues dans des variables d'environnement (noms dans
|
|
config/stations.json : MC_WL_*_CLE / MC_WL_*_SECRET). Les valeurs ne sont jamais affichees, jamais ecrites.
|
|
|
|
Usage :
|
|
python collecter_weatherlink.py # appels reels (une requete /current par station)
|
|
python collecter_weatherlink.py --exemple # donnees d'exemple, marquees "exemple", aucun appel
|
|
options : --sortie CHEMIN --config CHEMIN --jours-historique 1
|
|
|
|
Filtrage : seule une liste blanche de champs exterieurs sort (voir CHAMPS_PUBLICS). Pas d'IP, pas de chemin,
|
|
pas de temperature/humidite interieure, pas de trame brute, pas d'identifiant de compte ni de station_id.
|
|
Avant d'ecrire, le JSON est controle (verifier_public) ; en cas de motif interdit, rien n'est ecrit.
|
|
|
|
A VERIFIER sur une vraie reponse : les noms exacts des champs (voir CHAMPS_WL) et les unites (imperiales supposees).
|
|
"""
|
|
import argparse
|
|
import datetime as dt
|
|
import json
|
|
import math
|
|
import os
|
|
import re
|
|
import sys
|
|
import tempfile
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
URL_BASE = "https://api.weatherlink.com/v2"
|
|
SOURCE_WL = "WeatherLink (Davis Instruments)"
|
|
UA = "vercors.me/0.1 (meteo-vercors)"
|
|
RACINE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
SORTIE_DEFAUT = os.path.join(RACINE, "public", "data", "meteo.json")
|
|
CONFIG_DEFAUT = os.path.join(RACINE, "config", "stations.json")
|
|
|
|
# Liste blanche des champs d'un releve public (rien d'autre ne sort).
|
|
CHAMPS_PUBLICS = ("dh_utc", "temperature", "humidite", "point_de_rosee", "pression", "vent_moyen",
|
|
"vent_rafales", "vent_direction", "pluie_1h", "pluie_cumul_0h", "temperature_min", "temperature_max")
|
|
|
|
|
|
def _f2c(x): return (x - 32.0) * 5.0 / 9.0
|
|
def _mph(x): return x * 1.609344
|
|
def _inhg(x): return x * 33.8639
|
|
def _inch(x): return x * 25.4
|
|
def _id(x): return x
|
|
|
|
|
|
# Champs WeatherLink exterieurs uniquement ("temp_in", "hum_in" ne figurent volontairement pas ici).
|
|
CHAMPS_WL = {
|
|
"temperature": (("temp", _f2c), ("temp_out", _f2c)),
|
|
"humidite": (("hum", _id), ("hum_out", _id)),
|
|
"point_de_rosee": (("dew_point", _f2c),),
|
|
"pression": (("bar_sea_level", _inhg), ("bar", _inhg)),
|
|
"vent_moyen": (("wind_speed_avg_last_10_min", _mph), ("wind_speed_last", _mph)),
|
|
"vent_rafales": (("wind_speed_hi_last_10_min", _mph),),
|
|
"vent_direction": (("wind_dir_scalar_avg_last_10_min", _id), ("wind_dir_last", _id)),
|
|
"pluie_1h": (("rainfall_last_60_min_in", _inch),),
|
|
"pluie_cumul_0h": (("rainfall_daily_in", _inch),),
|
|
}
|
|
DECIMALES = {"vent_direction": 0, "humidite": 0}
|
|
|
|
# Motifs qui ne doivent JAMAIS apparaitre dans le JSON public.
|
|
MOTIFS_INTERDITS = [
|
|
(r"\b\d{1,3}(?:\.\d{1,3}){3}\b", "adresse IP"),
|
|
(r"/(?:mnt|home|etc|var|root|srv)/", "chemin"),
|
|
(r"[A-Za-z]:\\", "chemin Windows"),
|
|
(r"hdd\d|nvme|weather-db|weather-collector|cornafserver|cornafsuisse|crocserver", "nom interne"),
|
|
(r"api[-_]?key|api[-_]?secret|secret|token|password|mot_de_passe", "mot sensible"),
|
|
(r"trame|_hex|temp_in|hum_in|interieur|MC_WL_", "donnee interne"),
|
|
(r"station_id|account|compte", "identifiant de compte"),
|
|
]
|
|
|
|
|
|
class ErreurWL(Exception):
|
|
pass
|
|
|
|
|
|
def maintenant():
|
|
return dt.datetime.now(dt.timezone.utc).replace(microsecond=0)
|
|
|
|
|
|
def fmt_utc(d):
|
|
return d.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
def arrondir(champ, v):
|
|
return round(v, DECIMALES.get(champ, 1))
|
|
|
|
|
|
def mesure_depuis_current(corps):
|
|
"""Reponse /current (texte JSON) -> releve public {dh_utc, ...} ou ErreurWL. N'utilise que CHAMPS_WL."""
|
|
try:
|
|
rep = json.loads(corps)
|
|
except ValueError:
|
|
raise ErreurWL("reponse non JSON")
|
|
capteurs = rep.get("sensors") if isinstance(rep, dict) else None
|
|
if not isinstance(capteurs, list):
|
|
raise ErreurWL("pas de liste sensors")
|
|
brut, ts_champ = {}, {}
|
|
for cap in capteurs:
|
|
data = cap.get("data") if isinstance(cap, dict) else None
|
|
if not isinstance(data, list) or not data or not isinstance(data[0], dict):
|
|
continue
|
|
rec = data[0]
|
|
for k, v in rec.items():
|
|
if k == "ts" or isinstance(v, bool) or not isinstance(v, (int, float)):
|
|
continue
|
|
if k not in brut:
|
|
brut[k], ts_champ[k] = float(v), rec.get("ts")
|
|
mesure, ts_t = {}, None
|
|
for champ, candidats in CHAMPS_WL.items():
|
|
for nom, conv in candidats:
|
|
if nom in brut:
|
|
mesure[champ] = arrondir(champ, conv(brut[nom]))
|
|
if champ == "temperature":
|
|
ts_t = ts_champ[nom]
|
|
break
|
|
if "temperature" not in mesure:
|
|
raise ErreurWL("pas de temperature exterieure")
|
|
if "vent_direction" in mesure:
|
|
mesure["vent_direction"] = mesure["vent_direction"] % 360
|
|
if not isinstance(ts_t, (int, float)):
|
|
ts_t = rep.get("generated_at")
|
|
if not isinstance(ts_t, (int, float)):
|
|
raise ErreurWL("pas d'horodatage")
|
|
mesure["dh_utc"] = fmt_utc(dt.datetime.fromtimestamp(ts_t, dt.timezone.utc))
|
|
return mesure
|
|
|
|
|
|
def appeler_current(station_id, cle, secret):
|
|
"""Une requete /current. Retourne le corps (texte). La cle n'apparait dans aucun message d'erreur."""
|
|
url = "%s/current/%s?api-key=%s" % (URL_BASE, urllib.parse.quote(str(station_id), safe=""),
|
|
urllib.parse.quote(cle, safe=""))
|
|
req = urllib.request.Request(url, headers={"X-Api-Secret": secret, "User-Agent": UA, "Accept": "application/json"})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=20) as r:
|
|
return r.read().decode("utf-8", errors="replace")
|
|
except urllib.error.HTTPError as e:
|
|
raise ErreurWL("HTTP %d" % e.code)
|
|
except Exception as e:
|
|
raise ErreurWL("reseau : %s" % type(e).__name__)
|
|
|
|
|
|
def verifier_public(obj):
|
|
"""Leve ValueError si le JSON public contient un motif interdit."""
|
|
texte = json.dumps(obj, ensure_ascii=False)
|
|
for motif, nom in MOTIFS_INTERDITS:
|
|
m = re.search(motif, texte, re.I)
|
|
if m:
|
|
raise ValueError("motif interdit dans le JSON public (%s)" % nom)
|
|
for st in obj.get("stations", {}).values():
|
|
for r in st.get("releves", []):
|
|
extra = set(r) - set(CHAMPS_PUBLICS)
|
|
if extra:
|
|
raise ValueError("champ non autorise : %s" % sorted(extra))
|
|
|
|
|
|
def fusionner(anciens, nouveau, jours):
|
|
"""Ajoute `nouveau` (ou rien) a l'historique, sans doublon, borne a `jours` jours."""
|
|
par_date = {r["dh_utc"]: {k: r[k] for k in CHAMPS_PUBLICS if k in r} for r in anciens if "dh_utc" in r}
|
|
if nouveau:
|
|
par_date[nouveau["dh_utc"]] = nouveau
|
|
limite = fmt_utc(maintenant() - dt.timedelta(days=jours))
|
|
return [par_date[d] for d in sorted(par_date) if d >= limite]
|
|
|
|
|
|
def min_max_jour(releves):
|
|
"""Min/max de temperature sur les 24 dernieres heures du JSON (recalcules, pas fournis par l'API)."""
|
|
ts = [r["temperature"] for r in releves if "temperature" in r]
|
|
return (min(ts), max(ts)) if ts else (None, None)
|
|
|
|
|
|
def charger_json(chemin):
|
|
try:
|
|
with open(chemin, encoding="utf-8") as f:
|
|
return json.load(f)
|
|
except (OSError, ValueError):
|
|
return {}
|
|
|
|
|
|
def ecrire_atomique(chemin, obj):
|
|
os.makedirs(os.path.dirname(chemin), exist_ok=True)
|
|
fd, tmp = tempfile.mkstemp(dir=os.path.dirname(chemin), suffix=".tmp")
|
|
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
json.dump(obj, f, ensure_ascii=False, separators=(",", ":"))
|
|
os.replace(tmp, chemin)
|
|
|
|
|
|
def exemple_releves(graine, base_t, fin, heures=24, pas_min=10, vent=True):
|
|
"""Serie synthetique deterministe (pas de hasard) ; marquee 'exemple' par l'appelant."""
|
|
out = []
|
|
n = heures * 60 // pas_min
|
|
for i in range(n + 1):
|
|
t = fin - dt.timedelta(minutes=pas_min * (n - i))
|
|
h = (t.hour + t.minute / 60.0)
|
|
cyc = math.sin((h - 9) / 24.0 * 2 * math.pi)
|
|
r = {"dh_utc": fmt_utc(t),
|
|
"temperature": round(base_t + 4.0 * cyc + 0.3 * math.sin(i / 3.0 + graine), 1),
|
|
"humidite": int(70 - 15 * cyc + 3 * math.sin(i / 5.0 + graine)),
|
|
"pression": round(1018 + 2.0 * math.sin(i / 40.0 + graine), 1),
|
|
"pluie_cumul_0h": 0.0, "pluie_1h": 0.0}
|
|
r["point_de_rosee"] = round(r["temperature"] - (100 - r["humidite"]) / 5.0, 1)
|
|
if vent:
|
|
r["vent_moyen"] = round(6 + 4 * abs(math.sin(i / 7.0 + graine)), 1)
|
|
r["vent_rafales"] = round(r["vent_moyen"] * 1.6, 1)
|
|
r["vent_direction"] = int((200 + 40 * math.sin(i / 11.0 + graine)) % 360)
|
|
out.append(r)
|
|
return out
|
|
|
|
|
|
def construire_exemple(config):
|
|
fin = maintenant()
|
|
stations, meta = {}, {}
|
|
plan = {"bruyeres": (3.0, 1, 0), "meaudre": (3.5, 2, 0), "geve": (0.5, 3, 0), "allieres": (-0.5, 4, 0), "vp2-lans": (4.0, 5, 8)}
|
|
for compte in config["comptes"].values():
|
|
for sid, c in compte["stations"].items():
|
|
base, graine, retard_h = plan.get(sid, (2.0, 9, 0))
|
|
vp2 = sid == "vp2-lans"
|
|
rel = exemple_releves(graine, base, fin - dt.timedelta(hours=retard_h), vent=not vp2)
|
|
mn, mx = min_max_jour(rel)
|
|
rel[-1]["temperature_min"], rel[-1]["temperature_max"] = mn, mx
|
|
stations[sid] = {"nom": c["nom"], "source": SOURCE_WL, "exemple": True, "hors_ligne": bool(retard_h), "releves": rel}
|
|
meta[sid] = meta_station(c)
|
|
return {"source": "Donnees d'exemple (pas de mesure reelle)", "exemple": True,
|
|
"mis_a_jour_utc": fin.strftime("%Y-%m-%dT%H:%M:%SZ"), "metadonnees": meta, "stations": stations}
|
|
|
|
|
|
def meta_station(c):
|
|
m = {"lat": round(c["lat"], 2), "lon": round(c["lon"], 2), "licence": "donnees de l'exploitant de la station"}
|
|
if c.get("altitude") is not None:
|
|
m["altitude"] = c["altitude"]
|
|
return m
|
|
|
|
|
|
def collecter(config, ancien, jours):
|
|
"""Appels reels. Une station sans cle / sans station_id / en erreur : hors ligne, historique conserve."""
|
|
fin = maintenant()
|
|
stations, meta, journal = {}, {}, []
|
|
anciennes = ancien.get("stations", {}) if not ancien.get("exemple") else {}
|
|
for nom_compte, compte in config["comptes"].items():
|
|
cle = os.environ.get(compte["var_cle"], "")
|
|
secret = os.environ.get(compte["var_secret"], "")
|
|
for sid, c in compte["stations"].items():
|
|
meta[sid] = meta_station(c)
|
|
hist = anciennes.get(sid, {}).get("releves", [])
|
|
nouveau, etat = None, "ok"
|
|
if not cle or not secret:
|
|
etat = "sans_cle"
|
|
elif not c.get("station_id"):
|
|
etat = "sans_station_id"
|
|
else:
|
|
try:
|
|
nouveau = mesure_depuis_current(appeler_current(c["station_id"], cle, secret))
|
|
except ErreurWL as e:
|
|
etat = "erreur (%s)" % e
|
|
rel = fusionner(hist, nouveau, jours)
|
|
if rel and nouveau:
|
|
rel[-1]["temperature_min"], rel[-1]["temperature_max"] = min_max_jour(rel)
|
|
stations[sid] = {"nom": c["nom"], "source": SOURCE_WL, "hors_ligne": nouveau is None, "releves": rel}
|
|
journal.append("%s: %s" % (sid, etat))
|
|
obj = {"source": "WeatherLink (Davis Instruments)", "mis_a_jour_utc": fin.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
|
"metadonnees": meta, "stations": stations}
|
|
return obj, journal
|
|
|
|
|
|
def main(argv=None):
|
|
ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
|
|
ap.add_argument("--exemple", action="store_true")
|
|
ap.add_argument("--sortie", default=SORTIE_DEFAUT)
|
|
ap.add_argument("--config", default=CONFIG_DEFAUT)
|
|
ap.add_argument("--jours-historique", type=float, default=1.0)
|
|
a = ap.parse_args(argv)
|
|
with open(a.config, encoding="utf-8") as f:
|
|
config = json.load(f)
|
|
if a.exemple:
|
|
obj, journal = construire_exemple(config), ["mode exemple"]
|
|
else:
|
|
obj, journal = collecter(config, charger_json(a.sortie), a.jours_historique)
|
|
verifier_public(obj)
|
|
ecrire_atomique(a.sortie, obj)
|
|
print("ecrit : %s (%d stations)" % (os.path.basename(a.sortie), len(obj["stations"])))
|
|
print("; ".join(journal))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|