fix: async get_video_title + debug prints in telegram_bot

This commit is contained in:
Renato
2026-07-02 17:39:20 +02:00
parent d9310e904d
commit 02323222e4
+30 -11
View File
@@ -65,14 +65,22 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(f"⏳ Encolado ({qsize + 1}º en cola).")
def get_video_title(url: str) -> str:
async def get_video_title(url: str) -> str:
try:
r = subprocess.run(
r = await asyncio.to_thread(
subprocess.run,
["yt-dlp", "--print", "title", url],
capture_output=True, text=True, timeout=30,
)
return r.stdout.strip()
except Exception:
except subprocess.TimeoutExpired:
print("[DEBUG] yt-dlp title timed out after 30s")
return "video"
except FileNotFoundError:
print("[DEBUG] yt-dlp command not found in PATH")
return "video"
except Exception as e:
print(f"[DEBUG] yt-dlp title failed: {e}")
return "video"
@@ -94,11 +102,16 @@ async def process_queue(bot):
async def process_video(bot, url: str):
title = get_video_title(url)
print(f"[DEBUG] process_video: url={url[:80]}")
title = await get_video_title(url)
print(f"[DEBUG] title={title}")
safe = re.sub(r'[<>:"/\\|?*#\s]+', "_", title)[:80]
folder = f"{safe}_{datetime.now():%Y-%m-%d}"
target = f"{NEXTCLOUD_BASE}/{folder}"
os.makedirs(target, exist_ok=True)
print(f"[DEBUG] NEXTCLOUD_TARGET={target}")
msg = await bot.send_message(CHAT_ID, "🚀 Descargando...")
@@ -111,14 +124,16 @@ async def process_video(bot, url: str):
last_text = ""
try:
cmd = [
str(SCRIPT_DIR / "main.py"),
"-u", url,
"-o", temp_dir,
]
print(f"[DEBUG] Spawning: {' '.join(cmd)}")
proc = await asyncio.create_subprocess_exec(
sys.executable,
"-u",
str(SCRIPT_DIR / "main.py"),
"-u",
url,
"-o",
temp_dir,
"-u", *cmd,
env=env,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
@@ -128,6 +143,7 @@ async def process_video(bot, url: str):
line = raw.decode("utf-8", errors="replace").strip()
if not line:
continue
print(f"[DEBUG] stdout: {line[:100]}")
text = None
if "Transcribing" in line:
@@ -137,13 +153,15 @@ async def process_video(bot, url: str):
elif m := re.search(r"Found\s+(\d+)\s+viral", line, re.IGNORECASE):
total_clips = int(m.group(1))
text = f"🔥 Encontrados {total_clips} clips"
elif m := re.search(r"\bClip\s+(\d+)\]\s+Starting", line):
elif m := re.search(r"\[Clip\s+(\d+)\]\s+Starting", line):
cur_clip = int(m.group(1))
text = f"🎬 Clip {cur_clip}/?: reencuadrando + subs..."
elif "Normalizing" in line and cur_clip > 0:
text = f"📱 Clip {cur_clip}/{max(total_clips, cur_clip)}: normalizando..."
elif "Copying to Nextcloud" in line and cur_clip > 0:
text = f"📤 Clip {cur_clip}/{max(total_clips, cur_clip)}: subiendo a Nextcloud..."
elif "Total execution time" in line:
text = None
if text and text != last_text:
last_text = text
@@ -153,6 +171,7 @@ async def process_video(bot, url: str):
pass
await proc.wait()
print(f"[DEBUG] main.py exit code: {proc.returncode}")
finally:
shutil.rmtree(temp_dir, ignore_errors=True)