feat(douyu): 完成账号平台阶段一到四并接入最新协议
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
"""斗鱼官方兑换记录同步与事件补录。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.douyu import DouyuActivityClient
|
||||
|
||||
from ..models import Account
|
||||
from .account_sync_service import (
|
||||
DOMAIN_EXCHANGE_RECORDS,
|
||||
get_sync_state,
|
||||
record_sync_success,
|
||||
)
|
||||
from .business_events import record_official_exchange
|
||||
from .douyu_activity_query import (
|
||||
query_exchange_records,
|
||||
query_xpd_embed,
|
||||
)
|
||||
|
||||
EXCHANGE_RECORD_FETCH_LIMIT = 100
|
||||
|
||||
|
||||
def _to_int(value: Any) -> int | None:
|
||||
if value is None or value == "" or isinstance(value, bool):
|
||||
return None
|
||||
try:
|
||||
return int(float(value))
|
||||
except (TypeError, ValueError, OverflowError):
|
||||
return None
|
||||
|
||||
|
||||
def _parse_official_time(value: Any) -> datetime | None:
|
||||
if value is None or isinstance(value, bool):
|
||||
return None
|
||||
if isinstance(value, (int, float)):
|
||||
seconds = float(value)
|
||||
else:
|
||||
text = str(value).strip()
|
||||
if not text:
|
||||
return None
|
||||
if text.isdigit():
|
||||
seconds = float(text)
|
||||
else:
|
||||
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d"):
|
||||
try:
|
||||
return datetime.strptime(text, fmt).replace(tzinfo=UTC)
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
if seconds > 10_000_000_000:
|
||||
seconds /= 1000
|
||||
try:
|
||||
return datetime.fromtimestamp(seconds, tz=UTC)
|
||||
except (OverflowError, OSError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _group_by_scope(
|
||||
pairs: list[tuple[str, dict[str, Any]]],
|
||||
) -> dict[str, list[dict[str, Any]]]:
|
||||
grouped: dict[str, list[dict[str, Any]]] = {}
|
||||
for scope, record in pairs:
|
||||
grouped.setdefault(scope, []).append(record)
|
||||
return grouped
|
||||
|
||||
|
||||
def persist_official_exchange_records(
|
||||
db: Session,
|
||||
account: Account,
|
||||
pairs: list[tuple[str, dict[str, Any]]],
|
||||
) -> int:
|
||||
"""把官方记录幂等补成账号事件,返回新写入数量。"""
|
||||
added = 0
|
||||
for scope, record in pairs:
|
||||
if not isinstance(record, dict):
|
||||
continue
|
||||
occurred_at = _parse_official_time(record.get("time"))
|
||||
if record_official_exchange(
|
||||
db,
|
||||
account,
|
||||
commodity_id=str(
|
||||
record.get("commodityId") or record.get("commodity_id") or ""
|
||||
),
|
||||
commodity_name=str(
|
||||
record.get("commodityName") or record.get("commodity_name") or ""
|
||||
),
|
||||
score=_to_int(record.get("score")),
|
||||
occurred_at=occurred_at,
|
||||
show_name=str(
|
||||
record.get("showName") or record.get("show_name") or ""
|
||||
),
|
||||
exchange_num=record.get("exchangeNum") or record.get("count"),
|
||||
scope=scope,
|
||||
):
|
||||
added += 1
|
||||
return added
|
||||
|
||||
|
||||
def fetch_exchange_records(
|
||||
client: DouyuActivityClient,
|
||||
config: dict[str, Any],
|
||||
*,
|
||||
limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
|
||||
) -> tuple[list[tuple[str, dict[str, Any]]], dict[str, str]]:
|
||||
"""拉取已配置作用域的官方兑换记录,单项失败不阻断其他作用域。"""
|
||||
pairs: list[tuple[str, dict[str, Any]]] = []
|
||||
errors: dict[str, str] = {}
|
||||
manual_id = str(config.get("manual_id") or "").strip()
|
||||
esports_manual_id = str(config.get("esports_manual_id") or "").strip()
|
||||
configured = False
|
||||
successful = 0
|
||||
|
||||
for scope, label, selected_manual_id in (
|
||||
("elite", "精英宝典", manual_id),
|
||||
("esports", "电竞手册", esports_manual_id),
|
||||
):
|
||||
if not selected_manual_id:
|
||||
continue
|
||||
configured = True
|
||||
try:
|
||||
rows = query_exchange_records(
|
||||
client,
|
||||
manual_id=selected_manual_id,
|
||||
limit=limit,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败
|
||||
errors[f"exchange_records_{scope}"] = (
|
||||
f"{type(exc).__name__}: {exc}"[:200]
|
||||
)
|
||||
continue
|
||||
successful += 1
|
||||
pairs.extend((scope, dict(row)) for row in rows if isinstance(row, dict))
|
||||
|
||||
if 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()
|
||||
if xpd_act_alias and xpd_rid and xpd_act_id:
|
||||
configured = True
|
||||
try:
|
||||
embed = query_xpd_embed(
|
||||
client,
|
||||
act_alias=xpd_act_alias,
|
||||
rid=xpd_rid,
|
||||
)
|
||||
rows = (
|
||||
client.xpd_purchase_records(
|
||||
embed_query=embed["query"],
|
||||
act_id=xpd_act_id,
|
||||
).get("records")
|
||||
or []
|
||||
)
|
||||
pairs.extend(
|
||||
("peace", dict(row)) for row in rows if isinstance(row, dict)
|
||||
)
|
||||
successful += 1
|
||||
except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败
|
||||
errors["exchange_records_peace"] = (
|
||||
f"{type(exc).__name__}: {exc}"[:200]
|
||||
)
|
||||
|
||||
if not configured:
|
||||
raise RuntimeError("未配置可同步的斗鱼活动参数")
|
||||
if successful == 0:
|
||||
raise RuntimeError(";".join(errors.values()) or "兑换记录查询失败")
|
||||
return pairs, errors
|
||||
|
||||
|
||||
def sync_exchange_records(
|
||||
db: Session,
|
||||
account: Account,
|
||||
client: DouyuActivityClient,
|
||||
config: dict[str, Any],
|
||||
*,
|
||||
source: str,
|
||||
task_id: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""同步官方记录、补事件并保存可回放的按作用域原始投影。"""
|
||||
observed_at = datetime.now(UTC)
|
||||
pairs, errors = fetch_exchange_records(client, config)
|
||||
changed = persist_official_exchange_records(db, account, pairs)
|
||||
previous = get_sync_state(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_EXCHANGE_RECORDS,
|
||||
)
|
||||
grouped = (
|
||||
dict(previous.raw_payload_json)
|
||||
if previous is not None and isinstance(previous.raw_payload_json, dict)
|
||||
else {}
|
||||
)
|
||||
grouped.update(_group_by_scope(pairs))
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_EXCHANGE_RECORDS,
|
||||
source=source,
|
||||
observed_at=observed_at,
|
||||
item_count=len(pairs),
|
||||
changed_count=changed,
|
||||
raw_payload=grouped,
|
||||
task_id=task_id,
|
||||
)
|
||||
return {
|
||||
"item_count": len(pairs),
|
||||
"changed_count": changed,
|
||||
"records": grouped,
|
||||
"errors": errors,
|
||||
}
|
||||
|
||||
|
||||
__all__ = [
|
||||
"EXCHANGE_RECORD_FETCH_LIMIT",
|
||||
"fetch_exchange_records",
|
||||
"persist_official_exchange_records",
|
||||
"sync_exchange_records",
|
||||
]
|
||||
@@ -11,19 +11,48 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import WorkerSessionLocal
|
||||
from ..models import Account, AccountWorkItem
|
||||
from .account_sync_service import DOMAIN_STOREHOME, sync_storehome_snapshot
|
||||
from .account_exchange_sync_service import sync_exchange_records
|
||||
from .account_sync_service import (
|
||||
DOMAIN_ELITE,
|
||||
DOMAIN_ESPORTS,
|
||||
DOMAIN_EXCHANGE_RECORDS,
|
||||
DOMAIN_GOLD,
|
||||
DOMAIN_POINTS,
|
||||
DOMAIN_ROLE,
|
||||
DOMAIN_STOREHOME,
|
||||
sync_storehome_snapshot,
|
||||
)
|
||||
from .account_work_queue import (
|
||||
claim_account_work,
|
||||
complete_account_work,
|
||||
fail_account_work,
|
||||
)
|
||||
from .douyu_account_probe import (
|
||||
config_mapping,
|
||||
persist_probe_result,
|
||||
probe_account,
|
||||
)
|
||||
from .douyu_activity_query import activity_client
|
||||
from .douyu_read_gateway import StorehomeSnapshot, fetch_storehome_snapshot
|
||||
from .douyu_service import ensure_douyu_config
|
||||
from .task_runtime import TaskLane, available_slots, submit
|
||||
|
||||
WORK_STOREHOME_SYNC = DOMAIN_STOREHOME + "_sync"
|
||||
ACCOUNT_SYNC_WORK_TYPES = (WORK_STOREHOME_SYNC,)
|
||||
WORK_POINTS_SYNC = DOMAIN_POINTS + "_sync"
|
||||
WORK_GOLD_SYNC = DOMAIN_GOLD + "_sync"
|
||||
WORK_ROLE_SYNC = DOMAIN_ROLE + "_sync"
|
||||
WORK_ELITE_SYNC = DOMAIN_ELITE + "_sync"
|
||||
WORK_ESPORTS_SYNC = DOMAIN_ESPORTS + "_sync"
|
||||
WORK_EXCHANGE_RECORDS_SYNC = DOMAIN_EXCHANGE_RECORDS + "_sync"
|
||||
ACCOUNT_SYNC_WORK_TYPES = (
|
||||
WORK_STOREHOME_SYNC,
|
||||
WORK_POINTS_SYNC,
|
||||
WORK_GOLD_SYNC,
|
||||
WORK_ROLE_SYNC,
|
||||
WORK_ELITE_SYNC,
|
||||
WORK_ESPORTS_SYNC,
|
||||
WORK_EXCHANGE_RECORDS_SYNC,
|
||||
)
|
||||
|
||||
|
||||
def _execute_storehome_sync(
|
||||
@@ -64,12 +93,152 @@ def _execute_storehome_sync(
|
||||
}
|
||||
|
||||
|
||||
def _profile_probe_kwargs(
|
||||
work_type: str,
|
||||
config: Any,
|
||||
) -> dict[str, Any]:
|
||||
base = {
|
||||
"with_elite": False,
|
||||
"with_esports": False,
|
||||
"with_storehome": False,
|
||||
"with_eligibility": False,
|
||||
"with_limited_goods": False,
|
||||
"with_gold": False,
|
||||
"with_goods": False,
|
||||
"with_elite_role": False,
|
||||
"include_esports": False,
|
||||
"include_peace": False,
|
||||
"use_cache": False,
|
||||
}
|
||||
if work_type == WORK_POINTS_SYNC:
|
||||
return {**base, "with_eligibility": True}
|
||||
if work_type == WORK_GOLD_SYNC:
|
||||
return {**base, "with_gold": True}
|
||||
if work_type == WORK_ROLE_SYNC:
|
||||
return {
|
||||
**base,
|
||||
"with_elite_role": True,
|
||||
"include_esports": True,
|
||||
"include_peace": bool(getattr(config, "peace_enabled", False)),
|
||||
}
|
||||
if work_type == WORK_ELITE_SYNC:
|
||||
return {**base, "with_elite": True}
|
||||
if work_type == WORK_ESPORTS_SYNC:
|
||||
return {**base, "with_esports": True}
|
||||
raise RuntimeError(f"不支持的账号资料同步类型: {work_type}")
|
||||
|
||||
|
||||
def _profile_domain_succeeded(work_type: str, probe: dict[str, Any]) -> bool:
|
||||
if work_type == WORK_POINTS_SYNC:
|
||||
eligibility = probe.get("eligibility") or {}
|
||||
return bool(
|
||||
eligibility.get("points") is not None
|
||||
and isinstance(eligibility.get("points_response"), dict)
|
||||
and eligibility["points_response"]
|
||||
)
|
||||
if work_type == WORK_GOLD_SYNC:
|
||||
return (probe.get("gold") or {}).get("status") == "ok"
|
||||
if work_type == WORK_ROLE_SYNC:
|
||||
roles = probe.get("roles") or {}
|
||||
return any(
|
||||
(role or {}).get("status") in {"bound", "pending", "unbound"}
|
||||
for role in roles.values()
|
||||
if isinstance(role, dict)
|
||||
)
|
||||
if work_type == WORK_ELITE_SYNC:
|
||||
return (probe.get("elite") or {}).get("status") == "ok"
|
||||
if work_type == WORK_ESPORTS_SYNC:
|
||||
return (probe.get("esports") or {}).get("status") == "ok"
|
||||
return False
|
||||
|
||||
|
||||
def _execute_profile_sync(
|
||||
db: Session,
|
||||
*,
|
||||
account: Account,
|
||||
item: AccountWorkItem,
|
||||
client_factory: Callable[[str], Any],
|
||||
probe_fetcher: Callable[..., dict[str, Any]],
|
||||
) -> dict[str, Any]:
|
||||
if not account.cookie:
|
||||
raise RuntimeError("账号没有可用 CK,不能同步账号资料")
|
||||
if account.ck_check_status in {"invalid", "manual"}:
|
||||
raise RuntimeError(f"账号 CK 状态为 {account.ck_check_status},暂不能同步")
|
||||
config = ensure_douyu_config(db)
|
||||
client = client_factory(account.cookie)
|
||||
probe = probe_fetcher(
|
||||
account,
|
||||
config,
|
||||
client=client,
|
||||
**_profile_probe_kwargs(item.work_type, config),
|
||||
)
|
||||
errors = probe.get("errors") or {}
|
||||
domain_error = {
|
||||
WORK_POINTS_SYNC: "points",
|
||||
WORK_GOLD_SYNC: "gold",
|
||||
WORK_ROLE_SYNC: "elite_role",
|
||||
WORK_ELITE_SYNC: "elite",
|
||||
WORK_ESPORTS_SYNC: "esports",
|
||||
}.get(item.work_type, "")
|
||||
if domain_error and domain_error in errors:
|
||||
raise RuntimeError(str(errors[domain_error]))
|
||||
if not _profile_domain_succeeded(item.work_type, probe):
|
||||
detail = ";".join(str(value) for value in errors.values())
|
||||
raise RuntimeError(detail or f"{item.work_type} 未返回可落库结果")
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
probe,
|
||||
source=f"account_work.{item.work_type}",
|
||||
record_event=False,
|
||||
update_session=False,
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"work_id": int(item.id),
|
||||
"account_id": int(account.id),
|
||||
"work_type": item.work_type,
|
||||
"errors": sorted(errors),
|
||||
}
|
||||
|
||||
|
||||
def _execute_exchange_records_sync(
|
||||
db: Session,
|
||||
*,
|
||||
account: Account,
|
||||
item: AccountWorkItem,
|
||||
client_factory: Callable[[str], Any],
|
||||
) -> dict[str, Any]:
|
||||
if not account.cookie:
|
||||
raise RuntimeError("账号没有可用 CK,不能同步兑换记录")
|
||||
if account.ck_check_status in {"invalid", "manual"}:
|
||||
raise RuntimeError(
|
||||
f"账号 CK 状态为 {account.ck_check_status},暂不能同步兑换记录"
|
||||
)
|
||||
config = ensure_douyu_config(db)
|
||||
result = sync_exchange_records(
|
||||
db,
|
||||
account,
|
||||
client_factory(account.cookie),
|
||||
config_mapping(config),
|
||||
source=f"account_work.{item.work_type}",
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"work_id": int(item.id),
|
||||
"account_id": int(account.id),
|
||||
"work_type": item.work_type,
|
||||
**result,
|
||||
}
|
||||
|
||||
|
||||
def execute_account_work_item(
|
||||
work_id: int,
|
||||
*,
|
||||
session_factory: Callable[[], Session] = WorkerSessionLocal,
|
||||
client_factory: Callable[[str], Any] = activity_client,
|
||||
snapshot_fetcher: Callable[..., StorehomeSnapshot] = fetch_storehome_snapshot,
|
||||
probe_fetcher: Callable[..., dict[str, Any]] = probe_account,
|
||||
) -> dict[str, Any]:
|
||||
"""在一个短事务中执行单条账号维护工作。"""
|
||||
session = session_factory()
|
||||
@@ -81,15 +250,31 @@ def execute_account_work_item(
|
||||
account = session.get(Account, int(item.account_id))
|
||||
if account is None:
|
||||
raise RuntimeError("工作项对应账号不存在")
|
||||
if item.work_type != WORK_STOREHOME_SYNC:
|
||||
if item.work_type == WORK_STOREHOME_SYNC:
|
||||
result = _execute_storehome_sync(
|
||||
session,
|
||||
account=account,
|
||||
item=item,
|
||||
client_factory=client_factory,
|
||||
snapshot_fetcher=snapshot_fetcher,
|
||||
)
|
||||
elif item.work_type == WORK_EXCHANGE_RECORDS_SYNC:
|
||||
result = _execute_exchange_records_sync(
|
||||
session,
|
||||
account=account,
|
||||
item=item,
|
||||
client_factory=client_factory,
|
||||
)
|
||||
elif item.work_type in ACCOUNT_SYNC_WORK_TYPES:
|
||||
result = _execute_profile_sync(
|
||||
session,
|
||||
account=account,
|
||||
item=item,
|
||||
client_factory=client_factory,
|
||||
probe_fetcher=probe_fetcher,
|
||||
)
|
||||
else:
|
||||
raise RuntimeError(f"不支持的工作类型: {item.work_type}")
|
||||
result = _execute_storehome_sync(
|
||||
session,
|
||||
account=account,
|
||||
item=item,
|
||||
client_factory=client_factory,
|
||||
snapshot_fetcher=snapshot_fetcher,
|
||||
)
|
||||
complete_account_work(session, item)
|
||||
session.commit()
|
||||
return result
|
||||
@@ -122,6 +307,7 @@ def run_account_sync_queue(
|
||||
session_factory: Callable[[], Session] = WorkerSessionLocal,
|
||||
client_factory: Callable[[str], Any] = activity_client,
|
||||
snapshot_fetcher: Callable[..., StorehomeSnapshot] = fetch_storehome_snapshot,
|
||||
probe_fetcher: Callable[..., dict[str, Any]] = probe_account,
|
||||
) -> dict[str, Any]:
|
||||
"""领取少量到期工作并通过独立账号同步槽执行。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
@@ -159,6 +345,7 @@ def run_account_sync_queue(
|
||||
session_factory=session_factory,
|
||||
client_factory=client_factory,
|
||||
snapshot_fetcher=snapshot_fetcher,
|
||||
probe_fetcher=probe_fetcher,
|
||||
)
|
||||
if future is not None:
|
||||
futures[future] = work_id
|
||||
@@ -194,6 +381,12 @@ def run_account_sync_queue(
|
||||
|
||||
__all__ = [
|
||||
"ACCOUNT_SYNC_WORK_TYPES",
|
||||
"WORK_ELITE_SYNC",
|
||||
"WORK_ESPORTS_SYNC",
|
||||
"WORK_EXCHANGE_RECORDS_SYNC",
|
||||
"WORK_GOLD_SYNC",
|
||||
"WORK_POINTS_SYNC",
|
||||
"WORK_ROLE_SYNC",
|
||||
"WORK_STOREHOME_SYNC",
|
||||
"execute_account_work_item",
|
||||
"run_account_sync_queue",
|
||||
|
||||
@@ -40,6 +40,7 @@ from .account_events_service import CATEGORY_OPS, emit
|
||||
from .account_lifecycle import on_ck_verdict
|
||||
from .account_sync_service import (
|
||||
DOMAIN_ELITE,
|
||||
DOMAIN_ESPORTS,
|
||||
DOMAIN_GOLD,
|
||||
DOMAIN_POINTS,
|
||||
DOMAIN_ROLE,
|
||||
@@ -49,11 +50,13 @@ from .account_sync_service import (
|
||||
from .cookie_check_service import check_douyu_cookie, classify_douyu_cookie_result
|
||||
from .douyu_activity_query import (
|
||||
activity_client,
|
||||
extract_points_expire_time,
|
||||
query_activity_goods,
|
||||
query_bind_info,
|
||||
query_csrf,
|
||||
query_elite_user_info,
|
||||
query_esports_bind_info,
|
||||
query_esports_user_info,
|
||||
query_gold_account,
|
||||
query_limited_goods_result,
|
||||
query_points,
|
||||
@@ -520,14 +523,10 @@ def _points_expire_at(
|
||||
*,
|
||||
checked_at: datetime,
|
||||
) -> datetime | None:
|
||||
raw = (points_response or {}).get("willExpireTime") or (points_response or {}).get(
|
||||
"will_expire_time"
|
||||
)
|
||||
value = _to_int(raw)
|
||||
if value is None or value <= 0:
|
||||
# 2026-11-21:积分响应可能是归一化结果、raw 或 raw.data,统一走同一个提取器。
|
||||
value = extract_points_expire_time(points_response)
|
||||
if value is None:
|
||||
return None
|
||||
if value > 10_000_000_000:
|
||||
value //= 1000
|
||||
if value < int(checked_at.timestamp()) - 60:
|
||||
return None
|
||||
return datetime.fromtimestamp(value, tz=UTC)
|
||||
@@ -613,6 +612,8 @@ def probe_account(
|
||||
_DEFAULT_SESSION_CHECKER
|
||||
),
|
||||
with_elite: bool = True,
|
||||
with_esports: bool = False,
|
||||
with_storehome: bool = True,
|
||||
with_eligibility: bool = False,
|
||||
with_limited_goods: bool = False,
|
||||
with_gold: bool = False,
|
||||
@@ -628,6 +629,8 @@ def probe_account(
|
||||
cache_mode = "|".join(
|
||||
[
|
||||
f"elite={int(with_elite)}",
|
||||
f"esports_info={int(with_esports)}",
|
||||
f"storehome={int(with_storehome)}",
|
||||
f"elig={int(with_eligibility)}",
|
||||
f"limited={int(with_limited_goods)}",
|
||||
f"gold={int(with_gold)}",
|
||||
@@ -685,6 +688,15 @@ def probe_account(
|
||||
"opened": account.elite_opened,
|
||||
"error": "",
|
||||
},
|
||||
"esports": {
|
||||
"status": CAPABILITY_UNKNOWN,
|
||||
"raw": {},
|
||||
"manual_type": None,
|
||||
"manual_score": None,
|
||||
"expire_time": None,
|
||||
"opened": account.esports_opened,
|
||||
"error": "",
|
||||
},
|
||||
"eligibility": {
|
||||
"points": account.points,
|
||||
"points_response": {},
|
||||
@@ -742,6 +754,17 @@ def probe_account(
|
||||
except Exception as exc: # noqa: BLE001 - 单项能力边界
|
||||
errors["elite"] = f"{type(exc).__name__}: {_text(exc)}"
|
||||
|
||||
esports: dict[str, Any] = {}
|
||||
esports_manual_id = str(config_info.get("esports_manual_id") or "").strip()
|
||||
if esports_manual_id and with_esports:
|
||||
try:
|
||||
esports = query_esports_user_info(
|
||||
active_client,
|
||||
manual_id=esports_manual_id,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 单项能力边界
|
||||
errors["esports"] = f"{type(exc).__name__}: {_text(exc)}"
|
||||
|
||||
roles: dict[str, dict[str, Any]] = {}
|
||||
if with_elite_role:
|
||||
roles["elite"] = resolve_elite_role(active_client, config_info)
|
||||
@@ -764,7 +787,7 @@ def probe_account(
|
||||
points_response = points_result
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["points"] = f"{type(exc).__name__}: {_text(exc)}"
|
||||
if with_eligibility or with_limited_goods:
|
||||
if with_storehome and (with_eligibility or with_limited_goods):
|
||||
try:
|
||||
limited_result = query_limited_goods_result(
|
||||
active_client,
|
||||
@@ -849,6 +872,12 @@ def probe_account(
|
||||
manual_score = _to_int(elite.get("manual_score"))
|
||||
expire_time = normalize_elite_expire_time(elite.get("expire_time"))
|
||||
elite_opened = elite_is_currently_open(manual_type, expire_time, now=checked_at)
|
||||
esports_manual_type = _to_int(esports.get("manual_type"))
|
||||
esports_manual_score = _to_int(esports.get("manual_score"))
|
||||
esports_expire_time = normalize_elite_expire_time(esports.get("expire_time"))
|
||||
esports_opened = bool(
|
||||
esports_manual_type is not None and esports_manual_type >= 1
|
||||
)
|
||||
if points is None:
|
||||
points = manual_score
|
||||
identity = _identity_result(
|
||||
@@ -876,6 +905,19 @@ def probe_account(
|
||||
"opened": elite_opened,
|
||||
"error": errors.get("elite", ""),
|
||||
},
|
||||
"esports": {
|
||||
"status": CAPABILITY_OK
|
||||
if esports
|
||||
else CAPABILITY_ERROR
|
||||
if "esports" in errors
|
||||
else CAPABILITY_UNKNOWN,
|
||||
"raw": esports,
|
||||
"manual_type": esports_manual_type,
|
||||
"manual_score": esports_manual_score,
|
||||
"expire_time": esports_expire_time,
|
||||
"opened": esports_opened,
|
||||
"error": errors.get("esports", ""),
|
||||
},
|
||||
"eligibility": {
|
||||
"manual_id": manual_id,
|
||||
"rid": str(config_info.get("rid") or ""),
|
||||
@@ -966,6 +1008,7 @@ def persist_probe_result(
|
||||
source: str,
|
||||
record_event: bool = True,
|
||||
commit: bool = False,
|
||||
update_session: bool = True,
|
||||
) -> None:
|
||||
"""把一次探针结果写回账号和生命周期,是账号能力的唯一落库入口。"""
|
||||
now = _now()
|
||||
@@ -977,7 +1020,11 @@ def persist_probe_result(
|
||||
|
||||
session = probe.get("session") or {}
|
||||
session_status = str(session.get("status") or "")
|
||||
if session_status in {SESSION_VALID, SESSION_INVALID, SESSION_ERROR}:
|
||||
if update_session and session_status in {
|
||||
SESSION_VALID,
|
||||
SESSION_INVALID,
|
||||
SESSION_ERROR,
|
||||
}:
|
||||
detail = (
|
||||
session.get("detail") if isinstance(session.get("detail"), dict) else {}
|
||||
)
|
||||
@@ -1022,10 +1069,26 @@ def persist_probe_result(
|
||||
raw_payload=dict(elite),
|
||||
)
|
||||
|
||||
esports = probe.get("esports") or {}
|
||||
if esports.get("status") == CAPABILITY_OK:
|
||||
account.esports_opened = bool(esports.get("opened"))
|
||||
account.esports_points = _to_int(esports.get("manual_score"))
|
||||
account.esports_expire_time = _to_int(esports.get("expire_time"))
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_ESPORTS,
|
||||
source=source,
|
||||
observed_at=now,
|
||||
raw_payload=dict(esports),
|
||||
)
|
||||
|
||||
eligibility = probe.get("eligibility") or {}
|
||||
if _to_int(eligibility.get("points")) is not None:
|
||||
account.points = _to_int(eligibility.get("points"))
|
||||
if isinstance(eligibility.get("points_response"), dict):
|
||||
if isinstance(eligibility.get("points_response"), dict) and eligibility[
|
||||
"points_response"
|
||||
]:
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
|
||||
@@ -46,6 +46,14 @@ def query_elite_user_info(
|
||||
return client.elite_user_info(manual_id=manual_id, rid=rid)
|
||||
|
||||
|
||||
def query_esports_user_info(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
manual_id: str,
|
||||
) -> dict[str, Any]:
|
||||
return client.esports_user_info(manual_id=manual_id)
|
||||
|
||||
|
||||
def query_bind_info(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
@@ -98,6 +106,14 @@ def query_gold_account(client: DouyuActivityClient) -> dict[str, Any]:
|
||||
return client.gold_account()
|
||||
|
||||
|
||||
def query_gold_qr_state(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
token: str,
|
||||
) -> dict[str, Any]:
|
||||
return client.query_gold_qr_state(token)
|
||||
|
||||
|
||||
def query_exchange_balance(client: DouyuActivityClient) -> dict[str, Any]:
|
||||
return client.exchange_balance()
|
||||
|
||||
@@ -157,6 +173,65 @@ def query_points(
|
||||
return client.query_points(uid=uid, ctn=ctn)
|
||||
|
||||
|
||||
def query_points_expirations(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
user_id: str,
|
||||
current: int = 0,
|
||||
page_size: int = 20,
|
||||
) -> dict[str, Any]:
|
||||
return client.query_points_expirations(
|
||||
user_id=user_id,
|
||||
current=current,
|
||||
page_size=page_size,
|
||||
)
|
||||
|
||||
|
||||
def query_points_ledger(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
uid: str,
|
||||
page_num: int = 1,
|
||||
page_size: int = 15,
|
||||
) -> dict[str, Any]:
|
||||
return client.query_points_ledger(
|
||||
uid=uid,
|
||||
page_num=page_num,
|
||||
page_size=page_size,
|
||||
)
|
||||
|
||||
|
||||
def extract_points_expire_time(payload: Any) -> int | None:
|
||||
"""从各层积分响应中提取 willExpireTime(2026-11-21 统一口径)。"""
|
||||
if not isinstance(payload, dict):
|
||||
return None
|
||||
queue: list[dict[str, Any]] = [payload]
|
||||
seen: set[int] = set()
|
||||
while queue:
|
||||
current = queue.pop(0)
|
||||
marker = id(current)
|
||||
if marker in seen:
|
||||
continue
|
||||
seen.add(marker)
|
||||
for key in ("willExpireTime", "will_expire_time"):
|
||||
raw = current.get(key)
|
||||
if raw in (None, "", 0, "0"):
|
||||
continue
|
||||
try:
|
||||
value = int(float(raw))
|
||||
except (TypeError, ValueError, OverflowError):
|
||||
continue
|
||||
if value > 10_000_000_000:
|
||||
value //= 1000
|
||||
if value > 0:
|
||||
return value
|
||||
for key in ("raw", "data", "points_query", "result"):
|
||||
nested = current.get(key)
|
||||
if isinstance(nested, dict):
|
||||
queue.append(nested)
|
||||
return None
|
||||
|
||||
|
||||
def query_limited_goods_result(
|
||||
client: DouyuActivityClient,
|
||||
*,
|
||||
@@ -244,6 +319,7 @@ def query_exchange_orders(
|
||||
|
||||
__all__ = [
|
||||
"activity_client",
|
||||
"extract_points_expire_time",
|
||||
"query_activity_goods",
|
||||
"query_activity_goods_result",
|
||||
"query_bind_info",
|
||||
@@ -252,15 +328,19 @@ __all__ = [
|
||||
"query_esports_bind_info",
|
||||
"query_esports_goods",
|
||||
"query_esports_goods_result",
|
||||
"query_esports_user_info",
|
||||
"query_exchange_balance",
|
||||
"query_exchange_orders",
|
||||
"query_exchange_orders_result",
|
||||
"query_exchange_records",
|
||||
"query_exchange_records_result",
|
||||
"query_gold_account",
|
||||
"query_gold_qr_state",
|
||||
"query_limited_goods",
|
||||
"query_limited_goods_result",
|
||||
"query_points",
|
||||
"query_points_expirations",
|
||||
"query_points_ledger",
|
||||
"query_xpd_bind_info",
|
||||
"query_xpd_embed",
|
||||
"query_xpd_role",
|
||||
|
||||
@@ -11,7 +11,7 @@ from core.douyu import DouyuActivityClient
|
||||
|
||||
from ..models import Account, DouyuTask
|
||||
from .business_events import record_gold_spent, record_points_credit
|
||||
from .douyu_activity_query import query_csrf
|
||||
from .douyu_activity_query import extract_points_expire_time, query_csrf
|
||||
|
||||
DOUYU_GIFT_POINTS_REFRESH_TIMES = 3
|
||||
DOUYU_GIFT_POINTS_REFRESH_INTERVAL = 2
|
||||
@@ -366,12 +366,8 @@ def _points_expire_at(result: dict) -> object:
|
||||
旧收集器走的是 `result["points_query"]["raw"]["data"]["willExpireTime"]` 这条
|
||||
「猜出来」的路径;这里收敛成一个具名函数,只有它知道那段结构。
|
||||
"""
|
||||
query = result.get("points_query")
|
||||
if not isinstance(query, dict):
|
||||
return None
|
||||
raw = query.get("raw")
|
||||
data = raw.get("data") if isinstance(raw, dict) else None
|
||||
return data.get("willExpireTime") if isinstance(data, dict) else None
|
||||
# 2026-11-21:统一兼容顶层 will_expire_time 与旧的 points_query.raw.data 结构。
|
||||
return extract_points_expire_time(result.get("points_query"))
|
||||
|
||||
|
||||
def _esports_gift_points(result: dict, baseline_points: int | None) -> int | None:
|
||||
|
||||
@@ -19,7 +19,11 @@ from core.douyu import (
|
||||
from ..models import Account, DouyuTask
|
||||
from .account_sync_service import DOMAIN_GOLD, record_sync_success
|
||||
from .business_events import record_recharge
|
||||
from .douyu_activity_query import query_exchange_balance, query_gold_account
|
||||
from .douyu_activity_query import (
|
||||
query_exchange_balance,
|
||||
query_gold_account,
|
||||
query_gold_qr_state,
|
||||
)
|
||||
from .douyu_service import update_account_profile_from_cookie
|
||||
|
||||
|
||||
@@ -45,16 +49,70 @@ class GoldMixin:
|
||||
) -> dict:
|
||||
"""刷新鱼翅和钱包兑换余额并写回账号表。"""
|
||||
gold = query_gold_account(client)
|
||||
exchange = query_exchange_balance(client)
|
||||
account.gold_balance = self._to_int(gold.get("gold"))
|
||||
account.exchange_balance = self._to_int(exchange.get("count"))
|
||||
# 2026-11-21:鱼翅余额是充值确认的关键结果。钱包兑换余额属于旁路资产,
|
||||
# 单项失败不能反过来把已经成功拉到的鱼翅余额判为刷新失败。
|
||||
try:
|
||||
exchange = query_exchange_balance(client)
|
||||
account.exchange_balance = self._to_int(exchange.get("count"))
|
||||
exchange_error = ""
|
||||
except Exception as exc: # noqa: BLE001 兑换余额不能阻断鱼翅确认
|
||||
exchange = {}
|
||||
exchange_error = str(exc)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
return {
|
||||
result = {
|
||||
"gold_balance": account.gold_balance,
|
||||
"exchange_balance": account.exchange_balance,
|
||||
"gold": gold,
|
||||
"exchange_balance_query": exchange,
|
||||
}
|
||||
if exchange_error:
|
||||
result["exchange_balance_error"] = exchange_error
|
||||
return result
|
||||
|
||||
def _finish_wallet_gold_recharge(
|
||||
self,
|
||||
db: Session,
|
||||
task: DouyuTask,
|
||||
account: Account,
|
||||
result: dict,
|
||||
*,
|
||||
amount: int,
|
||||
channel: str,
|
||||
order_id: str = "",
|
||||
balance_refreshed: bool,
|
||||
confirm_source: str,
|
||||
) -> None:
|
||||
"""2026-11-21:新版支付状态确认后统一刷新余额并写充值事实。"""
|
||||
result.pop("payment_poll_error", None)
|
||||
result["gold_recharged"] = True
|
||||
result["payment_polling"] = False
|
||||
result["gold_refreshed_after_success"] = balance_refreshed
|
||||
result["gold_confirm_source"] = confirm_source
|
||||
if order_id:
|
||||
result["order_id"] = order_id
|
||||
account.updated_at = datetime.now(UTC)
|
||||
record_recharge(
|
||||
db,
|
||||
account,
|
||||
task_id=task.id,
|
||||
amount=max(1, int(amount)),
|
||||
order_id=order_id,
|
||||
channel=channel,
|
||||
balance_after=account.gold_balance,
|
||||
confirmed=True,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
order_text = f",订单 {order_id}" if order_id else ""
|
||||
self._mark_task(
|
||||
db,
|
||||
task,
|
||||
"success",
|
||||
"鱼翅已到账"
|
||||
f"{order_text},当前余额: "
|
||||
f"{account.gold_balance if account.gold_balance is not None else '-'}",
|
||||
result,
|
||||
)
|
||||
|
||||
def _poll_create_gold_qr_once(
|
||||
self,
|
||||
@@ -68,12 +126,85 @@ class GoldMixin:
|
||||
result = dict(task.result or {})
|
||||
result["payment_polling"] = True
|
||||
result["payment_poll_count"] = int(result.get("payment_poll_count") or 0) + 1
|
||||
amount = int(
|
||||
result.get("payment_amount")
|
||||
or self._task_payload(task).get("amount")
|
||||
or self._task_payload(task).get("gold_amount")
|
||||
or 1
|
||||
)
|
||||
channel = str(
|
||||
result.get("payment_channel")
|
||||
or config.get("gold_recharge_channel")
|
||||
or "wechat_qr"
|
||||
)
|
||||
try:
|
||||
client = self._client(cookie, direct=True)
|
||||
balance_result = self._refresh_account_gold_balance(client, account)
|
||||
db.commit()
|
||||
result.update(balance_result)
|
||||
last_gold = balance_result["gold_balance"]
|
||||
token = str(result.get("payment_token") or result.get("token") or "")
|
||||
qr_state: int | None = None
|
||||
qr_state_error = ""
|
||||
if token:
|
||||
try:
|
||||
state_result = query_gold_qr_state(client, token=token)
|
||||
if not isinstance(state_result, dict):
|
||||
raise TypeError("鱼翅支付状态响应格式异常")
|
||||
qr_state = self._to_int(state_result.get("state"))
|
||||
result["gold_qr_state"] = qr_state
|
||||
if state_result.get("quantity") is not None:
|
||||
result["gold_qr_quantity"] = state_result.get("quantity")
|
||||
quantity = self._to_int(state_result.get("quantity"))
|
||||
if quantity is not None and quantity > 0:
|
||||
amount = quantity
|
||||
if state_result.get("order_id"):
|
||||
result["order_id"] = str(state_result["order_id"])
|
||||
except Exception as exc: # noqa: BLE001 新接口失败时兼容余额轮询
|
||||
qr_state_error = str(exc)
|
||||
result["gold_qr_state_error"] = qr_state_error
|
||||
|
||||
balance_result: dict[str, Any] = {}
|
||||
balance_error = ""
|
||||
try:
|
||||
balance_result = self._refresh_account_gold_balance(client, account)
|
||||
db.commit()
|
||||
result.update(balance_result)
|
||||
except Exception as exc: # noqa: BLE001 官方状态成功后仍应完成充值
|
||||
balance_error = str(exc)
|
||||
result["payment_poll_error"] = balance_error
|
||||
last_gold = balance_result.get("gold_balance")
|
||||
|
||||
if qr_state == 2:
|
||||
self._finish_wallet_gold_recharge(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
result,
|
||||
amount=amount,
|
||||
channel=channel,
|
||||
order_id=str(result.get("order_id") or ""),
|
||||
balance_refreshed=not balance_error,
|
||||
confirm_source="gold_qr_state",
|
||||
)
|
||||
return
|
||||
if qr_state is not None:
|
||||
state_text = {-2: "等待支付", 0: "支付处理中"}.get(
|
||||
qr_state, f"状态 {qr_state}"
|
||||
)
|
||||
self._schedule_payment_poll(
|
||||
db,
|
||||
task,
|
||||
result,
|
||||
f"鱼翅充值{state_text}"
|
||||
f"(当前鱼翅 {last_gold if last_gold is not None else '-'})",
|
||||
)
|
||||
return
|
||||
if balance_error:
|
||||
self._schedule_payment_poll(
|
||||
db,
|
||||
task,
|
||||
result,
|
||||
f"等待鱼翅到账: {balance_error}",
|
||||
)
|
||||
return
|
||||
|
||||
baseline_gold = self._to_int(result.get("baseline_gold_balance"))
|
||||
if baseline_gold is None:
|
||||
result["baseline_gold_balance"] = last_gold
|
||||
@@ -86,37 +217,16 @@ class GoldMixin:
|
||||
return
|
||||
# 余额必须增加才算到账,下降可能来自同账号送礼等消费。
|
||||
if last_gold is not None and last_gold > baseline_gold:
|
||||
result.pop("payment_poll_error", None)
|
||||
result["gold_recharged"] = True
|
||||
result["payment_polling"] = False
|
||||
amount = int(
|
||||
result.get("payment_amount")
|
||||
or self._task_payload(task).get("amount")
|
||||
or self._task_payload(task).get("gold_amount")
|
||||
or 1
|
||||
)
|
||||
channel = str(
|
||||
result.get("payment_channel")
|
||||
or config.get("gold_recharge_channel")
|
||||
or "wechat_qr"
|
||||
)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
record_recharge(
|
||||
db,
|
||||
account,
|
||||
task_id=task.id,
|
||||
amount=amount,
|
||||
channel=channel,
|
||||
balance_after=account.gold_balance,
|
||||
confirmed=True,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(
|
||||
self._finish_wallet_gold_recharge(
|
||||
db,
|
||||
task,
|
||||
"success",
|
||||
f"鱼翅已到账,当前余额: {account.gold_balance if account.gold_balance is not None else '-'}",
|
||||
account,
|
||||
result,
|
||||
amount=amount,
|
||||
channel=channel,
|
||||
order_id=str(result.get("order_id") or ""),
|
||||
balance_refreshed=True,
|
||||
confirm_source="balance_increase",
|
||||
)
|
||||
return
|
||||
result.pop("payment_poll_error", None)
|
||||
@@ -443,6 +553,7 @@ class GoldMixin:
|
||||
{
|
||||
"payment_amount": amount,
|
||||
"payment_channel": channel,
|
||||
"payment_token": str(result.get("token") or ""),
|
||||
"baseline_gold_balance": baseline_gold,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -12,13 +12,8 @@ from sqlalchemy.orm import Session
|
||||
from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||
|
||||
from ..models import Account, DouyuEsportsGoodsSnapshot, DouyuGoodsSnapshot, DouyuTask
|
||||
from .account_sync_service import (
|
||||
DOMAIN_EXCHANGE_RECORDS,
|
||||
finish_sync_failure,
|
||||
finish_sync_success,
|
||||
start_sync_run,
|
||||
sync_storehome_snapshot,
|
||||
)
|
||||
from .account_exchange_sync_service import sync_exchange_records
|
||||
from .account_sync_service import sync_storehome_snapshot
|
||||
from .business_events import record_exchange
|
||||
from .douyu_activity_query import (
|
||||
query_activity_goods,
|
||||
@@ -26,12 +21,10 @@ from .douyu_activity_query import (
|
||||
query_csrf,
|
||||
query_elite_user_info,
|
||||
query_esports_goods_result,
|
||||
query_exchange_records_result,
|
||||
query_limited_goods_result,
|
||||
)
|
||||
from .douyu_goods_service import upsert_goods_snapshot
|
||||
from .douyu_read_gateway import storehome_snapshot_from_result
|
||||
from .douyu_runner_overview import record_official_exchange_records
|
||||
from .high_value_service import (
|
||||
DEFAULT_HIGH_VALUE_GOODS,
|
||||
HIGH_VALUE_STATUS_OWNED,
|
||||
@@ -793,58 +786,21 @@ class GoodsMixin:
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
client = self._client(cookie)
|
||||
result = query_exchange_records_result(
|
||||
client,
|
||||
manual_id=str(config["manual_id"]),
|
||||
limit=100,
|
||||
offset=0,
|
||||
)
|
||||
records = result.get("records") or []
|
||||
observed_at = datetime.now(UTC)
|
||||
sync_run = start_sync_run(
|
||||
result = sync_exchange_records(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_EXCHANGE_RECORDS,
|
||||
account,
|
||||
client,
|
||||
config,
|
||||
source="douyu_task.query_exchange_records",
|
||||
task_id=task.id,
|
||||
)
|
||||
try:
|
||||
added = record_official_exchange_records(
|
||||
db,
|
||||
account,
|
||||
[("elite", record) for record in records if isinstance(record, dict)],
|
||||
)
|
||||
finish_sync_success(
|
||||
db,
|
||||
sync_run,
|
||||
item_count=len(records),
|
||||
changed_count=added,
|
||||
observed_at=observed_at,
|
||||
raw_payload=(
|
||||
result.get("raw")
|
||||
if isinstance(result.get("raw"), dict)
|
||||
else None
|
||||
),
|
||||
)
|
||||
except Exception as exc:
|
||||
finish_sync_failure(
|
||||
db,
|
||||
sync_run,
|
||||
error=exc,
|
||||
raw_payload=(
|
||||
result.get("raw")
|
||||
if isinstance(result.get("raw"), dict)
|
||||
else None
|
||||
),
|
||||
)
|
||||
raise
|
||||
result["exchange_events_added"] = added
|
||||
account.updated_at = datetime.now(UTC)
|
||||
item_count = int(result.get("item_count") or 0)
|
||||
self._mark_task(
|
||||
db,
|
||||
task,
|
||||
"success",
|
||||
f"兑换记录 {len(records)} 条" if records else "暂无兑换记录",
|
||||
f"兑换记录 {item_count} 条" if item_count else "暂无兑换记录",
|
||||
result,
|
||||
)
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user