96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
from server.services.huangdou import api
|
|
from server.services.huangdou.client import TokenExpired, client
|
|
from server.services.huangdou.covers import covers as huangdou_covers
|
|
|
|
|
|
def enter() -> dict:
|
|
try:
|
|
if client.token:
|
|
profile = _profile(api.user_info())
|
|
client.profile = profile
|
|
client.save()
|
|
return {"token": client.token, "profile": profile}
|
|
except TokenExpired:
|
|
client.clear_token()
|
|
except RuntimeError:
|
|
pass
|
|
raw = api.login_device(force_new=False)
|
|
try:
|
|
info = api.user_info()
|
|
except RuntimeError:
|
|
info = raw
|
|
profile = _profile(info if isinstance(info, dict) else raw)
|
|
client.profile = profile
|
|
client.save()
|
|
return {"token": client.token, "profile": profile}
|
|
|
|
|
|
def resume() -> dict:
|
|
if not client.token or not client.profile:
|
|
raise RuntimeError("还没有登录")
|
|
return {"token": client.token, "profile": client.profile}
|
|
|
|
|
|
def current(token: str) -> dict:
|
|
token = (token or "").strip()
|
|
if not token:
|
|
raise RuntimeError("请先登录")
|
|
if token != client.token:
|
|
client.token = token
|
|
try:
|
|
profile = _profile(api.user_info())
|
|
except TokenExpired as exc:
|
|
client.clear_token()
|
|
raise RuntimeError("登录已失效") from exc
|
|
client.profile = profile
|
|
client.save()
|
|
return {"token": client.token, "profile": profile}
|
|
|
|
|
|
def logout(token: str) -> None:
|
|
if token and token == client.token:
|
|
client.clear_token()
|
|
return
|
|
if not token:
|
|
client.clear_token()
|
|
|
|
|
|
def _profile(raw: dict) -> dict:
|
|
data = raw if isinstance(raw, dict) else {}
|
|
vip = _truthy(data.get("is_vip"))
|
|
head = str(data.get("headico") or "").strip()
|
|
return {
|
|
"id": data.get("id") or data.get("user_id") or "",
|
|
"username": data.get("username") or "",
|
|
"nickname": data.get("nickname") or data.get("username") or "黄豆用户",
|
|
"email": data.get("email") or "",
|
|
"phone": data.get("phone") or "",
|
|
"badge": "VIP" if vip else "",
|
|
"vipday": 1 if vip else 0,
|
|
"score": _number(data.get("balance")),
|
|
"watched": _number(data.get("play_num")),
|
|
"rewards": 0,
|
|
"login_day": 0,
|
|
"is_vip": data.get("is_vip") or "",
|
|
"balance": data.get("balance") or "0",
|
|
"group_name": data.get("group_name") or "",
|
|
"group_end_time": data.get("group_end_time") or "",
|
|
"account_type": data.get("account_type") or "",
|
|
"has_login": data.get("has_login") or "",
|
|
"sign": data.get("sign") or "",
|
|
"avatarNumUrl": huangdou_covers.local_url(head) if head else "",
|
|
"headico": head,
|
|
}
|
|
|
|
|
|
def _truthy(value) -> bool:
|
|
text = str(value or "").strip().lower()
|
|
return text in {"1", "true", "y", "yes", "vip"}
|
|
|
|
|
|
def _number(value) -> int:
|
|
try:
|
|
return int(float(str(value or "0")))
|
|
except (TypeError, ValueError):
|
|
return 0
|