Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,44 +1,74 @@
|
|
| 1 |
# main.py
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
import uvicorn
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.responses import HTMLResponse
|
| 6 |
-
import
|
| 7 |
-
import asyncio
|
| 8 |
-
import bot # import your bot.py file
|
| 9 |
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
def home():
|
| 24 |
-
active_workers = bot.config.CONCURRENT_WORKERS
|
| 25 |
-
running_batches = len(bot.BATCH_JOBS)
|
| 26 |
-
queued_tasks = bot.TASK_QUEUE.qsize()
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
<html>
|
| 30 |
-
<head
|
|
|
|
|
|
|
| 31 |
<body>
|
| 32 |
-
<
|
| 33 |
-
<p
|
| 34 |
-
<
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
</body>
|
| 39 |
</html>
|
| 40 |
"""
|
| 41 |
-
return HTMLResponse(content=html)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
|
|
|
|
|
|
| 1 |
# main.py
|
| 2 |
+
import os
|
| 3 |
+
import logging
|
| 4 |
+
import asyncio
|
| 5 |
|
|
|
|
| 6 |
from fastapi import FastAPI
|
| 7 |
from fastapi.responses import HTMLResponse
|
| 8 |
+
import uvicorn
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
from bot import bot, setup_dispatcher, TASK_QUEUE, BATCH_JOBS
|
| 11 |
|
| 12 |
+
# Setup logging
|
| 13 |
+
logging.basicConfig(
|
| 14 |
+
level=logging.INFO,
|
| 15 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
app = FastAPI(
|
| 19 |
+
title="Terabox Bot Admin",
|
| 20 |
+
description="Web interface for checking Terabox Bot status.",
|
| 21 |
+
version="1.0.0",
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
dp = setup_dispatcher()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
@app.on_event("startup")
|
| 27 |
+
async def on_startup():
|
| 28 |
+
logging.info("Starting bot polling in background...")
|
| 29 |
+
asyncio.create_task(dp.start_polling(bot))
|
| 30 |
+
|
| 31 |
+
# Routes
|
| 32 |
+
@app.get("/", response_class=HTMLResponse)
|
| 33 |
+
async def root():
|
| 34 |
+
return """
|
| 35 |
<html>
|
| 36 |
+
<head>
|
| 37 |
+
<title>Terabox Bot - Admin Panel</title>
|
| 38 |
+
</head>
|
| 39 |
<body>
|
| 40 |
+
<h2>🚀 Terabox Bot is running!</h2>
|
| 41 |
+
<p>Endpoints:</p>
|
| 42 |
+
<ul>
|
| 43 |
+
<li><a href="/health">/health</a> → Health check</li>
|
| 44 |
+
<li><a href="/status">/status</a> → Bot queue status</li>
|
| 45 |
+
</ul>
|
| 46 |
</body>
|
| 47 |
</html>
|
| 48 |
"""
|
|
|
|
| 49 |
|
| 50 |
+
@app.get("/health")
|
| 51 |
+
async def health():
|
| 52 |
+
return {"status": "ok", "message": "Bot is healthy!"}
|
| 53 |
+
|
| 54 |
+
@app.get("/status", response_class=HTMLResponse)
|
| 55 |
+
async def status():
|
| 56 |
+
html = "<h3>📊 Terabox Bot - Current Status</h3>"
|
| 57 |
+
html += f"<p>Pending queue size: {TASK_QUEUE.qsize()}</p>"
|
| 58 |
+
|
| 59 |
+
if not BATCH_JOBS:
|
| 60 |
+
html += "<p>✅ No active batches.</p>"
|
| 61 |
+
else:
|
| 62 |
+
html += "<ul>"
|
| 63 |
+
for batch_id, batch in BATCH_JOBS.items():
|
| 64 |
+
processed = batch["processed_links"]
|
| 65 |
+
total = batch["total_links"]
|
| 66 |
+
html += f"<li>Batch <b>{batch_id[:6]}</b>: {processed}/{total} processed</li>"
|
| 67 |
+
html += "</ul>"
|
| 68 |
+
|
| 69 |
+
return html
|
| 70 |
+
|
| 71 |
+
# Run with uvicorn
|
| 72 |
if __name__ == "__main__":
|
| 73 |
+
port = int(os.getenv("PORT", 7860)) # HuggingFace prefers PORT env sometimes
|
| 74 |
+
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=False)
|