Karan Adhikari commited on
Update stream_routes.py
Browse files
Backend/fastapi/routes/stream_routes.py
CHANGED
|
@@ -10,9 +10,9 @@ from fastapi import APIRouter, Request, HTTPException, Depends
|
|
| 10 |
from fastapi.responses import Response as PlainResponse, StreamingResponse, JSONResponse
|
| 11 |
from collections import deque
|
| 12 |
from Backend import db
|
| 13 |
-
from Backend.config import Telegram
|
| 14 |
from Backend.logger import LOGGER
|
| 15 |
from Backend.helper.encrypt import decode_string
|
|
|
|
| 16 |
from Backend.helper.custom_dl import ByteStreamer, ACTIVE_STREAMS, RECENT_STREAMS
|
| 17 |
from Backend.pyrofork.bot import work_loads, multi_clients, client_dc_map, client_failures, client_avg_mbps
|
| 18 |
from Backend.fastapi.security.tokens import verify_token
|
|
@@ -99,60 +99,6 @@ def get_parallel_prefetch(client_count: int) -> tuple[int, int]:
|
|
| 99 |
value = min(max(ceil(client_count / 5), 1), 5)
|
| 100 |
return value, value
|
| 101 |
|
| 102 |
-
async def track_usage_from_stats(stream_id: str, token: str, token_data: dict):
|
| 103 |
-
await asyncio.sleep(2)
|
| 104 |
-
limits = token_data.get("limits", {}) if token_data else {}
|
| 105 |
-
usage = token_data.get("usage", {}) if token_data else {}
|
| 106 |
-
daily_limit_gb = limits.get("daily_limit_gb")
|
| 107 |
-
monthly_limit_gb = limits.get("monthly_limit_gb")
|
| 108 |
-
initial_daily_bytes = usage.get("daily", {}).get("bytes", 0)
|
| 109 |
-
initial_monthly_bytes = usage.get("monthly", {}).get("bytes", 0)
|
| 110 |
-
last_tracked_bytes = 0
|
| 111 |
-
update_interval = 10
|
| 112 |
-
try:
|
| 113 |
-
while True:
|
| 114 |
-
await asyncio.sleep(update_interval)
|
| 115 |
-
stream_info = ACTIVE_STREAMS.get(stream_id)
|
| 116 |
-
if not stream_info:
|
| 117 |
-
for rec in RECENT_STREAMS:
|
| 118 |
-
if rec.get("stream_id") == stream_id:
|
| 119 |
-
final_bytes = rec.get("total_bytes", 0)
|
| 120 |
-
delta = final_bytes - last_tracked_bytes
|
| 121 |
-
if delta > 0:
|
| 122 |
-
try:
|
| 123 |
-
await db.update_token_usage(token, delta)
|
| 124 |
-
except Exception as e:
|
| 125 |
-
LOGGER.error(f"Final usage update failed: {e}")
|
| 126 |
-
break
|
| 127 |
-
return
|
| 128 |
-
current_bytes = stream_info.get("total_bytes", 0)
|
| 129 |
-
delta = current_bytes - last_tracked_bytes
|
| 130 |
-
if delta > 0:
|
| 131 |
-
try:
|
| 132 |
-
await db.update_token_usage(token, delta)
|
| 133 |
-
last_tracked_bytes = current_bytes
|
| 134 |
-
except Exception as e:
|
| 135 |
-
LOGGER.error(f"Periodic usage update failed: {e}")
|
| 136 |
-
if daily_limit_gb and daily_limit_gb > 0:
|
| 137 |
-
current_daily_gb = (initial_daily_bytes + current_bytes) / (1024 ** 3)
|
| 138 |
-
if current_daily_gb >= daily_limit_gb:
|
| 139 |
-
LOGGER.debug(f"Daily limit reached for token, stream {stream_id} may be blocked by verify_token")
|
| 140 |
-
if monthly_limit_gb and monthly_limit_gb > 0:
|
| 141 |
-
current_monthly_gb = (initial_monthly_bytes + current_bytes) / (1024 ** 3)
|
| 142 |
-
if current_monthly_gb >= monthly_limit_gb:
|
| 143 |
-
LOGGER.debug(f"Monthly limit reached for token, stream {stream_id} may be blocked by verify_token")
|
| 144 |
-
except asyncio.CancelledError:
|
| 145 |
-
stream_info = ACTIVE_STREAMS.get(stream_id)
|
| 146 |
-
if stream_info:
|
| 147 |
-
current_bytes = stream_info.get("total_bytes", 0)
|
| 148 |
-
delta = current_bytes - last_tracked_bytes
|
| 149 |
-
if delta > 0:
|
| 150 |
-
try:
|
| 151 |
-
await db.update_token_usage(token, delta)
|
| 152 |
-
LOGGER.info(f"Cancelled - final update for {stream_id}: {delta} bytes")
|
| 153 |
-
except Exception as e:
|
| 154 |
-
LOGGER.error(f"Cancelled usage update failed: {e}")
|
| 155 |
-
|
| 156 |
@router.get("/dl/{token}/{id}/{name}")
|
| 157 |
@router.head("/dl/{token}/{id}/{name}")
|
| 158 |
async def stream_handler(request: Request, token: str, id: str, name: str, token_data: dict = Depends(verify_token),):
|
|
@@ -243,7 +189,7 @@ async def media_streamer(request: Request, chat_id: int, msg_id: int, token: str
|
|
| 243 |
extra_clients=extra_clients_for_stream,
|
| 244 |
)
|
| 245 |
|
| 246 |
-
asyncio.create_task(
|
| 247 |
|
| 248 |
file_name = file_id.file_name or f"{secrets.token_hex(4)}.bin"
|
| 249 |
mime_type = file_id.mime_type or mimetypes.guess_type(file_name)[0] or "application/octet-stream"
|
|
|
|
| 10 |
from fastapi.responses import Response as PlainResponse, StreamingResponse, JSONResponse
|
| 11 |
from collections import deque
|
| 12 |
from Backend import db
|
|
|
|
| 13 |
from Backend.logger import LOGGER
|
| 14 |
from Backend.helper.encrypt import decode_string
|
| 15 |
+
from Backend.helper.pyro import track_usage
|
| 16 |
from Backend.helper.custom_dl import ByteStreamer, ACTIVE_STREAMS, RECENT_STREAMS
|
| 17 |
from Backend.pyrofork.bot import work_loads, multi_clients, client_dc_map, client_failures, client_avg_mbps
|
| 18 |
from Backend.fastapi.security.tokens import verify_token
|
|
|
|
| 99 |
value = min(max(ceil(client_count / 5), 1), 5)
|
| 100 |
return value, value
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
@router.get("/dl/{token}/{id}/{name}")
|
| 103 |
@router.head("/dl/{token}/{id}/{name}")
|
| 104 |
async def stream_handler(request: Request, token: str, id: str, name: str, token_data: dict = Depends(verify_token),):
|
|
|
|
| 189 |
extra_clients=extra_clients_for_stream,
|
| 190 |
)
|
| 191 |
|
| 192 |
+
asyncio.create_task(track_usage(stream_id, token, token_data))
|
| 193 |
|
| 194 |
file_name = file_id.file_name or f"{secrets.token_hex(4)}.bin"
|
| 195 |
mime_type = file_id.mime_type or mimetypes.guess_type(file_name)[0] or "application/octet-stream"
|