177 lines
5.7 KiB
Python
177 lines
5.7 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(host: str = "") -> dict:
|
|
if host:
|
|
client.set_host(host)
|
|
client.account_token = ""
|
|
client.account_username = ""
|
|
client.roll_guest_fingerprint()
|
|
raw = api.login_device(force_new=False)
|
|
guest = _user_from_login(raw)
|
|
if guest:
|
|
profile = _profile(guest)
|
|
profile["has_login"] = ""
|
|
profile["host"] = client.host
|
|
else:
|
|
profile = public_profile(None)
|
|
client.profile = profile
|
|
client.save()
|
|
return {"token": client.token, "profile": profile}
|
|
|
|
|
|
def login_account(username: str, password: str, rotate_fingerprint: bool = False) -> dict:
|
|
username = (username or "").strip()
|
|
password = password or ""
|
|
if not username or not password:
|
|
raise RuntimeError("请填写账号和密码")
|
|
if not client.token:
|
|
raise RuntimeError("请先进入")
|
|
previous = None
|
|
if rotate_fingerprint:
|
|
previous = (client.device_id, client.session_id, dict(client.device_info), client.account_token)
|
|
client.roll_account_fingerprint()
|
|
try:
|
|
raw = api.login_username(username, password)
|
|
info = _user_from_login(raw) or api.user_info(identity="account")
|
|
except Exception:
|
|
if previous is not None:
|
|
client.device_id, client.session_id, client.device_info, client.account_token = previous
|
|
client.save()
|
|
raise
|
|
profile = public_profile(info if isinstance(info, dict) else {})
|
|
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:
|
|
raise RuntimeError("登录已失效")
|
|
account = _account_info()
|
|
profile = public_profile(account)
|
|
client.profile = profile
|
|
client.save()
|
|
return {"token": client.token, "profile": profile}
|
|
|
|
|
|
def account_balance() -> int:
|
|
if not client.account_token:
|
|
return 0
|
|
try:
|
|
info = api.user_info(identity="account")
|
|
except (TokenExpired, RuntimeError):
|
|
return _number(client.profile.get("balance") if isinstance(client.profile, dict) else 0)
|
|
return _number(info.get("balance") if isinstance(info, dict) else 0)
|
|
|
|
|
|
def account_is_vip() -> bool:
|
|
if not client.account_token:
|
|
return False
|
|
profile = client.profile if isinstance(client.profile, dict) else {}
|
|
return _truthy(profile.get("is_vip")) or bool(profile.get("badge"))
|
|
|
|
|
|
def _user_from_login(data: dict) -> dict:
|
|
if not isinstance(data, dict):
|
|
return {}
|
|
for key in ("user", "userInfo", "user_info", "info"):
|
|
nested = data.get(key)
|
|
if isinstance(nested, dict) and (nested.get("id") or nested.get("username") or nested.get("nickname")):
|
|
return nested
|
|
if data.get("id") or data.get("username") or data.get("nickname"):
|
|
return data
|
|
return {}
|
|
|
|
|
|
def _account_info() -> dict:
|
|
if not client.account_token:
|
|
return {}
|
|
try:
|
|
info = api.user_info(identity="account")
|
|
except TokenExpired:
|
|
client.account_token = ""
|
|
client.save()
|
|
return {}
|
|
except RuntimeError:
|
|
return {}
|
|
return info if isinstance(info, dict) else {}
|
|
|
|
|
|
def public_profile(account: dict | None = None) -> dict:
|
|
if account and client.account_token:
|
|
profile = _profile(account)
|
|
profile["has_login"] = "1"
|
|
profile["username"] = client.account_username or profile.get("username") or ""
|
|
else:
|
|
guest = {}
|
|
if client.token:
|
|
try:
|
|
fetched = api.user_info()
|
|
except (TokenExpired, RuntimeError):
|
|
fetched = {}
|
|
guest = fetched if isinstance(fetched, dict) else {}
|
|
profile = _profile(guest)
|
|
profile["has_login"] = ""
|
|
profile["host"] = client.host
|
|
return 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
|