Spaces:
Sleeping
Sleeping
File size: 2,872 Bytes
222f19c 0d44831 350b6eb 41fd54e 0d44831 431014c 3140238 3cedaf4 431014c 0d44831 431014c 41fd54e 431014c 41fd54e 431014c 3cedaf4 431014c 4b81f85 431014c 41fd54e 431014c 3cedaf4 431014c 3cedaf4 431014c 3cedaf4 431014c 3cedaf4 431014c 350b6eb 431014c 222f19c 431014c |
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 |
# main.py
import logging
import asyncio
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, PlainTextResponse
import uvicorn
from bot import start_bot
import db_utils
import config
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
logger = logging.getLogger("main")
# --- App & Bot setup ---
app = FastAPI(title="Terabox Downloader Bot API")
dp, bot = start_bot()
# --- Status Variables ---
APP_STATUS = {
"bot_running": False,
"workers": config.CONCURRENT_WORKERS,
"active_batches": 0,
"active_users": 0,
}
# --- Background Bot Runner ---
async def run_bot_polling():
from aiogram import Dispatcher
APP_STATUS["bot_running"] = True
await dp.start_polling(bot)
# --- FastAPI Routes ---
@app.on_event("startup")
async def on_startup():
logger.info("===== Application Startup =====")
await db_utils.initialize_database()
# Count users
active_users = await db_utils.get_all_active_user_ids_db()
APP_STATUS["active_users"] = len(active_users)
# Start bot in background
logger.info("Starting bot polling in background...")
asyncio.create_task(run_bot_polling())
@app.get("/health", response_class=PlainTextResponse)
async def health_check():
return "β
Terabox Bot is running."
@app.get("/", response_class=HTMLResponse)
async def index_page():
html = f"""
<html>
<head>
<title>π Terabox Downloader Bot Dashboard</title>
<style>
body {{ font-family: Arial, sans-serif; background-color: #f9f9f9; color: #333; }}
.container {{ max-width: 600px; margin: auto; padding: 20px; text-align: center; }}
.status {{ background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }}
.status h2 {{ color: #2c3e50; }}
.status p {{ font-size: 18px; }}
.footer {{ margin-top: 20px; font-size: 14px; color: #888; }}
</style>
</head>
<body>
<div class="container">
<h1>π Terabox Downloader Bot</h1>
<div class="status">
<h2>Status</h2>
<p>Bot Running: <b style="color:green;">{APP_STATUS['bot_running']}</b></p>
<p>Concurrent Workers: <b>{APP_STATUS['workers']}</b></p>
<p>Active Users (cached): <b>{APP_STATUS['active_users']}</b></p>
<p>Active Batches (live): <b>{APP_STATUS['active_batches']}</b></p>
</div>
<div class="footer">
© 2025 Terabox Bot | Powered by FastAPI & Aiogram
</div>
</div>
</body>
</html>
"""
return html
# --- Main runner ---
if __name__ == "__main__":
logger.info("===== Starting Uvicorn Server =====")
uvicorn.run("main:app", host="0.0.0.0", port=7860) |