02-ago: [proyecto] vigilancia - YouTube en la tele: youtube <URL> reproduce video y apaga al terminar. Comando en el bot.

This commit is contained in:
minguezsanzjuanjose
2026-08-02 21:43:47 +02:00
parent 4584fdb4a6
commit e33d4c1369
3 changed files with 78 additions and 2 deletions

View File

@@ -316,6 +316,53 @@ def reproducir_en_teles(texto):
anuncio_en_teles(texto)
def youtube_en_teles(url):
"""Reproduce un video de YouTube en la tele y la apaga al terminar."""
import re
match = re.search(r"(?:v=|youtu\.be/)([A-Za-z0-9_-]{11})", url)
if not match:
print("[Voz] URL de YouTube no válida")
return
video_id = match.group(1)
try:
import pychromecast
from pychromecast.controllers.youtube import YouTubeController
chromecasts, browser = pychromecast.get_chromecasts(timeout=8)
try:
if config.GOOGLE_TV_NOMBRES:
objetivos = [c for c in chromecasts
if c.cast_info.friendly_name in config.GOOGLE_TV_NOMBRES]
else:
objetivos = chromecasts
for c in objetivos:
try:
c.wait(timeout=10)
c.set_volume(config.GOOGLE_TV_VOLUMEN / 100.0)
yt = YouTubeController()
c.register_handler(yt)
print(f"[Voz] Lanzando YouTube en {c.cast_info.friendly_name}: {video_id}")
yt.play_video(video_id)
# Esperar a que termine el video
time.sleep(5)
while True:
try:
state = c.media_controller.status.player_state
if state == "IDLE":
break
except Exception:
pass
time.sleep(3)
apagar_tv_remote()
print(f"[Voz] Video terminado, TV apagada")
except Exception as e:
print(f"[Voz] Error en {c.cast_info.friendly_name}: {e}")
finally:
pychromecast.discovery.stop_discovery(browser)
except Exception as e:
print(f"[Voz] Error YouTube: {e}")
def apagar_tv_remote():
"""Apaga la TV via AndroidTVRemote (protocolo oficial Google)."""
try: