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.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() api.remember_api_lines() 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() api.remember_api_lines() sign = claim_daily_sign() return {"token": client.token, "profile": client.profile, "sign": sign} 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() sign = claim_daily_sign() return {"token": client.token, "profile": client.profile, "sign": sign} def claim_daily_sign() -> dict | None: if not client.account_token: return None try: status = api.user_sign() except (TokenExpired, RuntimeError): return None if _signed_today(status.get("today_signed") if isinstance(status, dict) else None): return None try: result = api.user_do_sign() except (TokenExpired, RuntimeError): return None if not isinstance(result, dict): result = {} balance = result.get("balance") if balance not in (None, "") and isinstance(client.profile, dict): client.profile["balance"] = balance client.profile["score"] = _number(balance) client.save() return {"message": _sign_message(result)} def _signed_today(value) -> bool: if isinstance(value, bool): return value if isinstance(value, (int, float)) and not isinstance(value, bool): return value != 0 text = str(value or "").strip().lower() return text in {"y", "1", "true", "yes"} def _sign_message(result: dict) -> str: name = str(result.get("prize_name") or "").strip() try: amount = int(float(str(result.get("prize_num") or "0"))) except (TypeError, ValueError): amount = 0 if amount > 0 and name: return f"签到成功,获得 {name} × {amount}" if amount > 0: return f"签到成功,获得 {amount} 积分" text = str(result.get("msg") or "").strip() return text or "签到成功" 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 _renew_account() -> bool: from server.services.huangdou.accounts import load username = client.account_username.strip() if not username: return False password = "" for item in load(): if item.get("username") == username: password = str(item.get("password") or "") break if not password: return False try: api.login_username(username, password) except Exception: return False return bool(client.account_token) def _renew_guest() -> bool: if not client.guest_device_id: return False try: api.login_device(force_new=False) except Exception: return False return bool(client.token) client.renew_account = _renew_account client.renew_guest = _renew_guest def _number(value) -> int: try: return int(float(str(value or "0"))) except (TypeError, ValueError): return 0