feat(douyu): 完成账号平台阶段一到四并接入最新协议
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user