superguardado: fixes pi_bot (offset persistente, unit systemd) + voz.py (desconectar chromecast) + lecciones 27-31
This commit is contained in:
@@ -9,6 +9,7 @@ import requests
|
||||
|
||||
BASE = os.path.dirname(os.path.abspath(__file__))
|
||||
LOG = os.path.join(BASE, "bot.log")
|
||||
OFFSET_FILE = os.path.join(BASE, "bot.offset")
|
||||
TV_API = "http://127.0.0.1:8600/tv/mensaje"
|
||||
|
||||
|
||||
@@ -174,13 +175,29 @@ def monitor_temperatura():
|
||||
f"(límite {ALERTA_TEMP}°C)\n{msg}", CHAT_ID)
|
||||
|
||||
|
||||
def cargar_offset():
|
||||
try:
|
||||
with open(OFFSET_FILE) as f:
|
||||
return int(f.read().strip())
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def guardar_offset(offset):
|
||||
try:
|
||||
with open(OFFSET_FILE, "w") as f:
|
||||
f.write(str(offset))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
if not TOKEN:
|
||||
print("Falta TELEGRAM_TOKEN en .env")
|
||||
return
|
||||
threading.Thread(target=monitor_temperatura, daemon=True).start()
|
||||
offset = None
|
||||
log("Bot iniciado")
|
||||
offset = cargar_offset()
|
||||
log("Bot iniciado" + (f" (offset {offset})" if offset else ""))
|
||||
print("Bot iniciado. Esperando mensajes...")
|
||||
while True:
|
||||
try:
|
||||
@@ -193,6 +210,7 @@ def main():
|
||||
).json()
|
||||
for upd in r.get("result", []):
|
||||
offset = upd["update_id"] + 1
|
||||
guardar_offset(offset)
|
||||
log(f"UPDATE {upd.get('update_id')}")
|
||||
msg = upd.get("message") or upd.get("channel_post")
|
||||
if not msg:
|
||||
|
||||
@@ -231,6 +231,15 @@ def _url_silencio():
|
||||
return f"http://{_ip_lan()}:{config.GOOGLE_TV_PUERTO}/{os.path.basename(ruta)}"
|
||||
|
||||
|
||||
def _desconectar(chromecasts):
|
||||
for cc in chromecasts:
|
||||
try:
|
||||
if cc.is_active and cc.is_connected:
|
||||
cc.disconnect()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _reproducir_y_esperar(c, url, content_type, titulo, desc):
|
||||
c.media_controller.play_media(url, content_type, title=titulo)
|
||||
print(f"[Voz] {desc}")
|
||||
@@ -307,6 +316,7 @@ def anuncio_en_teles(texto):
|
||||
except Exception as e:
|
||||
print(f"[Voz] Error en {c.cast_info.friendly_name}: {e}")
|
||||
finally:
|
||||
_desconectar(chromecasts)
|
||||
pychromecast.discovery.stop_discovery(browser)
|
||||
except Exception as e:
|
||||
print(f"[Voz] Error de casting: {e}")
|
||||
@@ -358,6 +368,7 @@ def youtube_en_teles(url):
|
||||
except Exception as e:
|
||||
print(f"[Voz] Error en {c.cast_info.friendly_name}: {e}")
|
||||
finally:
|
||||
_desconectar(chromecasts)
|
||||
pychromecast.discovery.stop_discovery(browser)
|
||||
except Exception as e:
|
||||
print(f"[Voz] Error YouTube: {e}")
|
||||
@@ -414,6 +425,7 @@ def anuncio_simple(texto):
|
||||
except Exception as e:
|
||||
print(f"[Voz] Error en {c.cast_info.friendly_name}: {e}")
|
||||
finally:
|
||||
_desconectar(chromecasts)
|
||||
pychromecast.discovery.stop_discovery(browser)
|
||||
except Exception as e:
|
||||
print(f"[Voz] Error de casting: {e}")
|
||||
|
||||
Reference in New Issue
Block a user