Karan Adhikari commited on
Commit
76c842e
·
unverified ·
1 Parent(s): 34637d4

Update pyro.py

Browse files
Files changed (1) hide show
  1. Backend/helper/pyro.py +60 -1
Backend/helper/pyro.py CHANGED
@@ -1,7 +1,7 @@
1
  from pyrogram.file_id import FileId
2
  from typing import Optional
3
  from Backend.logger import LOGGER
4
- from Backend import __version__, now, timezone
5
  from Backend.config import Telegram
6
  from Backend.helper.exceptions import FIleNotFound
7
  from aiofiles import open as aiopen
@@ -11,6 +11,65 @@ from Backend.pyrofork.bot import StreamBot
11
  import re
12
  from pyrogram.types import BotCommand
13
  from pyrogram import enums
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
 
16
  # ---------------------------------------------------------------------------
 
1
  from pyrogram.file_id import FileId
2
  from typing import Optional
3
  from Backend.logger import LOGGER
4
+ from Backend import __version__, now, timezone, db
5
  from Backend.config import Telegram
6
  from Backend.helper.exceptions import FIleNotFound
7
  from aiofiles import open as aiopen
 
11
  import re
12
  from pyrogram.types import BotCommand
13
  from pyrogram import enums
14
+ import asyncio
15
+ from Backend.helper.custom_dl import ACTIVE_STREAMS, RECENT_STREAMS
16
+
17
+
18
+ async def track_usage(stream_id: str, token: str, token_data: dict):
19
+ await asyncio.sleep(2)
20
+ limits = token_data.get("limits", {}) if token_data else {}
21
+ usage = token_data.get("usage", {}) if token_data else {}
22
+ daily_limit_gb = limits.get("daily_limit_gb")
23
+ monthly_limit_gb = limits.get("monthly_limit_gb")
24
+ initial_daily_bytes = usage.get("daily", {}).get("bytes", 0)
25
+ initial_monthly_bytes = usage.get("monthly", {}).get("bytes", 0)
26
+ last_tracked_bytes = 0
27
+ update_interval = 10
28
+ try:
29
+ while True:
30
+ await asyncio.sleep(update_interval)
31
+ stream_info = ACTIVE_STREAMS.get(stream_id)
32
+ if not stream_info:
33
+ for rec in RECENT_STREAMS:
34
+ if rec.get("stream_id") == stream_id:
35
+ final_bytes = rec.get("total_bytes", 0)
36
+ delta = final_bytes - last_tracked_bytes
37
+ if delta > 0:
38
+ try:
39
+ await db.update_token_usage(token, delta)
40
+ except Exception as e:
41
+ LOGGER.error(f"Final usage update failed: {e}")
42
+ break
43
+ return
44
+ current_bytes = stream_info.get("total_bytes", 0)
45
+ delta = current_bytes - last_tracked_bytes
46
+ if delta > 0:
47
+ try:
48
+ await db.update_token_usage(token, delta)
49
+ last_tracked_bytes = current_bytes
50
+ except Exception as e:
51
+ LOGGER.error(f"Periodic usage update failed: {e}")
52
+ if daily_limit_gb and daily_limit_gb > 0:
53
+ current_daily_gb = (initial_daily_bytes + current_bytes) / (1024 ** 3)
54
+ if current_daily_gb >= daily_limit_gb:
55
+ LOGGER.debug(f"Daily limit reached for token, stream {stream_id} may be blocked by verify_token")
56
+ if monthly_limit_gb and monthly_limit_gb > 0:
57
+ current_monthly_gb = (initial_monthly_bytes + current_bytes) / (1024 ** 3)
58
+ if current_monthly_gb >= monthly_limit_gb:
59
+ LOGGER.debug(f"Monthly limit reached for token, stream {stream_id} may be blocked by verify_token")
60
+ except asyncio.CancelledError:
61
+ stream_info = ACTIVE_STREAMS.get(stream_id)
62
+ if stream_info:
63
+ current_bytes = stream_info.get("total_bytes", 0)
64
+ delta = current_bytes - last_tracked_bytes
65
+ if delta > 0:
66
+ try:
67
+ await db.update_token_usage(token, delta)
68
+ LOGGER.info(f"Cancelled - final update for {stream_id}: {delta} bytes")
69
+ except Exception as e:
70
+ LOGGER.error(f"Cancelled usage update failed: {e}")
71
+
72
+
73
 
74
 
75
  # ---------------------------------------------------------------------------