feat(douyu): 和平小店扭蛋碎片查询 + 深色模式修复 + 任务状态 WS 即时推送
- 碎片: 新增 query_xpd_fragments 任务, 复用 e语言 gamecoin 接口取扭蛋币 jb2, 账号已存角色回退(规避 Livelink 风控), accounts.xpd_fragments 字段与迁移 - 列表: 和平小店移除鱼翅/限制兑换/换绑时间列, 新增扭蛋碎片列; 操作区硬编码白底改用 theme token, 深色模式适配(三个手册共用) - 轮询: 任务状态经 WS(level=task)即时推送(二维码/绑定进度即时展示), pending/running/终态全覆盖, REST/WS 载荷统一 douyu_task_payload, raw 快照递归剥离, HTTP 轮询降频 5s/30s 兜底
This commit is contained in:
@@ -13,8 +13,7 @@ from core.douyu.cookie_utils import cookie_value
|
||||
from ..models import Account, DouyuConfig, DouyuTask, LoginTask
|
||||
|
||||
|
||||
SUPPORTED_DOUYU_TASK_TYPES = {
|
||||
"get_bind_qr": "获取绑定二维码",
|
||||
SUPPORTED_DOUYU_TASK_TYPES = { "get_bind_qr": "获取绑定二维码",
|
||||
"confirm_bind": "确认绑定",
|
||||
"create_elite_qr": "开通精英宝典30",
|
||||
"prepare_esports_bind": "绑定电竞手册角色",
|
||||
@@ -44,11 +43,11 @@ SUPPORTED_DOUYU_TASK_TYPES = {
|
||||
"query_xpd_role": "查询小店绑定角色",
|
||||
"refresh_xpd_goods": "刷新小店商品列表",
|
||||
"query_xpd_balance": "查询小店点券余额",
|
||||
"query_xpd_fragments": "查询小店扭蛋碎片",
|
||||
}
|
||||
|
||||
|
||||
DOUYU_CONFIG_DEFAULTS = {
|
||||
"manual_id": "G4KA4Qnz4LDp7",
|
||||
DOUYU_CONFIG_DEFAULTS = { "manual_id": "G4KA4Qnz4LDp7",
|
||||
"rid": "9263298",
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
@@ -223,3 +222,121 @@ def cleanup_orphan_douyu_tasks(
|
||||
task.finished_at = now
|
||||
db.commit()
|
||||
return len(tasks)
|
||||
|
||||
|
||||
def slim_douyu_goods(goods: object) -> object:
|
||||
"""保留兑换图片和列表展示需要的商品小字段。"""
|
||||
if not isinstance(goods, dict):
|
||||
return None
|
||||
return {
|
||||
key: value
|
||||
for key, value in goods.items()
|
||||
if key in {"commodityId", "commodity_id", "commodityName", "name", "webPic", "pic", "score", "status"}
|
||||
}
|
||||
|
||||
|
||||
def slim_douyu_limited_goods(goods: object) -> list[dict[str, object]]:
|
||||
"""限兑列表只返回前几个商品名,避免任务列表携带完整原始数组。"""
|
||||
if not isinstance(goods, list):
|
||||
return []
|
||||
result: list[dict[str, object]] = []
|
||||
for item in goods[:5]:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
result.append({
|
||||
key: value
|
||||
for key, value in item.items()
|
||||
if key in {"commodityId", "commodity_id", "commodityName", "name"}
|
||||
})
|
||||
return result
|
||||
|
||||
|
||||
def strip_douyu_raw_snapshots(value: object) -> object:
|
||||
"""递归移除任务结果中的原始接口快照。"""
|
||||
if isinstance(value, dict):
|
||||
return {
|
||||
key: strip_douyu_raw_snapshots(item)
|
||||
for key, item in value.items()
|
||||
if key != "raw"
|
||||
}
|
||||
if isinstance(value, list):
|
||||
return [strip_douyu_raw_snapshots(item) for item in value]
|
||||
return value
|
||||
|
||||
|
||||
def sanitize_douyu_task_result(result: dict | None, task_type: str, *, include_detail: bool = False) -> dict | None:
|
||||
"""列表/实时推送接口剥离原始快照与大数组;详情接口保留完整 result。"""
|
||||
if not isinstance(result, dict):
|
||||
return result
|
||||
if include_detail:
|
||||
return result
|
||||
|
||||
data = strip_douyu_raw_snapshots(result)
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
for key in (
|
||||
"bind_info",
|
||||
"before_bind_info",
|
||||
"bind_candidates",
|
||||
"cooldown_bind_info",
|
||||
"after_bind_info",
|
||||
"activity_bind_snapshot",
|
||||
"activity_bind_info",
|
||||
"points_query",
|
||||
"exchange_balance_query",
|
||||
"query_act_aliases",
|
||||
"records",
|
||||
):
|
||||
data.pop(key, None)
|
||||
|
||||
# 和平小店任务 result 内嵌 role(含 gameOpenId 等),列表接口剥离其原始快照
|
||||
role = data.get("role")
|
||||
if isinstance(role, dict):
|
||||
data["role"] = strip_douyu_raw_snapshots(role)
|
||||
|
||||
goods = data.get("goods")
|
||||
if isinstance(goods, list):
|
||||
data.pop("goods", None)
|
||||
elif isinstance(goods, dict):
|
||||
data["goods"] = slim_douyu_goods(goods)
|
||||
|
||||
if "limited_goods" in data:
|
||||
data["limited_goods"] = slim_douyu_limited_goods(data.get("limited_goods"))
|
||||
|
||||
if task_type not in {"get_bind_qr", "prepare_esports_bind", "get_esports_bind_qr", "get_xpd_bind_qr"}:
|
||||
data.pop("url", None)
|
||||
if task_type not in {"create_elite_qr", "create_esports_qr", "create_gold_qr"}:
|
||||
data.pop("pay_url", None)
|
||||
return data
|
||||
|
||||
|
||||
def douyu_task_payload(task: DouyuTask, *, include_detail: bool = False) -> dict:
|
||||
"""生成 REST 与 WebSocket 共用的斗鱼任务载荷。"""
|
||||
account = task.account
|
||||
|
||||
def isoformat(value: datetime | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
if value.tzinfo is None:
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
return value.isoformat()
|
||||
|
||||
return {
|
||||
"id": task.id,
|
||||
"batch_id": task.batch_id,
|
||||
"account_id": task.account_id,
|
||||
"account_username": (account.username if account else "") or "",
|
||||
"account_uid": (account.uid if account else "") or "",
|
||||
"account_nickname": (account.nickname if account else "") or "",
|
||||
"task_type": task.task_type or "",
|
||||
"status": task.status or "",
|
||||
"message": task.message or "",
|
||||
"result": sanitize_douyu_task_result(
|
||||
task.result if isinstance(task.result, dict) else None,
|
||||
task.task_type or "",
|
||||
include_detail=include_detail,
|
||||
),
|
||||
"created_by": task.created_by,
|
||||
"created_at": isoformat(task.created_at),
|
||||
"finished_at": isoformat(task.finished_at),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user