feat(douyu): unify account probes and lifecycle writes
Centralize Douyu client creation, CK session probing, identity refresh, role alias resolution, capability persistence, cache and batch throttling across collaboration, fulfillment, tasks and cookie management. Add the account-name copy action in the collaboration table.
This commit is contained in:
@@ -9,21 +9,24 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
from core.douyu import DouyuActivityClient
|
||||
from core.douyu.cookie_utils import cookie_value
|
||||
|
||||
from .account_binding import binding_snapshot
|
||||
from .douyu_goods_service import serialize_goods
|
||||
|
||||
ERROR_MAX_LENGTH = 240
|
||||
|
||||
|
||||
def activity_client(cookie: str = "") -> DouyuActivityClient:
|
||||
def activity_client(
|
||||
cookie: str = "",
|
||||
*,
|
||||
logger_callback: Callable[[str], None] | None = None,
|
||||
proxies: dict[str, str] | None = None,
|
||||
) -> DouyuActivityClient:
|
||||
"""创建统一活动客户端;空 Cookie 可用于公开商品目录查询。"""
|
||||
return DouyuActivityClient(cookie)
|
||||
return DouyuActivityClient(
|
||||
cookie,
|
||||
logger=logger_callback,
|
||||
proxies=proxies,
|
||||
)
|
||||
|
||||
|
||||
def query_csrf(
|
||||
@@ -68,6 +71,29 @@ def query_xpd_bind_info(
|
||||
return client.xpd_bind_info(act_alias=act_alias)
|
||||
|
||||
|
||||
def query_xpd_embed(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
act_alias: str,
|
||||
rid: str,
|
||||
) -> dict[str, Any]:
|
||||
return client.xpd_embed_query(act_alias=act_alias, rid=rid)
|
||||
|
||||
|
||||
def query_xpd_role(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
embed_query: dict[str, str],
|
||||
act_id: str,
|
||||
rid: str = "",
|
||||
) -> dict[str, Any]:
|
||||
return client.xpd_get_role(
|
||||
embed_query=embed_query,
|
||||
act_id=act_id,
|
||||
rid=rid,
|
||||
)
|
||||
|
||||
|
||||
def query_gold_account(client: DouyuActivityClient) -> dict[str, Any]:
|
||||
return client.gold_account()
|
||||
|
||||
@@ -203,95 +229,8 @@ def query_exchange_orders(
|
||||
return [dict(item) for item in result.get("orders") or [] if isinstance(item, dict)]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DouyuEligibilityResult:
|
||||
elite_user_info: dict[str, Any]
|
||||
points: int
|
||||
points_response: dict[str, Any]
|
||||
limited_goods: list[dict[str, Any]]
|
||||
limited_ids: frozenset[str]
|
||||
|
||||
|
||||
def query_exchange_eligibility(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
cookie: str,
|
||||
manual_id: str,
|
||||
rid: str,
|
||||
uid: str = "",
|
||||
) -> DouyuEligibilityResult:
|
||||
"""一次读取兑换资格需要的精英状态、积分和限兑商品。"""
|
||||
ctn = query_csrf(client)
|
||||
elite = query_elite_user_info(client, manual_id=manual_id, rid=rid)
|
||||
resolved_uid = str(uid or cookie_value(cookie, "acf_uid") or "").strip()
|
||||
points_result = query_points(client, uid=resolved_uid, ctn=ctn)
|
||||
limited = query_limited_goods(client, manual_id=manual_id, rid=rid)
|
||||
limited_ids = {
|
||||
str(item.get("commodityId") or item.get("commodity_id") or "").strip()
|
||||
for item in limited
|
||||
}
|
||||
limited_ids.discard("")
|
||||
return DouyuEligibilityResult(
|
||||
elite_user_info=elite,
|
||||
points=int(points_result.get("points") or 0),
|
||||
points_response=points_result,
|
||||
limited_goods=limited,
|
||||
limited_ids=frozenset(limited_ids),
|
||||
)
|
||||
|
||||
|
||||
def query_account_overview(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
manual_id: str,
|
||||
rid: str,
|
||||
act_alias: str,
|
||||
with_goods: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""账号只读探针,单项失败只进入 errors,不阻断其他查询。"""
|
||||
elite: dict[str, Any] = {}
|
||||
bind: dict[str, Any] = {}
|
||||
gold: dict[str, Any] = {}
|
||||
goods: list[dict[str, Any]] = []
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
try:
|
||||
elite = query_elite_user_info(client, manual_id=manual_id, rid=rid)
|
||||
except Exception as exc: # noqa: BLE001 - 外部接口边界
|
||||
errors["elite"] = _error_text(exc)
|
||||
try:
|
||||
bind = query_bind_info(client, act_alias=act_alias)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["bind"] = _error_text(exc)
|
||||
try:
|
||||
gold = query_gold_account(client)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["gold"] = _error_text(exc)
|
||||
if with_goods:
|
||||
try:
|
||||
goods = query_activity_goods(client, manual_id=manual_id, rid=rid)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["goods"] = _error_text(exc)
|
||||
|
||||
return {
|
||||
"elite": elite,
|
||||
"bind": bind,
|
||||
"bind_snapshot": binding_snapshot(bind),
|
||||
"gold": gold,
|
||||
"goods": goods,
|
||||
"serialized_goods": [serialize_goods(item) for item in goods],
|
||||
"errors": errors,
|
||||
}
|
||||
|
||||
|
||||
def _error_text(exc: BaseException) -> str:
|
||||
return f"{type(exc).__name__}: {exc}"[:ERROR_MAX_LENGTH]
|
||||
|
||||
|
||||
__all__ = [
|
||||
"DouyuEligibilityResult",
|
||||
"activity_client",
|
||||
"query_account_overview",
|
||||
"query_activity_goods",
|
||||
"query_bind_info",
|
||||
"query_csrf",
|
||||
@@ -300,7 +239,6 @@ __all__ = [
|
||||
"query_esports_goods",
|
||||
"query_esports_goods_result",
|
||||
"query_exchange_balance",
|
||||
"query_exchange_eligibility",
|
||||
"query_exchange_orders",
|
||||
"query_exchange_orders_result",
|
||||
"query_exchange_records",
|
||||
@@ -310,4 +248,6 @@ __all__ = [
|
||||
"query_limited_goods_result",
|
||||
"query_points",
|
||||
"query_xpd_bind_info",
|
||||
"query_xpd_embed",
|
||||
"query_xpd_role",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user