Backup automatico script del 2026-01-11 07:00
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import html
|
||||
import json
|
||||
@@ -36,6 +37,8 @@ SOGLIA_PIOGGIA_3H_MM = 30.0 # mm in 3 ore (rolling)
|
||||
PERSIST_HOURS = 2 # persistenza minima (ore)
|
||||
HOURS_AHEAD = 24
|
||||
SNOW_HOURLY_EPS_CM = 0.2 # Soglia minima neve cm/h
|
||||
# Codici meteo che indicano neve (WMO)
|
||||
SNOW_WEATHER_CODES = [71, 73, 75, 77, 85, 86] # Neve leggera, moderata, forte, granelli, rovesci
|
||||
|
||||
# File di stato
|
||||
STATE_FILE = "/home/daniely/docker/telegram-bot/student_state.json"
|
||||
@@ -53,10 +56,12 @@ POINTS = [
|
||||
|
||||
# ----------------- OPEN-METEO -----------------
|
||||
OPEN_METEO_URL = "https://api.open-meteo.com/v1/forecast"
|
||||
TZ = "Europe/Rome"
|
||||
TZ = "Europe/Berlin"
|
||||
TZINFO = ZoneInfo(TZ)
|
||||
HTTP_HEADERS = {"User-Agent": "rpi-student-alert/1.0"}
|
||||
MODEL = "meteofrance_arome_france_hd"
|
||||
HTTP_HEADERS = {"User-Agent": "rpi-student-alert/2.0"}
|
||||
MODEL_AROME = "meteofrance_seamless"
|
||||
MODEL_ICON_IT = "italia_meteo_arpae_icon_2i"
|
||||
COMPARISON_THRESHOLD = 0.30 # 30% scostamento per comparazione
|
||||
|
||||
# ----------------- LOG -----------------
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
@@ -132,12 +137,20 @@ def hhmm(dt: datetime.datetime) -> str:
|
||||
# =============================================================================
|
||||
# Telegram
|
||||
# =============================================================================
|
||||
def telegram_send_html(message_html: str) -> bool:
|
||||
def telegram_send_html(message_html: str, chat_ids: Optional[List[str]] = None) -> bool:
|
||||
"""
|
||||
Args:
|
||||
message_html: Messaggio HTML da inviare
|
||||
chat_ids: Lista di chat IDs (default: TELEGRAM_CHAT_IDS)
|
||||
"""
|
||||
token = load_bot_token()
|
||||
if not token:
|
||||
LOGGER.warning("Telegram token missing: message not sent.")
|
||||
return False
|
||||
|
||||
if chat_ids is None:
|
||||
chat_ids = TELEGRAM_CHAT_IDS
|
||||
|
||||
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
||||
base_payload = {
|
||||
"text": message_html,
|
||||
@@ -147,7 +160,7 @@ def telegram_send_html(message_html: str) -> bool:
|
||||
|
||||
sent_ok = False
|
||||
with requests.Session() as s:
|
||||
for chat_id in TELEGRAM_CHAT_IDS:
|
||||
for chat_id in chat_ids:
|
||||
payload = dict(base_payload)
|
||||
payload["chat_id"] = chat_id
|
||||
try:
|
||||
@@ -193,24 +206,130 @@ def save_state(alert_active: bool, signature: str) -> None:
|
||||
LOGGER.exception("State write error: %s", e)
|
||||
|
||||
|
||||
def get_forecast(session: requests.Session, lat: float, lon: float) -> Optional[Dict]:
|
||||
def get_forecast(session: requests.Session, lat: float, lon: float, model: str) -> Optional[Dict]:
|
||||
params = {
|
||||
"latitude": lat,
|
||||
"longitude": lon,
|
||||
"hourly": "precipitation,snowfall",
|
||||
"hourly": "precipitation,snowfall,weathercode", # Aggiunto weathercode per rilevare neve
|
||||
"timezone": TZ,
|
||||
"forecast_days": 2,
|
||||
"precipitation_unit": "mm",
|
||||
"models": MODEL,
|
||||
"models": model,
|
||||
}
|
||||
|
||||
# Aggiungi minutely_15 per AROME Seamless (dettaglio 15 minuti per inizio preciso eventi)
|
||||
# Se fallisce o ha buchi, riprova senza minutely_15
|
||||
if model == MODEL_AROME:
|
||||
params["minutely_15"] = "precipitation,rain,snowfall,precipitation_probability,temperature_2m"
|
||||
try:
|
||||
r = session.get(OPEN_METEO_URL, params=params, headers=HTTP_HEADERS, timeout=25)
|
||||
if r.status_code == 400:
|
||||
# Se 400 e abbiamo minutely_15, riprova senza
|
||||
if "minutely_15" in params and model == MODEL_AROME:
|
||||
LOGGER.warning("Open-Meteo 400 con minutely_15 (model=%s), riprovo senza minutely_15", model)
|
||||
params_no_minutely = params.copy()
|
||||
del params_no_minutely["minutely_15"]
|
||||
try:
|
||||
r2 = session.get(OPEN_METEO_URL, params=params_no_minutely, headers=HTTP_HEADERS, timeout=25)
|
||||
if r2.status_code == 200:
|
||||
return r2.json()
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
elif r.status_code == 504:
|
||||
# Gateway Timeout: se abbiamo minutely_15, riprova senza
|
||||
if "minutely_15" in params and model == MODEL_AROME:
|
||||
LOGGER.warning("Open-Meteo 504 Gateway Timeout con minutely_15 (model=%s), riprovo senza minutely_15", model)
|
||||
params_no_minutely = params.copy()
|
||||
del params_no_minutely["minutely_15"]
|
||||
try:
|
||||
r2 = session.get(OPEN_METEO_URL, params=params_no_minutely, headers=HTTP_HEADERS, timeout=25)
|
||||
if r2.status_code == 200:
|
||||
return r2.json()
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
data = r.json()
|
||||
|
||||
# Verifica se minutely_15 ha buchi (anche solo 1 None = fallback a hourly)
|
||||
if "minutely_15" in params and model == MODEL_AROME:
|
||||
minutely = data.get("minutely_15", {}) or {}
|
||||
minutely_times = minutely.get("time", []) or []
|
||||
minutely_precip = minutely.get("precipitation", []) or []
|
||||
minutely_snow = minutely.get("snowfall", []) or []
|
||||
|
||||
# Controlla se ci sono buchi (anche solo 1 None)
|
||||
if minutely_times:
|
||||
# Controlla tutti i parametri principali per buchi
|
||||
has_holes = False
|
||||
# Controlla precipitation
|
||||
if minutely_precip and any(v is None for v in minutely_precip):
|
||||
has_holes = True
|
||||
# Controlla snowfall
|
||||
if minutely_snow and any(v is None for v in minutely_snow):
|
||||
has_holes = True
|
||||
|
||||
if has_holes:
|
||||
LOGGER.warning("minutely_15 ha buchi (valori None rilevati, model=%s), riprovo senza minutely_15", model)
|
||||
params_no_minutely = params.copy()
|
||||
del params_no_minutely["minutely_15"]
|
||||
try:
|
||||
r2 = session.get(OPEN_METEO_URL, params=params_no_minutely, headers=HTTP_HEADERS, timeout=25)
|
||||
if r2.status_code == 200:
|
||||
return r2.json()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return data
|
||||
except requests.exceptions.Timeout:
|
||||
# Timeout: se abbiamo minutely_15, riprova senza
|
||||
if "minutely_15" in params and model == MODEL_AROME:
|
||||
LOGGER.warning("Open-Meteo Timeout con minutely_15 (model=%s), riprovo senza minutely_15", model)
|
||||
params_no_minutely = params.copy()
|
||||
del params_no_minutely["minutely_15"]
|
||||
try:
|
||||
r2 = session.get(OPEN_METEO_URL, params=params_no_minutely, headers=HTTP_HEADERS, timeout=25)
|
||||
if r2.status_code == 200:
|
||||
return r2.json()
|
||||
except Exception:
|
||||
pass
|
||||
LOGGER.exception("Open-Meteo timeout (model=%s)", model)
|
||||
return None
|
||||
except Exception as e:
|
||||
LOGGER.exception("Open-Meteo request error: %s", e)
|
||||
# Altri errori: se abbiamo minutely_15, riprova senza
|
||||
if "minutely_15" in params and model == MODEL_AROME:
|
||||
LOGGER.warning("Open-Meteo error con minutely_15 (model=%s): %s, riprovo senza minutely_15", model, str(e))
|
||||
params_no_minutely = params.copy()
|
||||
del params_no_minutely["minutely_15"]
|
||||
try:
|
||||
r2 = session.get(OPEN_METEO_URL, params=params_no_minutely, headers=HTTP_HEADERS, timeout=25)
|
||||
if r2.status_code == 200:
|
||||
return r2.json()
|
||||
except Exception:
|
||||
pass
|
||||
LOGGER.exception("Open-Meteo request error (model=%s): %s", model, e)
|
||||
return None
|
||||
|
||||
|
||||
def compare_values(arome_val: float, icon_val: float) -> Optional[Dict]:
|
||||
"""Confronta due valori e ritorna info se scostamento >30%"""
|
||||
if arome_val == 0 and icon_val == 0:
|
||||
return None
|
||||
|
||||
if arome_val > 0:
|
||||
diff_pct = abs(icon_val - arome_val) / arome_val
|
||||
elif icon_val > 0:
|
||||
diff_pct = abs(arome_val - icon_val) / icon_val
|
||||
else:
|
||||
return None
|
||||
|
||||
if diff_pct > COMPARISON_THRESHOLD:
|
||||
return {
|
||||
"diff_pct": diff_pct * 100,
|
||||
"arome": arome_val,
|
||||
"icon": icon_val
|
||||
}
|
||||
return None
|
||||
|
||||
|
||||
@@ -272,6 +391,7 @@ def compute_stats(data: Dict) -> Optional[Dict]:
|
||||
times = hourly.get("time", []) or []
|
||||
precip = hourly.get("precipitation", []) or []
|
||||
snow = hourly.get("snowfall", []) or []
|
||||
weathercode = hourly.get("weathercode", []) or [] # Per rilevare neve anche quando snowfall è basso
|
||||
|
||||
n = min(len(times), len(precip), len(snow))
|
||||
if n == 0: return None
|
||||
@@ -289,7 +409,8 @@ def compute_stats(data: Dict) -> Optional[Dict]:
|
||||
|
||||
times_w = times[start_idx:end_idx]
|
||||
precip_w = precip[start_idx:end_idx]
|
||||
snow_w = snow[start_idx:end_idx]
|
||||
snow_w = [float(x) if x is not None else 0.0 for x in snow[start_idx:end_idx]]
|
||||
weathercode_w = [int(x) if x is not None else None for x in weathercode[start_idx:end_idx]] if len(weathercode) > start_idx else []
|
||||
dt_w = [parse_time_to_local(t) for t in times_w]
|
||||
|
||||
rain3 = rolling_sum_3h(precip_w)
|
||||
@@ -302,11 +423,98 @@ def compute_stats(data: Dict) -> Optional[Dict]:
|
||||
)
|
||||
rain_persist_time = hhmm(dt_w[rain_run_start]) if (rain_persist_ok and rain_run_start < len(dt_w)) else ""
|
||||
|
||||
# Analizza evento pioggia completa (48h): rileva inizio e calcola durata e accumulo totale
|
||||
rain_start_idx = None
|
||||
rain_end_idx = None
|
||||
total_rain_accumulation = 0.0
|
||||
rain_duration_hours = 0.0
|
||||
max_rain_intensity = 0.0
|
||||
|
||||
# Codici meteo che indicano pioggia (WMO)
|
||||
RAIN_WEATHER_CODES = [61, 63, 65, 66, 67, 80, 81, 82]
|
||||
|
||||
# Trova inizio evento pioggia (prima occorrenza con precipitation > 0 OPPURE weathercode pioggia)
|
||||
# Estendi l'analisi a 48 ore se disponibile
|
||||
extended_end_idx = min(start_idx + 48, n) # Estendi a 48 ore
|
||||
precip_extended = precip[start_idx:extended_end_idx]
|
||||
weathercode_extended = [int(x) if x is not None else None for x in weathercode[start_idx:extended_end_idx]] if len(weathercode) > start_idx else []
|
||||
|
||||
for i, (p_val, code) in enumerate(zip(precip_extended, weathercode_extended if len(weathercode_extended) == len(precip_extended) else [None] * len(precip_extended))):
|
||||
p_val_float = float(p_val) if p_val is not None else 0.0
|
||||
is_rain = (p_val_float > 0.0) or (code is not None and code in RAIN_WEATHER_CODES)
|
||||
if is_rain and rain_start_idx is None:
|
||||
rain_start_idx = i
|
||||
break
|
||||
|
||||
# Se trovato inizio, calcola durata e accumulo totale su 48 ore
|
||||
if rain_start_idx is not None:
|
||||
# Trova fine evento pioggia (ultima occorrenza con pioggia)
|
||||
for i in range(len(precip_extended) - 1, rain_start_idx - 1, -1):
|
||||
p_val = precip_extended[i] if i < len(precip_extended) else None
|
||||
code = weathercode_extended[i] if i < len(weathercode_extended) else None
|
||||
p_val_float = float(p_val) if p_val is not None else 0.0
|
||||
is_rain = (p_val_float > 0.0) or (code is not None and code in RAIN_WEATHER_CODES)
|
||||
if is_rain:
|
||||
rain_end_idx = i
|
||||
break
|
||||
|
||||
if rain_end_idx is not None:
|
||||
# Calcola durata
|
||||
times_extended = times[start_idx:extended_end_idx]
|
||||
dt_extended = [parse_time_to_local(t) for t in times_extended]
|
||||
if rain_end_idx < len(dt_extended) and rain_start_idx < len(dt_extended):
|
||||
rain_duration_hours = (dt_extended[rain_end_idx] - dt_extended[rain_start_idx]).total_seconds() / 3600.0
|
||||
# Calcola accumulo totale (somma di tutti i precipitation > 0)
|
||||
total_rain_accumulation = sum(float(p) for p in precip_extended[rain_start_idx:rain_end_idx+1] if p is not None and float(p) > 0.0)
|
||||
# Calcola intensità massima oraria
|
||||
max_rain_intensity = max((float(p) for p in precip_extended[rain_start_idx:rain_end_idx+1] if p is not None), default=0.0)
|
||||
|
||||
# Analizza nevicata completa (48h): rileva inizio usando snowfall > 0 OPPURE weathercode
|
||||
# Calcola durata e accumulo totale
|
||||
snow_start_idx = None
|
||||
snow_end_idx = None
|
||||
total_snow_accumulation = 0.0
|
||||
snow_duration_hours = 0.0
|
||||
|
||||
# Trova inizio nevicata (prima occorrenza con snowfall > 0 OPPURE weathercode neve)
|
||||
for i, (s_val, code) in enumerate(zip(snow_w, weathercode_w if len(weathercode_w) == len(snow_w) else [None] * len(snow_w))):
|
||||
is_snow = (s_val > 0.0) or (code is not None and code in SNOW_WEATHER_CODES)
|
||||
if is_snow and snow_start_idx is None:
|
||||
snow_start_idx = i
|
||||
break
|
||||
|
||||
# Se trovato inizio, calcola durata e accumulo totale
|
||||
if snow_start_idx is not None:
|
||||
# Trova fine nevicata (ultima occorrenza con neve)
|
||||
for i in range(len(snow_w) - 1, snow_start_idx - 1, -1):
|
||||
s_val = snow_w[i]
|
||||
code = weathercode_w[i] if i < len(weathercode_w) else None
|
||||
is_snow = (s_val > 0.0) or (code is not None and code in SNOW_WEATHER_CODES)
|
||||
if is_snow:
|
||||
snow_end_idx = i
|
||||
break
|
||||
|
||||
if snow_end_idx is not None:
|
||||
# Calcola durata
|
||||
snow_duration_hours = (dt_w[snow_end_idx] - dt_w[snow_start_idx]).total_seconds() / 3600.0
|
||||
# Calcola accumulo totale (somma di tutti i snowfall > 0)
|
||||
total_snow_accumulation = sum(s for s in snow_w[snow_start_idx:snow_end_idx+1] if s > 0.0)
|
||||
|
||||
# Per compatibilità con logica esistente
|
||||
snow_run_len, snow_run_start = max_consecutive_gt(snow_w, eps=SNOW_HOURLY_EPS_CM)
|
||||
snow_run_time = hhmm(dt_w[snow_run_start]) if (snow_run_start >= 0 and snow_run_start < len(dt_w)) else ""
|
||||
|
||||
snow_12h = sum(float(x) for x in snow_w[:min(12, len(snow_w))] if x is not None)
|
||||
snow_24h = sum(float(x) for x in snow_w[:min(24, len(snow_w))] if x is not None)
|
||||
# Se trovato inizio nevicata, usa quello invece del run
|
||||
if snow_start_idx is not None:
|
||||
snow_run_time = hhmm(dt_w[snow_start_idx])
|
||||
# Durata minima per alert: almeno 2 ore
|
||||
if snow_duration_hours >= PERSIST_HOURS:
|
||||
snow_run_len = int(snow_duration_hours)
|
||||
else:
|
||||
snow_run_len = 0 # Durata troppo breve
|
||||
|
||||
snow_12h = sum(s for s in snow_w[:min(12, len(snow_w))] if s > 0.0)
|
||||
snow_24h = sum(s for s in snow_w[:min(24, len(snow_w))] if s > 0.0)
|
||||
|
||||
return {
|
||||
"rain3_max": float(rain3_max),
|
||||
@@ -315,10 +523,15 @@ def compute_stats(data: Dict) -> Optional[Dict]:
|
||||
"rain_persist_time": rain_persist_time,
|
||||
"rain_persist_run_max": float(rain_run_max),
|
||||
"rain_persist_run_len": int(rain_run_len),
|
||||
"rain_duration_hours": float(rain_duration_hours),
|
||||
"total_rain_accumulation_mm": float(total_rain_accumulation),
|
||||
"max_rain_intensity_mm_h": float(max_rain_intensity),
|
||||
"snow_run_len": int(snow_run_len),
|
||||
"snow_run_time": snow_run_time,
|
||||
"snow_12h": float(snow_12h),
|
||||
"snow_24h": float(snow_24h),
|
||||
"snow_duration_hours": float(snow_duration_hours),
|
||||
"total_snow_accumulation_cm": float(total_snow_accumulation),
|
||||
}
|
||||
|
||||
|
||||
@@ -338,6 +551,9 @@ def point_alerts(point_name: str, stats: Dict) -> Dict:
|
||||
"rain_persist_time": stats["rain_persist_time"],
|
||||
"rain_persist_run_max": stats["rain_persist_run_max"],
|
||||
"rain_persist_run_len": stats["rain_persist_run_len"],
|
||||
"rain_duration_hours": stats.get("rain_duration_hours", 0.0),
|
||||
"total_rain_accumulation_mm": stats.get("total_rain_accumulation_mm", 0.0),
|
||||
"max_rain_intensity_mm_h": stats.get("max_rain_intensity_mm_h", 0.0),
|
||||
}
|
||||
|
||||
|
||||
@@ -354,32 +570,54 @@ def build_signature(bologna: Dict, route: List[Dict]) -> str:
|
||||
return "|".join(parts)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
def main(chat_ids: Optional[List[str]] = None, debug_mode: bool = False) -> None:
|
||||
LOGGER.info("--- Student alert Bologna (Neve/Pioggia intensa) ---")
|
||||
|
||||
state = load_state()
|
||||
was_active = bool(state.get("alert_active", False))
|
||||
last_sig = state.get("signature", "")
|
||||
|
||||
comparisons: Dict[str, Dict] = {} # point_name -> comparison info
|
||||
|
||||
with requests.Session() as session:
|
||||
# Trigger: Bologna
|
||||
bo = POINTS[0]
|
||||
bo_data = get_forecast(session, bo["lat"], bo["lon"])
|
||||
if not bo_data: return
|
||||
bo_stats = compute_stats(bo_data)
|
||||
if not bo_stats:
|
||||
bo_data_arome = get_forecast(session, bo["lat"], bo["lon"], MODEL_AROME)
|
||||
if not bo_data_arome: return
|
||||
bo_stats_arome = compute_stats(bo_data_arome)
|
||||
if not bo_stats_arome:
|
||||
LOGGER.error("Impossibile calcolare statistiche Bologna.")
|
||||
return
|
||||
bo_alerts = point_alerts(bo["name"], bo_stats)
|
||||
bo_alerts = point_alerts(bo["name"], bo_stats_arome)
|
||||
|
||||
# Recupera ICON Italia per Bologna
|
||||
bo_data_icon = get_forecast(session, bo["lat"], bo["lon"], MODEL_ICON_IT)
|
||||
if bo_data_icon:
|
||||
bo_stats_icon = compute_stats(bo_data_icon)
|
||||
if bo_stats_icon:
|
||||
comp_snow = compare_values(bo_stats_arome["snow_24h"], bo_stats_icon["snow_24h"])
|
||||
comp_rain = compare_values(bo_stats_arome["rain3_max"], bo_stats_icon["rain3_max"])
|
||||
if comp_snow or comp_rain:
|
||||
comparisons[bo["name"]] = {"snow": comp_snow, "rain": comp_rain, "icon_stats": bo_stats_icon}
|
||||
|
||||
# Route points
|
||||
route_alerts: List[Dict] = []
|
||||
for p in POINTS[1:]:
|
||||
d = get_forecast(session, p["lat"], p["lon"])
|
||||
if not d: continue
|
||||
st = compute_stats(d)
|
||||
if not st: continue
|
||||
route_alerts.append(point_alerts(p["name"], st))
|
||||
d_arome = get_forecast(session, p["lat"], p["lon"], MODEL_AROME)
|
||||
if not d_arome: continue
|
||||
st_arome = compute_stats(d_arome)
|
||||
if not st_arome: continue
|
||||
route_alerts.append(point_alerts(p["name"], st_arome))
|
||||
|
||||
# Recupera ICON Italia per punto
|
||||
d_icon = get_forecast(session, p["lat"], p["lon"], MODEL_ICON_IT)
|
||||
if d_icon:
|
||||
st_icon = compute_stats(d_icon)
|
||||
if st_icon:
|
||||
comp_snow = compare_values(st_arome["snow_24h"], st_icon["snow_24h"])
|
||||
comp_rain = compare_values(st_arome["rain3_max"], st_icon["rain3_max"])
|
||||
if comp_snow or comp_rain:
|
||||
comparisons[p["name"]] = {"snow": comp_snow, "rain": comp_rain, "icon_stats": st_icon}
|
||||
|
||||
any_route_alert = any(x["snow_alert"] or x["rain_alert"] for x in route_alerts)
|
||||
any_alert = (bo_alerts["snow_alert"] or bo_alerts["rain_alert"] or any_route_alert)
|
||||
@@ -388,7 +626,10 @@ def main() -> None:
|
||||
|
||||
# --- Scenario A: Allerta ---
|
||||
if any_alert:
|
||||
if (not was_active) or (sig != last_sig):
|
||||
# In modalità debug, bypassa controlli anti-spam
|
||||
if debug_mode:
|
||||
LOGGER.info("[DEBUG MODE] Bypass anti-spam: invio forzato")
|
||||
if debug_mode or (not was_active) or (sig != last_sig):
|
||||
now_str = now_local().strftime("%H:%M")
|
||||
header_icon = "❄️" if (bo_alerts["snow_alert"] or any(x["snow_alert"] for x in route_alerts)) \
|
||||
else "🌧️" if (bo_alerts["rain_alert"] or any(x["rain_alert"] for x in route_alerts)) \
|
||||
@@ -397,20 +638,38 @@ def main() -> None:
|
||||
msg: List[str] = []
|
||||
msg.append(f"{header_icon} <b>ALLERTA METEO (Bologna / Rientro)</b>")
|
||||
msg.append(f"🕒 <i>Aggiornamento ore {html.escape(now_str)}</i>")
|
||||
msg.append(f"🛰️ <code>Modello: {html.escape(MODEL)}</code>")
|
||||
model_info = MODEL_AROME
|
||||
if comparisons:
|
||||
model_info = f"{MODEL_AROME} + ICON Italia (discordanza rilevata)"
|
||||
msg.append(f"🛰️ <code>Modello: {html.escape(model_info)}</code>")
|
||||
msg.append(f"⏱️ <code>Finestra: {HOURS_AHEAD} ore | Persistenza: {PERSIST_HOURS} ore</code>")
|
||||
msg.append("")
|
||||
|
||||
# Bologna
|
||||
msg.append("🎓 <b>A BOLOGNA</b>")
|
||||
bo_comp = comparisons.get(bo["name"])
|
||||
if bo_alerts["snow_alert"]:
|
||||
msg.append(f"❄️ Neve (≥{PERSIST_HOURS}h) da ~<b>{html.escape(bo_alerts['snow_run_time'] or '—')}</b> (run ~{bo_alerts['snow_run_len']}h).")
|
||||
msg.append(f"• Accumulo: 12h <b>{bo_alerts['snow_12h']:.1f} cm</b> | 24h <b>{bo_alerts['snow_24h']:.1f} cm</b>")
|
||||
if bo_comp and bo_comp.get("snow"):
|
||||
comp = bo_comp["snow"]
|
||||
icon_s24 = bo_comp["icon_stats"]["snow_24h"]
|
||||
msg.append(f"⚠️ <b>Discordanza modelli</b>: AROME {comp['arome']:.1f} cm | ICON {icon_s24:.1f} cm (scostamento {comp['diff_pct']:.0f}%)")
|
||||
else:
|
||||
msg.append(f"❄️ Neve: nessuna persistenza ≥ {PERSIST_HOURS}h (24h {bo_alerts['snow_24h']:.1f} cm).")
|
||||
|
||||
if bo_alerts["rain_alert"]:
|
||||
rain_duration = bo_alerts.get("rain_duration_hours", 0.0)
|
||||
total_rain = bo_alerts.get("total_rain_accumulation_mm", 0.0)
|
||||
max_intensity = bo_alerts.get("max_rain_intensity_mm_h", 0.0)
|
||||
|
||||
msg.append(f"🌧️ Pioggia molto forte (3h ≥ {SOGLIA_PIOGGIA_3H_MM:.0f} mm, ≥{PERSIST_HOURS}h) da ~<b>{html.escape(bo_alerts['rain_persist_time'] or '—')}</b>.")
|
||||
if rain_duration > 0:
|
||||
msg.append(f"⏱️ <b>Durata totale evento (48h):</b> ~{rain_duration:.0f} ore | <b>Accumulo totale:</b> ~{total_rain:.1f} mm | <b>Intensità max:</b> {max_intensity:.1f} mm/h")
|
||||
if bo_comp and bo_comp.get("rain"):
|
||||
comp = bo_comp["rain"]
|
||||
icon_r3 = bo_comp["icon_stats"]["rain3_max"]
|
||||
msg.append(f"⚠️ <b>Discordanza modelli</b>: AROME {comp['arome']:.1f} mm | ICON {icon_r3:.1f} mm (scostamento {comp['diff_pct']:.0f}%)")
|
||||
else:
|
||||
msg.append(f"🌧️ Pioggia: max 3h <b>{bo_alerts['rain3_max']:.1f} mm</b> (picco ~{html.escape(bo_alerts['rain3_max_time'] or '—')}).")
|
||||
|
||||
@@ -427,15 +686,35 @@ def main() -> None:
|
||||
if x["snow_alert"]:
|
||||
parts.append(f"❄️ neve (≥{PERSIST_HOURS}h) da ~{html.escape(x['snow_run_time'] or '—')} (24h {x['snow_24h']:.1f} cm)")
|
||||
if x["rain_alert"]:
|
||||
rain_dur = x.get("rain_duration_hours", 0.0)
|
||||
rain_tot = x.get("total_rain_accumulation_mm", 0.0)
|
||||
if rain_dur > 0:
|
||||
parts.append(f"🌧️ pioggia forte da ~{html.escape(x['rain_persist_time'] or '—')} (durata ~{rain_dur:.0f}h, totale ~{rain_tot:.1f}mm)")
|
||||
else:
|
||||
parts.append(f"🌧️ pioggia forte da ~{html.escape(x['rain_persist_time'] or '—')}")
|
||||
line += " | ".join(parts)
|
||||
msg.append(line)
|
||||
|
||||
# Aggiungi nota discordanza se presente
|
||||
point_comp = comparisons.get(x["name"])
|
||||
if point_comp:
|
||||
disc_parts = []
|
||||
if point_comp.get("snow"):
|
||||
comp = point_comp["snow"]
|
||||
icon_s24 = point_comp["icon_stats"]["snow_24h"]
|
||||
disc_parts.append(f"Neve: AROME {comp['arome']:.1f} cm | ICON {icon_s24:.1f} cm ({comp['diff_pct']:.0f}%)")
|
||||
if point_comp.get("rain"):
|
||||
comp = point_comp["rain"]
|
||||
icon_r3 = point_comp["icon_stats"]["rain3_max"]
|
||||
disc_parts.append(f"Pioggia: AROME {comp['arome']:.1f} mm | ICON {icon_r3:.1f} mm ({comp['diff_pct']:.0f}%)")
|
||||
if disc_parts:
|
||||
msg.append(f" ⚠️ Discordanza: {' | '.join(disc_parts)}")
|
||||
|
||||
msg.append("")
|
||||
msg.append("<i>Fonte dati: Open-Meteo</i>")
|
||||
|
||||
# FIX: usare \n invece di <br/>
|
||||
ok = telegram_send_html("\n".join(msg))
|
||||
ok = telegram_send_html("\n".join(msg), chat_ids=chat_ids)
|
||||
if ok:
|
||||
LOGGER.info("Notifica inviata.")
|
||||
else:
|
||||
@@ -457,7 +736,7 @@ def main() -> None:
|
||||
f"di neve (≥{PERSIST_HOURS}h) o pioggia 3h sopra soglia (≥{PERSIST_HOURS}h).\n"
|
||||
"<i>Fonte dati: Open-Meteo</i>"
|
||||
)
|
||||
ok = telegram_send_html(msg)
|
||||
ok = telegram_send_html(msg, chat_ids=chat_ids)
|
||||
if ok:
|
||||
LOGGER.info("Rientro notificato.")
|
||||
else:
|
||||
@@ -471,4 +750,11 @@ def main() -> None:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
arg_parser = argparse.ArgumentParser(description="Student alert Bologna")
|
||||
arg_parser.add_argument("--debug", action="store_true", help="Invia messaggi solo all'admin (chat ID: %s)" % TELEGRAM_CHAT_IDS[0])
|
||||
args = arg_parser.parse_args()
|
||||
|
||||
# In modalità debug, invia solo al primo chat ID (admin) e bypassa anti-spam
|
||||
chat_ids = [TELEGRAM_CHAT_IDS[0]] if args.debug else None
|
||||
|
||||
main(chat_ids=chat_ids, debug_mode=args.debug)
|
||||
|
||||
Reference in New Issue
Block a user