File size: 3,210 Bytes
00af25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60bd104
 
00af25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60bd104
 
00af25d
 
 
 
 
 
 
 
 
 
60bd104
00af25d
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
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}