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:
@@ -25,7 +25,16 @@ from .account_binding import (
|
||||
set_bind_status,
|
||||
)
|
||||
from .business_events import record_bind
|
||||
from .douyu_activity_query import query_bind_info, query_esports_bind_info
|
||||
from .douyu_account_probe import (
|
||||
ROLE_BOUND,
|
||||
ROLE_ERROR,
|
||||
ROLE_PENDING,
|
||||
ROLE_UNKNOWN,
|
||||
persist_probe_result,
|
||||
query_elite_bind_candidates,
|
||||
resolve_elite_role,
|
||||
)
|
||||
from .douyu_activity_query import query_esports_bind_info
|
||||
|
||||
DOUYU_LEGACY_BIND_ACT_ALIAS = "20250213NQCYX"
|
||||
DOUYU_BIND_ROLE_POLL_SECONDS = 600
|
||||
@@ -252,21 +261,15 @@ class BindMixin:
|
||||
aliases: list[str],
|
||||
) -> list[dict]:
|
||||
"""按多个 actAlias 查询绑定信息,保留成功结果。"""
|
||||
results: list[dict] = []
|
||||
for alias in aliases:
|
||||
if not alias:
|
||||
continue
|
||||
try:
|
||||
info = query_bind_info(client, act_alias=alias, v2=True)
|
||||
except DouyuActivityError as exc:
|
||||
self._push_log("warning", f"查询绑定信息失败 act={alias}: {exc}")
|
||||
continue
|
||||
info = {**info, "act_alias": alias}
|
||||
results, errors = query_elite_bind_candidates(client, aliases)
|
||||
for error in errors:
|
||||
self._push_log("warning", f"查询绑定信息失败 {error}")
|
||||
for info in results:
|
||||
self._push_log(
|
||||
"info",
|
||||
f"绑定信息 act={alias} {self._format_bind_summary(info)}",
|
||||
"绑定信息 "
|
||||
f"act={info.get('act_alias') or '-'} {self._format_bind_summary(info)}",
|
||||
)
|
||||
results.append(info)
|
||||
return results
|
||||
|
||||
def _role_alias_priority(self, config: dict, primary_alias: str = "") -> list[str]:
|
||||
@@ -1270,41 +1273,17 @@ class BindMixin:
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
client = self._client(cookie)
|
||||
query_aliases = self._query_bind_act_aliases(config)
|
||||
if not query_aliases:
|
||||
self._mark_task(db, task, "failed", "请先配置绑定活动 actAlias")
|
||||
return
|
||||
candidates = self._fetch_bind_info_candidates(client, query_aliases)
|
||||
if not candidates:
|
||||
self._mark_task(
|
||||
db, task, "failed", "查询绑定信息失败,请检查 Cookie 或 actAlias"
|
||||
)
|
||||
return
|
||||
|
||||
# 1) 优先“已生效绑定”角色(bound_act=1 且有角色名,通常是活动 alias)。
|
||||
# 避免把扫码后未确认的新角色当成当前绑定结果。
|
||||
bound_info = self._pick_current_bound_info(candidates, config)
|
||||
# 2) 待确认角色统一交给共享状态机判断。cjm 中与当前绑定同名的残留
|
||||
# 不算 pending;同名但活动状态发生 needBindAct 转换才算 pending。
|
||||
pending_info = self._pick_pending_role_info(
|
||||
candidates,
|
||||
prefer_aliases=self._role_alias_priority(config),
|
||||
baseline_role_name=str((bound_info or {}).get("role_name") or ""),
|
||||
baseline_is_bound_act=self._is_bound_act(bound_info),
|
||||
baseline_info=bound_info,
|
||||
)
|
||||
pending_role = str((pending_info or {}).get("role_name") or "").strip()
|
||||
bound_role = str(bound_info.get("role_name") or "") if bound_info else ""
|
||||
has_pending = pending_info is not None and bool(pending_role)
|
||||
|
||||
# 查询结果角色 = 已生效绑定;首次绑定(无绑定)时回退待确认角色
|
||||
role = resolve_elite_role(client, config)
|
||||
status = str(role.get("status") or "")
|
||||
bind_info = (
|
||||
bound_info if bound_info else (pending_info if has_pending else None)
|
||||
role.get("bind_info") if isinstance(role.get("bind_info"), dict) else {}
|
||||
)
|
||||
snapshot = self._bind_snapshot(bind_info) if bind_info else {}
|
||||
role_name = snapshot.get("role_name") or ""
|
||||
is_bound = snapshot.get("is_bound_act", False)
|
||||
source_alias = str((bind_info or {}).get("act_alias") or "")
|
||||
snapshot = (
|
||||
role.get("snapshot") if isinstance(role.get("snapshot"), dict) else {}
|
||||
)
|
||||
role_name = str(role.get("role_name") or "")
|
||||
pending_role = str(role.get("pending_role_name") or "")
|
||||
source_alias = str(role.get("source_alias") or "")
|
||||
summary = (
|
||||
f"act={source_alias or '-'} {self._format_bind_summary(bind_info)}"
|
||||
if bind_info
|
||||
@@ -1313,54 +1292,70 @@ class BindMixin:
|
||||
self._push_log(
|
||||
"info",
|
||||
"查询角色 "
|
||||
f"aliases={','.join(query_aliases)} bound_hit={(bound_info or {}).get('act_alias') or '-'} "
|
||||
f"pending_hit={(pending_info or {}).get('act_alias') or '-'} "
|
||||
f"aliases={','.join(role.get('query_act_aliases') or [])} "
|
||||
f"status={status} source={source_alias or '-'} "
|
||||
f"{self._format_bind_summary(bind_info) if bind_info else 'role=- bound_act=-'}",
|
||||
)
|
||||
# 只有已生效绑定才写账号表;待确认角色不污染 game_name
|
||||
if bind_info and is_bound:
|
||||
self._apply_bind_info_to_account(account, bind_info)
|
||||
else:
|
||||
account.updated_at = datetime.now(UTC)
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
{
|
||||
"probe_id": f"task:{task.id}",
|
||||
"identity": {"uid": account.uid, "username": account.username},
|
||||
"session": {},
|
||||
"elite": {},
|
||||
"eligibility": {},
|
||||
"roles": {"elite": role},
|
||||
"gold": {},
|
||||
"goods": {},
|
||||
"errors": {"elite_role": ";".join(role.get("errors") or [])}
|
||||
if role.get("errors")
|
||||
else {},
|
||||
},
|
||||
source="douyu_task.query_game_name",
|
||||
record_event=False,
|
||||
)
|
||||
result = {
|
||||
**snapshot,
|
||||
"act_alias": source_alias,
|
||||
"query_act_alias": source_alias,
|
||||
"query_act_aliases": query_aliases,
|
||||
**snapshot,
|
||||
"bind_ready_for_confirm": has_pending,
|
||||
"bind_confirmed": is_bound,
|
||||
"query_act_aliases": role.get("query_act_aliases") or [],
|
||||
"role_status": status,
|
||||
"role_name": role_name,
|
||||
"bind_ready_for_confirm": status == ROLE_PENDING,
|
||||
"bind_confirmed": status == ROLE_BOUND,
|
||||
"bind_phase": (
|
||||
"confirmed"
|
||||
if is_bound
|
||||
if status == ROLE_BOUND
|
||||
else ("role_ready" if role_name else "waiting_role")
|
||||
),
|
||||
"bind_summary": summary,
|
||||
"bind_candidates": [
|
||||
{
|
||||
"act_alias": item.get("act_alias"),
|
||||
"role_name": item.get("role_name"),
|
||||
"is_bound_act": self._is_bound_act(item),
|
||||
}
|
||||
for item in candidates
|
||||
],
|
||||
"bind_candidates": role.get("candidates") or [],
|
||||
"errors": role.get("errors") or [],
|
||||
}
|
||||
# 已绑定角色之外另有待确认新角色时,单独字段展示,不覆盖 role_name
|
||||
if has_pending and bound_info and pending_role != bound_role:
|
||||
if pending_role:
|
||||
result["pending_role_name"] = pending_role
|
||||
result["pending_area_name"] = str(pending_info.get("area_name") or "")
|
||||
result["pending_plat_name"] = str(pending_info.get("plat_name") or "")
|
||||
if not bind_info:
|
||||
message = f"未获取到游戏名 | {summary}"
|
||||
elif is_bound:
|
||||
if has_pending and pending_role != bound_role:
|
||||
message = (
|
||||
f"当前已绑定: {bound_role},待确认: {pending_role} | {summary}"
|
||||
)
|
||||
else:
|
||||
message = f"当前已绑定: {bound_role} | {summary}"
|
||||
else:
|
||||
result["pending_area_name"] = role.get("pending_area_name") or ""
|
||||
result["pending_plat_name"] = role.get("pending_plat_name") or ""
|
||||
if status == ROLE_ERROR:
|
||||
message = f"查询角色失败: {';'.join(role.get('errors') or ['未知错误'])}"
|
||||
elif status == ROLE_UNKNOWN:
|
||||
message = "角色状态无法确认,已保留上次有效结果"
|
||||
elif status == ROLE_BOUND:
|
||||
message = f"当前已绑定: {role_name} | {summary}"
|
||||
elif status == ROLE_PENDING:
|
||||
message = f"待确认角色: {role_name} | {summary}"
|
||||
self._mark_task(db, task, "success" if role_name else "failed", message, result)
|
||||
else:
|
||||
message = f"未获取到游戏名 | {summary}"
|
||||
self._mark_task(
|
||||
db,
|
||||
task,
|
||||
"success"
|
||||
if status in {ROLE_BOUND, ROLE_PENDING} and bool(role_name)
|
||||
else "failed",
|
||||
message,
|
||||
result,
|
||||
)
|
||||
|
||||
def _pick_change_wait_bind_info(
|
||||
self, candidates: list[dict], config: dict
|
||||
@@ -1387,39 +1382,74 @@ class BindMixin:
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
client = self._client(cookie)
|
||||
query_aliases = self._query_bind_act_aliases(config)
|
||||
if not query_aliases:
|
||||
self._mark_task(db, task, "failed", "请先配置绑定活动 actAlias")
|
||||
return
|
||||
candidates = self._fetch_bind_info_candidates(client, query_aliases)
|
||||
if not candidates:
|
||||
role = resolve_elite_role(client, config)
|
||||
status = str(role.get("status") or "")
|
||||
query_aliases = role.get("query_act_aliases") or []
|
||||
raw_candidates = role.get("bind_infos") or []
|
||||
if status == ROLE_ERROR:
|
||||
self._mark_task(
|
||||
db, task, "failed", "查询绑定信息失败,请检查 Cookie 或 actAlias"
|
||||
db,
|
||||
task,
|
||||
"failed",
|
||||
f"查询绑定信息失败: {';'.join(role.get('errors') or ['未知错误'])}",
|
||||
)
|
||||
return
|
||||
for item in candidates:
|
||||
for item in raw_candidates:
|
||||
self._push_log(
|
||||
"info",
|
||||
f"查询换绑时间候选 act={item.get('act_alias') or '-'} "
|
||||
f"{self._format_bind_summary(item)}",
|
||||
)
|
||||
current_bound = self._pick_current_bound_info(candidates, config)
|
||||
pending_info = self._pick_pending_role_info(
|
||||
candidates,
|
||||
prefer_aliases=self._role_alias_priority(config),
|
||||
baseline_role_name=str((current_bound or {}).get("role_name") or ""),
|
||||
baseline_is_bound_act=self._is_bound_act(current_bound),
|
||||
baseline_info=current_bound,
|
||||
)
|
||||
pending_role = str((pending_info or {}).get("role_name") or "").strip()
|
||||
# 换绑时间看“当前已绑定”活动态,不要优先 cjm(cjm 常无 changeRoleWaitTime)
|
||||
action_aliases = self._action_act_aliases(config)
|
||||
bind_info = next(
|
||||
(
|
||||
item
|
||||
for alias in action_aliases
|
||||
for item in raw_candidates
|
||||
if str(item.get("act_alias") or "") == alias
|
||||
and item.get("change_role_wait_time") is not None
|
||||
),
|
||||
None,
|
||||
)
|
||||
bind_info = (
|
||||
self._pick_change_wait_bind_info(candidates, config) or candidates[0]
|
||||
bind_info
|
||||
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"]
|
||||
account.change_role_wait_time = wait_time
|
||||
account.updated_at = datetime.now(UTC)
|
||||
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_change_bind_time",
|
||||
record_event=False,
|
||||
)
|
||||
source_alias = str(bind_info.get("act_alias") or "")
|
||||
wait_text = self._format_wait_time(wait_time)
|
||||
can_change = snapshot["can_change_role"]
|
||||
@@ -1442,31 +1472,16 @@ class BindMixin:
|
||||
"query_act_aliases": query_aliases,
|
||||
**snapshot,
|
||||
"change_role_wait_text": wait_text,
|
||||
# 待确认语义统一由 _pick_pending_role_info 决定;这里不再根据
|
||||
# “只是有角色名且 bindAct=0”自行推断。
|
||||
"bind_ready_for_confirm": pending_info is not None,
|
||||
"role_status": status,
|
||||
"bind_ready_for_confirm": status == ROLE_PENDING,
|
||||
"bind_confirmed": snapshot["is_bound_act"],
|
||||
"bind_summary": f"act={source_alias or '-'} {self._format_bind_summary(bind_info)}",
|
||||
"bind_candidates": [
|
||||
{
|
||||
"act_alias": item.get("act_alias"),
|
||||
"role_name": item.get("role_name"),
|
||||
"is_bound_act": self._is_bound_act(item),
|
||||
"change_role_wait_time": self._to_int(
|
||||
item.get("change_role_wait_time")
|
||||
),
|
||||
"can_change_role": item.get("can_change_role"),
|
||||
}
|
||||
for item in candidates
|
||||
],
|
||||
"bind_candidates": role.get("candidates") or [],
|
||||
}
|
||||
if (
|
||||
pending_info is not None
|
||||
and pending_role != str(snapshot["role_name"] or "").strip()
|
||||
):
|
||||
result["pending_role_name"] = pending_role
|
||||
result["pending_area_name"] = str(pending_info.get("area_name") or "")
|
||||
result["pending_plat_name"] = str(pending_info.get("plat_name") or "")
|
||||
if role.get("pending_role_name"):
|
||||
result["pending_role_name"] = role.get("pending_role_name")
|
||||
result["pending_area_name"] = role.get("pending_area_name") or ""
|
||||
result["pending_plat_name"] = role.get("pending_plat_name") or ""
|
||||
self._push_log(
|
||||
"info",
|
||||
f"查询换绑时间 hit={source_alias or '-'} role={role_name} "
|
||||
|
||||
Reference in New Issue
Block a user