Download Backend/fastapi/security/tokens.py from DevDoCode/stremio: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/spaces/DevDoCode/stremio/resolve/d33f42e5cff488437eb2db2b65d113d2f85109c8/Backend/fastapi/security/tokens.py
- Command line
-
hf download hf://spaces/DevDoCode/stremio@d33f42e5cff488437eb2db2b65d113d2f85109c8/Backend/fastapi/security/tokens.py
-
curl -L -o tokens.py https://huggingface.co/spaces/DevDoCode/stremio/resolve/d33f42e5cff488437eb2db2b65d113d2f85109c8/Backend/fastapi/security/tokens.py
2.54 kB
| from fastapi import HTTPException | |
| from datetime import datetime | |
| from Backend import db | |
| from Backend.config import Telegram | |
| DAILY_LIMIT_VIDEO = "https://bit.ly/3YZFKT5" | |
| MONTHLY_LIMIT_VIDEO = "https://bit.ly/4rfjtgd" | |
| SUBSCRIPTION_EXPIRED_VIDEO = "https://bit.ly/4rfjtgd" | |
| async def verify_token(token: str): | |
| token_data = await db.get_api_token(token) | |
| if not token_data: | |
| raise HTTPException(status_code=401, detail="Invalid or expired API token") | |
| limits = token_data.get("limits", {}) | |
| usage = token_data.get("usage", {}) | |
| token_data["limit_exceeded"] = None | |
| token_data["limit_video"] = None | |
| token_data["subscription_expired"] = False | |
| # --- Subscription expiry check (only when SUBSCRIPTION feature is enabled) --- | |
| if Telegram.SUBSCRIPTION: | |
| user_id = token_data.get("user_id") | |
| if not user_id: | |
| # Token has no linked user — treat as expired (unverified token) | |
| token_data["subscription_expired"] = True | |
| return token_data | |
| user = await db.get_user(int(user_id)) | |
| if not user or user.get("subscription_status") != "active": | |
| token_data["subscription_expired"] = True | |
| return token_data | |
| expiry = user.get("subscription_expiry") | |
| if not expiry: | |
| token_data["subscription_expired"] = True | |
| return token_data | |
| # Compare correctly regardless of timezone awareness | |
| now = datetime.utcnow() | |
| try: | |
| if expiry.tzinfo is not None: | |
| from datetime import timezone | |
| now = datetime.now(timezone.utc) | |
| except AttributeError: | |
| pass | |
| if expiry < now: | |
| token_data["subscription_expired"] = True | |
| return token_data | |
| if daily_limit := limits.get("daily_limit_gb"): | |
| if daily_limit > 0: | |
| current_daily_gb = usage.get("daily", {}).get("bytes", 0) / (1024 ** 3) | |
| if current_daily_gb >= daily_limit: | |
| token_data["limit_exceeded"] = "daily" | |
| token_data["limit_video"] = DAILY_LIMIT_VIDEO | |
| return token_data | |
| if monthly_limit := limits.get("monthly_limit_gb"): | |
| if monthly_limit > 0: | |
| current_monthly_gb = usage.get("monthly", {}).get("bytes", 0) / (1024 ** 3) | |
| if current_monthly_gb >= monthly_limit: | |
| token_data["limit_exceeded"] = "monthly" | |
| token_data["limit_video"] = MONTHLY_LIMIT_VIDEO | |
| return token_data | |
| return token_data | |