feat(douyu): 新增斗鱼查询工作台与失效CK人工重登预算支持 (v0.4.26)
This commit is contained in:
@@ -202,6 +202,11 @@ class OverviewMixin:
|
||||
def _client(self, cookie: str) -> DouyuActivityClient: ...
|
||||
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
|
||||
def _push_log(self, level: str, message: str) -> None: ...
|
||||
def _pick_change_wait_bind_info(
|
||||
self, candidates: list[dict], config: dict
|
||||
) -> dict | None: ...
|
||||
def _bind_snapshot(self, bind_info: dict | None) -> dict[str, Any]: ...
|
||||
def _format_wait_time(self, seconds: int | None) -> str: ...
|
||||
|
||||
def _overview_view(self, task: DouyuTask) -> str:
|
||||
result = task.result if isinstance(task.result, dict) else {}
|
||||
@@ -1083,3 +1088,193 @@ class OverviewMixin:
|
||||
|
||||
logger.info(f"[douyu] 一键查询账号 {account.id}: {message}")
|
||||
self._mark_task(db, task, "success", message, overview)
|
||||
|
||||
def _execute_query_assets_and_bind(
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
"""轻量一键查询:仅实查鱼翅余额、积分(含到期)与精英绑定/换绑时间。"""
|
||||
queried_at = datetime.now(UTC)
|
||||
client = self._client(cookie)
|
||||
errors: dict[str, str] = {}
|
||||
payload: dict[str, Any] = {
|
||||
"queried_at": queried_at.isoformat(),
|
||||
"gold_balance": account.gold_balance,
|
||||
"exchange_balance": account.exchange_balance,
|
||||
"points": account.points,
|
||||
"points_expire_at": (
|
||||
account.points_expire_at.isoformat()
|
||||
if account.points_expire_at
|
||||
else None
|
||||
),
|
||||
"role_bind_status": account.role_bind_status or "unknown",
|
||||
"game_name": account.game_name or "",
|
||||
"change_role_wait_time": None,
|
||||
"role_change_available_at": None,
|
||||
"role_change_official_at": None,
|
||||
"errors": errors,
|
||||
}
|
||||
|
||||
# 1. 鱼翅余额
|
||||
gold_ok = False
|
||||
try:
|
||||
gold = self._refresh_account_gold_balance(client, account)
|
||||
payload["gold_balance"] = gold.get("gold_balance")
|
||||
payload["exchange_balance"] = gold.get("exchange_balance")
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_GOLD,
|
||||
source="douyu_task.query_assets_and_bind",
|
||||
observed_at=queried_at,
|
||||
raw_payload=gold,
|
||||
task_id=task.id,
|
||||
)
|
||||
gold_ok = True
|
||||
except Exception as exc: # noqa: BLE001 外部接口边界需要独立容错
|
||||
errors["gold"] = _err(exc)
|
||||
|
||||
# 2. 积分与到期时间
|
||||
points_ok = False
|
||||
try:
|
||||
ctn = query_csrf(client)
|
||||
points = self._refresh_account_points(client, account, cookie, ctn=ctn)
|
||||
payload["points"] = points.get("points")
|
||||
expire_raw = _ts(_points_expire_at(points))
|
||||
if expire_raw is not None:
|
||||
expire_raw = str(expire_raw)
|
||||
expire_dt = _parse_expire_at(expire_raw)
|
||||
if expire_dt is not None:
|
||||
account.points_expire_at = expire_dt
|
||||
payload["points_expire_at"] = expire_dt.isoformat()
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_POINTS,
|
||||
source="douyu_task.query_assets_and_bind",
|
||||
observed_at=queried_at,
|
||||
raw_payload=points.get("points_query")
|
||||
if isinstance(points.get("points_query"), dict)
|
||||
else points,
|
||||
task_id=task.id,
|
||||
)
|
||||
points_ok = True
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["points"] = _err(exc)
|
||||
|
||||
# 3. 绑定角色与换绑时间(合并本地 7 天锁)
|
||||
bind_ok = False
|
||||
bind_label = ""
|
||||
try:
|
||||
role = resolve_elite_role(client, config)
|
||||
status = str(role.get("status") or "")
|
||||
raw_candidates = role.get("bind_infos") or []
|
||||
if status == ROLE_ERROR:
|
||||
errors["bind"] = ";".join(role.get("errors") or ["查询绑定失败"])[
|
||||
:_ERROR_MAX
|
||||
]
|
||||
else:
|
||||
bind_info = (
|
||||
self._pick_change_wait_bind_info(raw_candidates, config)
|
||||
or (
|
||||
role.get("bind_info")
|
||||
if isinstance(role.get("bind_info"), dict)
|
||||
else None
|
||||
)
|
||||
or (raw_candidates[0] if raw_candidates else {})
|
||||
)
|
||||
snapshot = self._bind_snapshot(bind_info)
|
||||
wait_time = snapshot["change_role_wait_time"]
|
||||
persist_role = {
|
||||
**role,
|
||||
"bind_info": bind_info,
|
||||
"snapshot": snapshot,
|
||||
"role_name": snapshot.get("role_name") or "",
|
||||
"game_channel": snapshot.get("game_channel") or "",
|
||||
"change_role_wait_time": wait_time,
|
||||
"can_change_role": snapshot.get("can_change_role"),
|
||||
"can_change_time": snapshot.get("can_change_time"),
|
||||
}
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
{
|
||||
"probe_id": f"task:{task.id}",
|
||||
"identity": {"uid": account.uid, "username": account.username},
|
||||
"session": {},
|
||||
"elite": {},
|
||||
"eligibility": {},
|
||||
"roles": {"elite": persist_role},
|
||||
"gold": {},
|
||||
"goods": {},
|
||||
"errors": {},
|
||||
},
|
||||
source="douyu_task.query_assets_and_bind",
|
||||
record_event=False,
|
||||
)
|
||||
bind_ok = True
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["bind"] = _err(exc)
|
||||
|
||||
account.updated_at = datetime.now(UTC)
|
||||
payload["role_bind_status"] = account.role_bind_status or "unknown"
|
||||
payload["game_name"] = account.game_name or ""
|
||||
payload["change_role_wait_time"] = (
|
||||
account.change_role_wait_time if account.role_bind_status == "bound" else 0
|
||||
)
|
||||
payload["role_change_available_at"] = (
|
||||
account.role_change_available_at.isoformat()
|
||||
if account.role_change_available_at
|
||||
else None
|
||||
)
|
||||
payload["role_change_official_at"] = (
|
||||
account.role_change_official_at.isoformat()
|
||||
if account.role_change_official_at
|
||||
else None
|
||||
)
|
||||
|
||||
if bind_ok:
|
||||
if account.role_bind_status == "unbound":
|
||||
bind_label = "未绑定角色"
|
||||
elif account.role_bind_status == "bound":
|
||||
available_at = account.role_change_available_at
|
||||
if available_at is not None:
|
||||
aware = (
|
||||
available_at
|
||||
if available_at.tzinfo
|
||||
else available_at.replace(tzinfo=UTC)
|
||||
)
|
||||
if aware > queried_at:
|
||||
rem_sec = int((aware - queried_at).total_seconds())
|
||||
bind_label = f"换绑冷却 {self._format_wait_time(rem_sec)}"
|
||||
else:
|
||||
bind_label = "可换绑"
|
||||
elif (account.change_role_wait_time or 0) > 0:
|
||||
bind_label = (
|
||||
f"换绑冷却 {self._format_wait_time(account.change_role_wait_time)}"
|
||||
)
|
||||
else:
|
||||
bind_label = "可换绑"
|
||||
elif account.role_bind_status == "pending":
|
||||
bind_label = "待确认绑定"
|
||||
else:
|
||||
bind_label = "绑定未知"
|
||||
|
||||
parts = [
|
||||
f"鱼翅 {payload['gold_balance'] if payload['gold_balance'] is not None else '-'}",
|
||||
f"积分 {payload['points'] if payload['points'] is not None else '-'}",
|
||||
]
|
||||
if bind_label:
|
||||
parts.append(bind_label)
|
||||
elif "bind" in errors:
|
||||
parts.append(f"换绑查询失败: {errors['bind']}")
|
||||
message = " · ".join(parts)
|
||||
|
||||
if not (gold_ok or points_ok or bind_ok):
|
||||
fail_msg = ";".join(f"{k}: {v}" for k, v in errors.items()) or "查询失败"
|
||||
self._mark_task(db, task, "failed", fail_msg, payload)
|
||||
return
|
||||
|
||||
if errors:
|
||||
message = f"{message}(部分异常: {', '.join(errors.keys())})"
|
||||
logger.info(f"[douyu] 查询工作台账号 {account.id}: {message}")
|
||||
self._mark_task(db, task, "success", message, payload)
|
||||
|
||||
Reference in New Issue
Block a user