understanding commited on
Commit
ffdffe9
Β·
verified Β·
1 Parent(s): 33a88e6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -59
main.py CHANGED
@@ -1,74 +1,38 @@
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)
 
1
  # main.py
 
2
  import logging
3
  import asyncio
 
4
  from fastapi import FastAPI
5
+ from fastapi.responses import JSONResponse
6
  import uvicorn
7
 
8
+ import bot # import your bot.py (it already starts Dispatcher there)
9
+ import db_utils
 
 
 
 
 
10
 
11
+ logging.basicConfig(level=logging.INFO)
12
+ logger = logging.getLogger(__name__)
 
 
 
13
 
14
+ app = FastAPI()
15
 
16
  @app.on_event("startup")
17
  async def on_startup():
18
+ logger.info("Starting bot polling in background...")
19
+ asyncio.create_task(bot.run_polling())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @app.get("/health")
22
+ async def health_check():
23
+ return JSONResponse({
24
+ "status": "ok",
25
+ "workers": bot.config.CONCURRENT_WORKERS,
26
+ "active_batches": len(bot.BATCH_JOBS),
27
+ "active_users": await db_utils.get_all_active_user_ids_db(),
28
+ })
29
+
30
+ @app.get("/")
31
+ async def root_page():
32
+ return JSONResponse({
33
+ "message": "πŸš€ TeraBox Bot is running.",
34
+ "endpoints": ["/health"],
35
+ })
 
 
 
 
 
36
 
 
37
  if __name__ == "__main__":
38
+ uvicorn.run(app, host="0.0.0.0", port=7860)