feat: 将工具更名为 VideosDownTools,并完善黄豆登录与清单
This commit is contained in:
@@ -3,23 +3,45 @@ 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
|
||||
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:
|
||||
info = api.user_info()
|
||||
except RuntimeError:
|
||||
info = raw
|
||||
profile = _profile(info if isinstance(info, dict) else raw)
|
||||
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}
|
||||
@@ -36,17 +58,76 @@ def current(token: str) -> dict:
|
||||
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
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user