refactor(douyu): 统一活动查询与直播商品映射

- 收口斗鱼活动、商品、绑定信息和兑换资格查询
- 工作台改为复用统一客户端、快照与实时商品目录
- 补充活动查询与商品映射测试
This commit is contained in:
yml
2026-11-18 10:59:20 +08:00
parent 1ca768eba5
commit 0220ef995f
15 changed files with 711 additions and 303 deletions
+50 -47
View File
@@ -35,6 +35,15 @@ from ..models import (
)
from .audit_service import record_audit
from .collab_common import apply_order_visibility, count_archived
from .douyu_activity_query import (
activity_client,
query_account_overview,
query_activity_goods,
query_elite_user_info,
query_exchange_orders,
query_exchange_records,
query_gold_account,
)
from .douyu_goods_service import (
serialize_live_goods as _serialize_live_goods,
)
@@ -251,7 +260,7 @@ def _client_for_account(account: Account) -> DouyuActivityClient:
cookie = str(account.cookie or "").strip()
if not cookie:
raise CollabError("该账号没有 CK", code="account_no_cookie")
return DouyuActivityClient(cookie)
return activity_client(cookie)
def serialize_live_goods(goods: list[dict[str, Any]]) -> list[dict[str, Any]]:
@@ -279,35 +288,27 @@ def _probe_with_client(
}
active_client = client or _client_for_account(account)
errors: list[str] = []
elite: dict[str, Any] = {}
bind: dict[str, Any] = {}
gold: dict[str, Any] = {}
goods: list[dict[str, Any]] = []
try:
elite = active_client.elite_user_info(manual_id=manual_id, rid=rid)
except Exception as exc: # noqa: BLE001 - 外部接口边界
errors.append(f"手册状态: {exc}")
try:
bind = active_client.bind_info(act_alias=act_alias)
except Exception as exc: # noqa: BLE001
errors.append(f"绑定角色: {exc}")
try:
gold = active_client.gold_account()
except Exception as exc: # noqa: BLE001
errors.append(f"鱼翅余额: {exc}")
if with_goods:
try:
goods = (
active_client.list_goods(manual_id=manual_id, rid=rid).get("goods")
or []
)
except Exception as exc: # noqa: BLE001
errors.append(f"活动商品: {exc}")
overview = query_account_overview(
active_client,
manual_id=manual_id,
rid=rid,
act_alias=act_alias,
with_goods=with_goods,
)
elite = overview["elite"]
bind = overview["bind"]
gold = overview["gold"]
goods = overview["serialized_goods"]
error_labels = {
"elite": "手册状态",
"bind": "绑定角色",
"gold": "鱼翅余额",
"goods": "活动商品",
}
errors = [
f"{error_labels.get(key, key)}: {value}"
for key, value in overview["errors"].items()
]
manual_type = _to_int(elite.get("manual_type"))
manual_score = _to_int(elite.get("manual_score"))
@@ -343,7 +344,7 @@ def _probe_with_client(
"bind_game_role": bind_role,
"bind_game_channel": bind_channel,
"fish_fin_balance": _to_int(gold.get("gold")),
"goods": serialize_live_goods(goods),
"goods": goods,
"goods_count": len(goods),
}
@@ -1121,31 +1122,33 @@ def query_activity(
records: list[dict[str, Any]] = []
orders: list[dict[str, Any]] = []
try:
elite = active_client.elite_user_info(
manual_id=str(config.manual_id or ""), rid=str(config.rid or "")
elite = query_elite_user_info(
active_client,
manual_id=str(config.manual_id or ""),
rid=str(config.rid or ""),
)
except Exception as exc: # noqa: BLE001
errors.append(f"积分: {exc}")
try:
gold = active_client.gold_account()
gold = query_gold_account(active_client)
except Exception as exc: # noqa: BLE001
errors.append(f"鱼翅: {exc}")
if include_records:
try:
records = (
active_client.exchange_records(
manual_id=str(config.manual_id or ""), limit=100, offset=0
).get("records")
or []
records = query_exchange_records(
active_client,
manual_id=str(config.manual_id or ""),
limit=100,
offset=0,
)
except Exception as exc: # noqa: BLE001
errors.append(f"兑换记录: {exc}")
try:
orders = (
active_client.exchange_orders(
manual_id=str(config.manual_id or ""), page=1, page_size=10
).get("orders")
or []
orders = query_exchange_orders(
active_client,
manual_id=str(config.manual_id or ""),
page=1,
page_size=10,
)
except Exception as exc: # noqa: BLE001
errors.append(f"锁单: {exc}")
@@ -1634,10 +1637,9 @@ def refresh_activity_goods(
if account is None:
raise CollabError("没有带 CK 的斗鱼账号,无法刷新活动商品", code="no_account")
active_client = client or _client_for_account(account)
result = active_client.list_goods(
manual_id=str(config.manual_id or ""), rid=str(config.rid or "")
goods = query_activity_goods(
active_client, manual_id=str(config.manual_id or ""), rid=str(config.rid or "")
)
goods = result.get("goods") or []
upsert_goods_snapshot(db, goods, commit=False)
record_audit(
db,
@@ -2226,6 +2228,7 @@ def export_cks(
)
continue
assert account is not None
ck_status = str(account.ck_check_status or "")
items.append(
{