feat(douyu): 新增斗鱼查询工作台与失效CK人工重登预算支持 (v0.4.26)

This commit is contained in:
yml
2026-11-30 17:11:14 +08:00
parent 74ceb398b6
commit fdc01826a3
18 changed files with 2863 additions and 37 deletions
@@ -68,6 +68,7 @@ RETIRED_NOISE_TASK_TYPES: tuple[str, ...] = (
#: 只读、看一眼就走,且前端**不**拿它派生账号状态 → 短保留。
NOISE_TASK_TYPES: tuple[str, ...] = (
*RETIRED_NOISE_TASK_TYPES,
"query_assets_and_bind",
"query_change_bind_time",
"query_gold_balance",
"query_xpd_balance",
+1
View File
@@ -237,6 +237,7 @@ class DouyuBatchRunner(
"query_game_name": self._execute_query_game_name,
"query_change_bind_time": self._execute_query_change_bind_time,
"query_account_overview": self._execute_query_account_overview,
"query_assets_and_bind": self._execute_query_assets_and_bind,
"query_gold_balance": self._execute_query_gold_balance,
"query_exchange_records": self._execute_query_exchange_records,
"prefetch_csrf_token": self._execute_prefetch_csrf_token,
@@ -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)
+10 -1
View File
@@ -53,6 +53,7 @@ SUPPORTED_DOUYU_TASK_TYPES = {
"query_change_bind_time": "一键查询换绑时间",
# 跨域聚合查询:鱼翅/积分/精英/电竞/小店/兑换记录/高价值套装是否已拥有
"query_account_overview": "一键查询账号信息",
"query_assets_and_bind": "一键查询鱼翅/积分/换绑时间",
"query_gold_balance": "一键查询鱼翅余额",
"query_exchange_records": "一键查询兑换记录",
"prefetch_csrf_token": "一键获取兑换 CSRF Token",
@@ -69,7 +70,7 @@ SUPPORTED_DOUYU_TASK_TYPES = {
BINDING_POLL_CLEANUP_GRACE_SECONDS = 120
DOUYU_HANDBOOK_SCOPES = {"elite", "esports", "peace"}
DOUYU_HANDBOOK_SCOPES = {"elite", "esports", "peace", "query"}
DOUYU_HANDBOOK_TASK_TYPES = {
"elite": {
"get_bind_qr",
@@ -88,6 +89,7 @@ DOUYU_HANDBOOK_TASK_TYPES = {
"query_exchange_records",
"prefetch_csrf_token",
"query_account_overview",
"query_assets_and_bind",
},
"esports": {
"prepare_esports_bind",
@@ -117,6 +119,13 @@ DOUYU_HANDBOOK_TASK_TYPES = {
"exchange_xpd_goods",
"query_account_overview",
},
"query": {
"query_assets_and_bind",
"query_gold_balance",
"query_points",
"query_change_bind_time",
"query_account_overview",
},
}
+1
View File
@@ -309,6 +309,7 @@ class ExecutionJobWorker:
trigger_source=str(
payload.get("trigger_source", "manual") or "manual"
),
category=str(payload.get("category", "") or ""),
batch_id=current_batch_id,
log_sink=log_sink,
)
+15 -1
View File
@@ -528,7 +528,21 @@ def get_login_quota_usage_summary(
quota_day, quota_hour = _bucket(moment)
setting = db.query(LoginQuotaSetting).filter(LoginQuotaSetting.id == 1).first()
if setting is None:
setting = LoginQuotaSetting(id=1)
setting = LoginQuotaSetting(
id=1,
daily_attempt_limit=1500,
hourly_attempt_limit=100,
per_account_daily_limit=2,
onboarding_daily_limit=150,
relogin_high_value_daily_limit=300,
relogin_ordinary_daily_limit=500,
relogin_protected_daily_limit=500,
lease_refresh_daily_limit=300,
manual_daily_limit=300,
login_concurrency=3,
failure_pause_ratio=40,
enabled=True,
)
protection = load_protection_snapshot(db)
per_account_limit = int(setting.per_account_daily_limit)