Spaces:
Sleeping
Sleeping
| # 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 --- | |
| 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()) | |
| async def health_check(): | |
| return "β Terabox Bot is running." | |
| 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) |