ForcePilot/backend/package/yuxi/channel/extensions/bilibili/probe.py

38 lines
1.2 KiB
Python
Raw Normal View History

from __future__ import annotations
import time
import httpx
class BilibiliProbe:
NAV_URL = "https://api.bilibili.com/x/web-interface/nav"
async def probe(self, account: dict) -> dict:
start = time.time()
try:
async with httpx.AsyncClient() as client:
resp = await client.get(
self.NAV_URL,
cookies={
"SESSDATA": account.get("sessdata", ""),
"bili_jct": account.get("bili_jct", ""),
"DedeUserID": account.get("dedeuserid", ""),
},
)
data = resp.json()
is_valid = data.get("code") == 0 and data.get("data", {}).get("isLogin")
latency = time.time() - start
return {
"ok": is_valid,
"latency_ms": int(latency * 1000),
"error": data.get("message") if not is_valid else None,
}
except Exception as e:
return {
"ok": False,
"latency_ms": int((time.time() - start) * 1000),
"error": str(e),
}