Spaces:
Runtime error
Runtime error
Update bot/integrations/cf_worker2.py
Browse files
bot/integrations/cf_worker2.py
CHANGED
|
@@ -3,27 +3,70 @@ from bot.config import Workers
|
|
| 3 |
from bot.integrations.http import post_json
|
| 4 |
from bot.core.settings import ROTATE_AFTER
|
| 5 |
|
|
|
|
| 6 |
def _h():
|
| 7 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
async def allow_user(tg_id: int) -> dict:
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
async def disallow_user(tg_id: int) -> dict:
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
async def is_allowed(tg_id: int) -> bool:
|
| 16 |
-
|
|
|
|
| 17 |
return bool(j.get("ok") and j.get("allowed") is True)
|
| 18 |
|
|
|
|
| 19 |
async def pick_profile(tg_id: int, channel_id: str, rotate_after: int = ROTATE_AFTER) -> dict:
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
async def access_token(tg_id: int, profile_id: str) -> dict:
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
async def record_upload(tg_id: int, profile_id: str) -> dict:
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
async def stats_today() -> dict:
|
| 29 |
-
|
|
|
|
|
|
| 3 |
from bot.integrations.http import post_json
|
| 4 |
from bot.core.settings import ROTATE_AFTER
|
| 5 |
|
| 6 |
+
|
| 7 |
def _h():
|
| 8 |
+
return {
|
| 9 |
+
"Authorization": f"Bearer {Workers.HF_API_KEY}",
|
| 10 |
+
"Content-Type": "application/json",
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _unwrap(r: dict) -> dict:
|
| 15 |
+
"""
|
| 16 |
+
Unwrap post_json wrapper:
|
| 17 |
+
{"ok": true, "status": 200, "data": {...}} -> {...}
|
| 18 |
+
"""
|
| 19 |
+
if isinstance(r, dict) and isinstance(r.get("data"), dict):
|
| 20 |
+
data = r["data"]
|
| 21 |
+
if "ok" not in data and "ok" in r:
|
| 22 |
+
data["ok"] = r["ok"]
|
| 23 |
+
return data
|
| 24 |
+
return r
|
| 25 |
+
|
| 26 |
|
| 27 |
async def allow_user(tg_id: int) -> dict:
|
| 28 |
+
r = await post_json(f"{Workers.WORKER2_URL}/api/allow_user", _h(), {"tg_id": str(tg_id)})
|
| 29 |
+
return _unwrap(r)
|
| 30 |
+
|
| 31 |
|
| 32 |
async def disallow_user(tg_id: int) -> dict:
|
| 33 |
+
r = await post_json(f"{Workers.WORKER2_URL}/api/disallow_user", _h(), {"tg_id": str(tg_id)})
|
| 34 |
+
return _unwrap(r)
|
| 35 |
+
|
| 36 |
|
| 37 |
async def is_allowed(tg_id: int) -> bool:
|
| 38 |
+
r = await post_json(f"{Workers.WORKER2_URL}/api/is_allowed", _h(), {"tg_id": str(tg_id)})
|
| 39 |
+
j = _unwrap(r)
|
| 40 |
return bool(j.get("ok") and j.get("allowed") is True)
|
| 41 |
|
| 42 |
+
|
| 43 |
async def pick_profile(tg_id: int, channel_id: str, rotate_after: int = ROTATE_AFTER) -> dict:
|
| 44 |
+
r = await post_json(
|
| 45 |
+
f"{Workers.WORKER2_URL}/api/pick_profile",
|
| 46 |
+
_h(),
|
| 47 |
+
{"tg_id": str(tg_id), "channel_id": str(channel_id), "rotate_after": int(rotate_after)},
|
| 48 |
+
)
|
| 49 |
+
return _unwrap(r)
|
| 50 |
+
|
| 51 |
|
| 52 |
async def access_token(tg_id: int, profile_id: str) -> dict:
|
| 53 |
+
r = await post_json(
|
| 54 |
+
f"{Workers.WORKER2_URL}/api/access_token",
|
| 55 |
+
_h(),
|
| 56 |
+
{"tg_id": str(tg_id), "profile_id": profile_id},
|
| 57 |
+
)
|
| 58 |
+
return _unwrap(r)
|
| 59 |
+
|
| 60 |
|
| 61 |
async def record_upload(tg_id: int, profile_id: str) -> dict:
|
| 62 |
+
r = await post_json(
|
| 63 |
+
f"{Workers.WORKER2_URL}/api/record_upload",
|
| 64 |
+
_h(),
|
| 65 |
+
{"tg_id": str(tg_id), "profile_id": profile_id},
|
| 66 |
+
)
|
| 67 |
+
return _unwrap(r)
|
| 68 |
+
|
| 69 |
|
| 70 |
async def stats_today() -> dict:
|
| 71 |
+
r = await post_json(f"{Workers.WORKER2_URL}/api/stats_today", _h(), {})
|
| 72 |
+
return _unwrap(r)
|