diff --git a/openshorts/telegram_bot.py b/openshorts/telegram_bot.py index a9a15d0..af47911 100644 --- a/openshorts/telegram_bot.py +++ b/openshorts/telegram_bot.py @@ -231,15 +231,19 @@ async def process_video(bot, url: str): log.info(f"[process] Sent initial Telegram message (id={msg.message_id})") # 3. Set up environment for main.py + # Use a temp dir for intermediate files — main.py will copy finished clips + # to NEXTCLOUD_TARGET and run docker files:scan automatically. + temp_output = tempfile.mkdtemp(prefix="viral_bot_") env = os.environ.copy() env["NEXTCLOUD_TARGET"] = target env["PYTHONUNBUFFERED"] = "1" + log.info(f"[process] Temp output dir: {temp_output}") # 4. Build command — main.py MUST run with the viral venv Python cmd = [ VENV_PYTHON, "-u", MAIN_PY, "-u", url, - "-o", target, + "-o", temp_output, ] log.info(f"[process] Spawning command: {' '.join(cmd)}") @@ -347,10 +351,37 @@ async def process_video(bot, url: str): ) raise - # 5. Final completion message - if rc == 0: + # 5. Cleanup temp dir + try: + shutil.rmtree(temp_output, ignore_errors=True) + log.info(f"[process] Cleaned up temp dir: {temp_output}") + except Exception as e: + log.warning(f"[process] Failed to clean temp dir: {e}") + + # 6. Always force a Nextcloud scan so files appear regardless of what main.py did + log.info("[process] Forcing Nextcloud files:scan...") + try: + scan_result = await asyncio.to_thread( + subprocess.run, + ["docker", "exec", "-u", "www-data", "nextcloud", "php", "occ", "files:scan", "renato97"], + capture_output=True, text=True, timeout=120, + ) + log.info(f"[process] Nextcloud scan done (rc={scan_result.returncode})") + log.debug(f"[process] scan stdout: {scan_result.stdout[-300:]}") + except Exception as e: + log.warning(f"[process] Nextcloud scan failed: {e}") + + # 7. Count final clips in target folder + try: + final_clips = [f for f in os.listdir(target) if f.endswith(".mp4") and not f.startswith("temp_")] + total_done = len(final_clips) + log.info(f"[process] Final clip count in Nextcloud folder: {total_done}") + except Exception: total_done = len(completed_clips) - log.info(f"[process] SUCCESS — {total_done} clips uploaded to Nextcloud") + + # 8. Final completion message + if rc == 0 and total_done > 0: + log.info(f"[process] SUCCESS — {total_done} clips in Nextcloud") try: await msg.delete() except Exception: @@ -358,18 +389,24 @@ async def process_video(bot, url: str): await bot.send_message( CHAT_ID, f"✅ *¡Listo!* `{title[:60] or url[:60]}`\n\n" - f"📱 *{total_done} clip{'s' if total_done != 1 else ''}* subidos a Nextcloud:\n" + f"📱 *{total_done} clip{'s' if total_done != 1 else ''}* en Nextcloud:\n" f"`{folder}/`\n\n" f"Abrilos en tu Nextcloud 🎉", parse_mode="Markdown", ) - else: + elif rc != 0: log.error(f"[process] FAILED — main.py returned code {rc}") await edit_msg( f"❌ *Falló el procesamiento* (código {rc})\n" f"`{url[:80]}`\n\n" f"Revisá los logs: `sudo journalctl -u viral-telegram.service -n 100`" ) + else: + log.warning("[process] rc=0 but no final clips found in Nextcloud folder") + await edit_msg( + f"⚠️ *Pipeline completado pero no se encontraron clips*\n" + f"Revisá la carpeta `{folder}/` en Nextcloud manualmente." + ) # ─── App lifecycle ─────────────────────────────────────────────────────────────