Compresas-03 / app.py
saske560's picture
Upload 4 files
60bd104 verified
Raw History Blame Contribute Delete
3.21 kB
"""
app.py — FastAPI + CompresUltra Bot
Diseñado para Hugging Face Spaces (o cualquier servidor con uvicorn).
El bot Pyrogram y FastAPI comparten el MISMO event loop asyncio.
Esto evita conflictos con asyncio y garantiza que el bot funcione.
Arrancar con:
uvicorn app:app --host 0.0.0.0 --port 7860
"""
import asyncio
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
# Importamos las funciones de inicio/parada del bot
from main import start_bot, stop_bot
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(application: FastAPI):
"""
Ciclo de vida de FastAPI:
- Al arrancar: inicia el bot en el mismo event loop.
- Al apagar: detiene el bot limpiamente.
"""
logger.info("🔧 Arrancando CompresUltra Bot...")
await start_bot() # ← awaitable, mismo event loop
logger.info("✅ Bot activo y FastAPI listo.")
yield # ← FastAPI sirve peticiones aquí
logger.info("🛑 Apagando bot...")
await stop_bot()
app = FastAPI(
title="CompresUltra Bot",
description="Bot de Telegram para comprimir videos con FFmpeg y colas por plan",
version="6.0",
lifespan=lifespan,
)
@app.get("/", response_class=HTMLResponse)
async def root():
return """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CompresUltra Bot</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex; justify-content: center; align-items: center;
min-height: 100vh; margin: 0;
background: linear-gradient(135deg, #1a1a2e, #16213e, #0f3460);
color: #eee;
}
.card {
background: rgba(255,255,255,0.07);
border-radius: 16px;
padding: 40px 50px;
text-align: center;
box-shadow: 0 8px 32px rgba(0,0,0,0.4);
backdrop-filter: blur(10px);
}
h1 { font-size: 2.2rem; margin-bottom: 8px; }
.badge {
display: inline-block;
background: #00d084;
color: #000;
border-radius: 20px;
padding: 4px 14px;
font-weight: bold;
font-size: 0.9rem;
margin-bottom: 20px;
}
p { color: #aaa; margin: 6px 0; }
a { color: #00d084; text-decoration: none; }
</style>
</head>
<body>
<div class="card">
<h1>🤖 CompresUltra Bot</h1>
<div class="badge">● Online</div>
<p>Bot de Telegram para comprimir videos con FFmpeg y colas por plan</p>
<p>⚡ Motor: libx265 | 🗄️ DB: Supabase / Postgres</p>
<br>
<p><a href="https://t.me/Sasuke286">👨‍💻 Desarrollador</a></p>
</div>
</body>
</html>
"""
@app.get("/health")
async def health():
return {"status": "ok", "bot": "CompresUltra Bot V.6 Ultra Supabase"}
@app.get("/ping")
async def ping():
return {"pong": True}