understanding commited on
Commit
350b6eb
Β·
verified Β·
1 Parent(s): c612731

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +58 -28
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 logging
7
- import asyncio
8
- import bot # import your bot.py file
9
 
10
- app = FastAPI()
11
 
12
- @app.on_event("startup")
13
- async def startup_event():
14
- logging.basicConfig(level=logging.INFO)
15
- logging.info("Starting bot polling in background...")
16
- asyncio.create_task(bot.run_bot())
17
 
18
- @app.get("/health")
19
- def health():
20
- return {"status": "ok"}
 
 
21
 
22
- @app.get("/")
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
- html = f"""
 
 
 
 
 
 
 
 
29
  <html>
30
- <head><title>Terabox Bot Status</title></head>
 
 
31
  <body>
32
- <h1>Terabox Bot Status</h1>
33
- <p><b>Bot Username:</b> {bot.bot_username or 'Unknown'}</p>
34
- <p><b>Active Workers:</b> {active_workers}</p>
35
- <p><b>Running Batches:</b> {running_batches}</p>
36
- <p><b>Queued Tasks:</b> {queued_tasks}</p>
37
- <p><b>Health:</b> <a href="/health">/health</a></p>
38
  </body>
39
  </html>
40
  """
41
- return HTMLResponse(content=html)
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  if __name__ == "__main__":
44
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
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)