Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| # (c) Shrimadhav U K (Modded by Optima 🫦) | |
| import os | |
| import subprocess | |
| import logging | |
| import asyncio | |
| from flask import Flask | |
| from threading import Thread | |
| from dotenv import load_dotenv | |
| import pyrogram | |
| import uvloop | |
| # ---------------------------------------- | |
| # 1. PARCHE ARIA2C Y ENTORNO | |
| # ---------------------------------------- | |
| if not os.path.exists("aria2c"): | |
| print("Mami está bajando tu juguetito para Render/HF...") | |
| link = "https://github.com/asdo92/aria2-static-builds/releases/download/v1.37.0/aria2-1.37.0-linux-gnu-64bit-build1.tar.bz2" | |
| os.system(f"curl -L {link} -o aria2.tar.bz2") | |
| os.system("tar -jxvf aria2.tar.bz2 --wildcards '*/aria2c' --strip-components=1") | |
| if os.path.exists("aria2c"): | |
| os.chmod("aria2c", 0o755) | |
| os.system("rm aria2.tar.bz2") | |
| print("✅ aria2c está listo para la acción.") | |
| # ---------------------- | |
| # 2. CONFIGURACIÓN LOGGING | |
| # ---------------------- | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # ---------------------- | |
| # 3. SERVIDOR FLASK (LA CARA PÚBLICA) | |
| # ---------------------- | |
| server = Flask(__name__) | |
| def home(): | |
| return "I'm alive and kicking! 🚀 - Optimus Prime Edition" | |
| def run_server(): | |
| # Hugging Face necesita el puerto 7860 sí o sí | |
| port = int(os.environ.get("PORT", 7860)) | |
| logger.info(f"🌐 Iniciando Flask en el puerto {port}") | |
| server.run(host='0.0.0.0', port=port) | |
| def keep_alive(): | |
| t = Thread(target=run_server) | |
| t.daemon = True | |
| t.start() | |
| # ---------------------- | |
| # 4. CONFIGURACIÓN DEL BOT | |
| # ---------------------- | |
| load_dotenv() | |
| # Intentamos cargar la configuración | |
| try: | |
| if os.environ.get("WEBHOOK"): | |
| from sample_config import Config | |
| else: | |
| from config import Config | |
| except ImportError: | |
| logger.error("❌ No se encontró el archivo de configuración (config.py).") | |
| class Config: | |
| DOWNLOAD_LOCATION = "./downloads" | |
| TG_BOT_TOKEN = os.environ.get("TG_BOT_TOKEN") | |
| APP_ID = int(os.environ.get("APP_ID", 0)) | |
| API_HASH = os.environ.get("API_HASH") | |
| # ---------------------- | |
| # 5. FUNCIÓN PRINCIPAL | |
| # ---------------------- | |
| async def main(): | |
| # Crear carpeta de descargas | |
| if not os.path.isdir(Config.DOWNLOAD_LOCATION): | |
| os.makedirs(Config.DOWNLOAD_LOCATION) | |
| # IMPORTANTE: Cargamos el String de 271 bytes que generamos con Optima | |
| SESSION = os.environ.get("BOT_SESSION_STRING") | |
| app = pyrogram.Client( | |
| name=":memory:", | |
| session_string=SESSION if SESSION else None, | |
| bot_token=Config.TG_BOT_TOKEN, | |
| api_id=Config.APP_ID, | |
| api_hash=Config.API_HASH, | |
| plugins=dict(root="plugins"), | |
| in_memory=True, # Vital para Hugging Face | |
| ipv6=False # Evita Timeouts en la red de HF | |
| ) | |
| async with app: | |
| me = await app.get_me() | |
| logger.info(f"🚀 Bot @{me.username} iniciado con éxito.") | |
| # Mantener vivo el loop asíncrono | |
| await asyncio.Event().wait() | |
| # ---------------------- | |
| # 6. EJECUCIÓN | |
| # ---------------------- | |
| if __name__ == "__main__" : | |
| # Instalamos el loop de alta velocidad | |
| uvloop.install() | |
| # PASO CLAVE: Arrancamos la web ANTES que el bot | |
| keep_alive() | |
| try: | |
| asyncio.run(main()) | |
| except KeyboardInterrupt: | |
| logger.warning("Bot detenido por el usuario.") | |
| except Exception as e: | |
| logger.critical(f"Error fatal: {e}") |