Backup automatico script del 2026-07-26 07:00
This commit is contained in:
1 parent
4802b021fe
commit
a9bbb92090
19 files changed
+1086
-550
No files matched your search
@@ -78,7 +78,7 @@ def get_bot_token():
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def save_current_state(state, report_meta=None):
|
||||
def save_current_state(state, report_meta=None, summary=None):
|
||||
try:
|
||||
# Aggiungi timestamp corrente per tracciare quando è stato salvato lo stato
|
||||
if report_meta is None:
|
||||
@@ -87,7 +87,19 @@ def save_current_state(state, report_meta=None):
|
||||
"points": state,
|
||||
"last_update": datetime.datetime.now().isoformat(),
|
||||
"report_meta": report_meta,
|
||||
"alert_active": any(int(v or 0) > 0 for v in (state or {}).values()),
|
||||
}
|
||||
if summary:
|
||||
state_with_meta["summary"] = str(summary)[:3000]
|
||||
else:
|
||||
# Mantieni l'ultimo summary Telegram se non ci sono nuovi aggiornamenti
|
||||
try:
|
||||
with open(STATE_FILE, "r") as rf:
|
||||
prev = json.load(rf)
|
||||
if isinstance(prev, dict) and prev.get("summary"):
|
||||
state_with_meta["summary"] = str(prev["summary"])[:3000]
|
||||
except Exception:
|
||||
pass
|
||||
with open(STATE_FILE, 'w') as f:
|
||||
json.dump(state_with_meta, f)
|
||||
except Exception as e:
|
||||
@@ -2251,7 +2263,26 @@ def main():
|
||||
append_report(new_alerts, improvement_msg, important, report_meta, DEBUG_MODE)
|
||||
|
||||
# Genera e invia mappa solo quando ci sono aggiornamenti
|
||||
ice_summary = None
|
||||
if new_alerts or solved_alerts:
|
||||
parts = []
|
||||
if new_alerts:
|
||||
parts.append("Aggiornamenti rischio:\n" + "\n\n".join(new_alerts[:12]))
|
||||
if solved_alerts:
|
||||
parts.append("Rientri:\n" + "\n".join(solved_alerts[:8]))
|
||||
ice_summary = "\n\n".join(parts)
|
||||
try:
|
||||
from webapp_alert import publish_web_alert
|
||||
publish_web_alert(
|
||||
ice_summary,
|
||||
"ghiaccio",
|
||||
"warning",
|
||||
is_html=True,
|
||||
title="Rischio ghiaccio stradale",
|
||||
)
|
||||
except Exception as e:
|
||||
if DEBUG_MODE:
|
||||
print(f"Web summary failed: {e}")
|
||||
if DEBUG_MODE:
|
||||
print(f"Generazione mappa per {len(map_points_data)} punti...")
|
||||
map_path = os.path.join(SCRIPT_DIR, "ice_risk_map.png")
|
||||
@@ -2265,18 +2296,16 @@ def main():
|
||||
f"🕒 {now.strftime('%d/%m/%Y %H:%M')}\n"
|
||||
f"📊 Punti monitorati: {len(map_points_data)}"
|
||||
)
|
||||
# Invia anche i report testuali (allineati a Telegram/WebApp)
|
||||
for block in (new_alerts + solved_alerts)[:15]:
|
||||
try:
|
||||
send_telegram_broadcast(token, block, debug_mode=DEBUG_MODE)
|
||||
except Exception:
|
||||
pass
|
||||
photo_sent = send_telegram_photo(token, map_path, caption, debug_mode=DEBUG_MODE)
|
||||
if DEBUG_MODE:
|
||||
print(f"Mappa inviata via Telegram: {photo_sent}")
|
||||
# Pulisci file temporaneo solo se non in debug mode (per permettere verifica)
|
||||
if not DEBUG_MODE:
|
||||
try:
|
||||
if os.path.exists(map_path):
|
||||
os.remove(map_path)
|
||||
except Exception:
|
||||
pass
|
||||
elif DEBUG_MODE:
|
||||
print(f"File mappa mantenuto per debug: {map_path}")
|
||||
# Mantieni la mappa per la WebApp (non cancellare)
|
||||
print("Mappa inviata.")
|
||||
else:
|
||||
if DEBUG_MODE:
|
||||
@@ -2288,7 +2317,7 @@ def main():
|
||||
print("Nessuna variazione.")
|
||||
|
||||
if not DEBUG_MODE:
|
||||
save_current_state(current_state, report_meta=report_meta)
|
||||
save_current_state(current_state, report_meta=report_meta, summary=ice_summary)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user