Backup automatico script del 2026-04-05 07:00

This commit is contained in:
2026-04-05 07:00:02 +02:00
parent c25c309a15
commit 7594a42875
3 changed files with 108 additions and 53 deletions
+47 -22
View File
@@ -153,24 +153,25 @@ def get_timezone_from_coords(lat: float, lon: float) -> str:
except Exception as e:
logger.warning(f"Errore timezonefinder: {e}")
# Fallback: stima timezone da longitudine (approssimativo)
# Ogni 15 gradi = 1 ora di differenza da UTC
# Fallback: stima da longitudine (approssimativo). Ordine importante: gli offset
# tipici delle Americhe (-4 NY, -7 Denver, …) rientrano in [-10, 2] e non devono
# essere classificati come Europa prima dei rami per le Americhe.
offset_hours = int(lon / 15)
# Mappatura approssimativa a timezone IANA
if -10 <= offset_hours <= 2: # Europa
return "Europe/Rome"
elif 3 <= offset_hours <= 5: # Medio Oriente
return "Asia/Dubai"
elif 6 <= offset_hours <= 8: # Asia centrale
return "Asia/Kolkata"
elif 9 <= offset_hours <= 11: # Asia orientale
return "Asia/Tokyo"
elif -5 <= offset_hours <= -3: # Americhe orientali
return "America/New_York"
elif -8 <= offset_hours <= -6: # Americhe occidentali
if -8 <= offset_hours <= -6:
return "America/Los_Angeles"
else:
return "UTC"
if -5 <= offset_hours <= -3:
return "America/New_York"
if lon < -30 and offset_hours <= -9:
return "America/Los_Angeles"
if 3 <= offset_hours <= 5:
return "Asia/Dubai"
if 6 <= offset_hours <= 8:
return "Asia/Kolkata"
if 9 <= offset_hours <= 11:
return "Asia/Tokyo"
if -10 <= offset_hours <= 2:
return "Europe/Rome"
return "UTC"
def add_viaggio(chat_id: str, location: str, lat: float, lon: float, name: str, timezone: Optional[str] = None) -> None:
"""Aggiunge o aggiorna un viaggio attivo per un chat_id (sovrascrive se esiste)"""
@@ -656,10 +657,11 @@ async def meteo_viaggio_command(update: Update, context: ContextTypes.DEFAULT_TY
await update.message.reply_text(f"❌ Località '{location}' non trovata. Verifica il nome e riprova.", parse_mode="Markdown")
return
lat, lon, name, cc = coords
lat, lon, name, cc, geo_tz = coords
# Ottieni timezone per questa localizzazione
timezone = get_timezone_from_coords(lat, lon)
# Fuso: Open-Meteo geocoding espone già timezone IANA; altrimenti timezonefinder / fallback
geo_tz_clean = geo_tz.strip() if isinstance(geo_tz, str) and geo_tz.strip() else ""
timezone = geo_tz_clean or get_timezone_from_coords(lat, lon)
# Conferma riconoscimento località
await update.message.reply_text(
@@ -755,13 +757,36 @@ async def meteo_viaggio_command(update: Update, context: ContextTypes.DEFAULT_TY
await update.message.reply_text(f"❌ Errore durante l'elaborazione: {str(e)}", parse_mode="Markdown")
async def scheduled_morning_report(context: ContextTypes.DEFAULT_TYPE) -> None:
# Esegui una sola chiamata e invia il report a tutti i chat_id
report = call_meteo_script(["--home"])
# Stesso comportamento di `/meteo` senza argomenti: Casa (+ viaggio se attivo) per utente.
report_casa = call_meteo_script(["--home"])
for uid in ALLOWED_IDS:
chat_id = str(uid)
try:
await context.bot.send_message(chat_id=uid, text=report, parse_mode="Markdown")
await context.bot.send_message(
chat_id=uid,
text=f"🏠 **Report Meteo - Casa**\n\n{report_casa}",
parse_mode="Markdown",
)
except Exception:
pass
viaggio_attivo = get_viaggio(chat_id)
if viaggio_attivo:
report_viaggio = call_meteo_script(
[
"--query",
viaggio_attivo["location"],
"--timezone",
viaggio_attivo.get("timezone", "Europe/Rome"),
]
)
try:
await context.bot.send_message(
chat_id=uid,
text=f"✈️ **Report Meteo - {viaggio_attivo['name']}**\n\n{report_viaggio}",
parse_mode="Markdown",
)
except Exception:
pass
@restricted
async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: