refactor(douyu): 统一账号准入与本地查询链路

This commit is contained in:
yml
2026-11-22 19:31:32 +08:00
parent e652d30283
commit d78437c6ac
14 changed files with 592 additions and 548 deletions
@@ -105,10 +105,14 @@ def fetch_exchange_records(
config: dict[str, Any],
*,
limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
scopes: tuple[str, ...] | None = None,
max_pages: int = 1,
) -> tuple[list[tuple[str, dict[str, Any]]], dict[str, str]]:
"""拉取已配置作用域的官方兑换记录,单项失败不阻断其他作用域。"""
pairs: list[tuple[str, dict[str, Any]]] = []
errors: dict[str, str] = {}
requested_scopes = set(scopes or ("elite", "esports", "peace"))
page_limit = max(1, min(int(max_pages), 10))
manual_id = str(config.get("manual_id") or "").strip()
esports_manual_id = str(config.get("esports_manual_id") or "").strip()
configured = False
@@ -118,15 +122,23 @@ def fetch_exchange_records(
("elite", "精英宝典", manual_id),
("esports", "电竞手册", esports_manual_id),
):
if scope not in requested_scopes:
continue
if not selected_manual_id:
continue
configured = True
try:
rows = query_exchange_records(
client,
manual_id=selected_manual_id,
limit=limit,
)
rows: list[dict[str, Any]] = []
for page in range(page_limit):
page_rows = query_exchange_records(
client,
manual_id=selected_manual_id,
limit=limit,
offset=page * limit,
)
rows.extend(page_rows)
if len(page_rows) < limit:
break
except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败
errors[f"exchange_records_{scope}"] = (
f"{type(exc).__name__}: {exc}"[:200]
@@ -135,7 +147,7 @@ def fetch_exchange_records(
successful += 1
pairs.extend((scope, dict(row)) for row in rows if isinstance(row, dict))
if bool(config.get("peace_enabled")):
if "peace" in requested_scopes and bool(config.get("peace_enabled")):
xpd_act_alias = str(config.get("xpd_act_alias") or "").strip()
xpd_rid = str(config.get("xpd_rid") or "").strip()
xpd_act_id = str(config.get("xpd_act_id") or "").strip()
@@ -178,10 +190,19 @@ def sync_exchange_records(
*,
source: str,
task_id: int | None = None,
scopes: tuple[str, ...] | None = None,
limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
max_pages: int = 1,
) -> dict[str, Any]:
"""同步官方记录、补事件并保存可回放的按作用域原始投影。"""
observed_at = datetime.now(UTC)
pairs, errors = fetch_exchange_records(client, config)
pairs, errors = fetch_exchange_records(
client,
config,
limit=limit,
scopes=scopes,
max_pages=max_pages,
)
changed = persist_official_exchange_records(db, account, pairs)
previous = get_sync_state(
db,