修复(CK检测): 统一斗鱼CK有效性检测入口并禁止非鉴权接口覆盖状态
This commit is contained in:
@@ -28,10 +28,12 @@ from .account_work_queue import (
|
||||
complete_account_work,
|
||||
fail_account_work,
|
||||
)
|
||||
from .cookie_check_service import check_douyu_cookie
|
||||
from .douyu_account_probe import (
|
||||
config_mapping,
|
||||
persist_probe_result,
|
||||
probe_account,
|
||||
verify_account_session,
|
||||
)
|
||||
from .douyu_activity_query import activity_client
|
||||
from .douyu_read_gateway import StorehomeSnapshot, fetch_storehome_snapshot
|
||||
@@ -56,6 +58,34 @@ ACCOUNT_SYNC_WORK_TYPES = (
|
||||
)
|
||||
|
||||
|
||||
def _ensure_live_session(
|
||||
db: Session,
|
||||
account: Account,
|
||||
*,
|
||||
work_type: str,
|
||||
session_checker: Callable[[str], dict[str, Any]] | None,
|
||||
) -> None:
|
||||
if session_checker is None:
|
||||
return
|
||||
session = verify_account_session(
|
||||
db,
|
||||
account,
|
||||
source=f"account_work.{work_type}",
|
||||
checker=session_checker,
|
||||
max_age_seconds=300,
|
||||
record_event=False,
|
||||
commit=False,
|
||||
)
|
||||
status = str(session.get("status") or "")
|
||||
if status != "valid":
|
||||
prefix = (
|
||||
"账号 CK 已失效,请重新登录"
|
||||
if status == "invalid"
|
||||
else "账号 CK 状态无法确认"
|
||||
)
|
||||
raise RuntimeError(f"{prefix}:{session.get('message') or '请稍后重试'}")
|
||||
|
||||
|
||||
def _execute_storehome_sync(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -63,11 +93,18 @@ def _execute_storehome_sync(
|
||||
item: AccountWorkItem,
|
||||
client_factory: Callable[[str], Any],
|
||||
snapshot_fetcher: Callable[..., StorehomeSnapshot],
|
||||
session_checker: Callable[[str], dict[str, Any]] | None = check_douyu_cookie,
|
||||
) -> 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},暂不能同步商品能力")
|
||||
_ensure_live_session(
|
||||
db,
|
||||
account,
|
||||
work_type=item.work_type,
|
||||
session_checker=session_checker,
|
||||
)
|
||||
config = ensure_douyu_config(db)
|
||||
manual_id = str(getattr(config, "manual_id", "") or "")
|
||||
rid = str(getattr(config, "rid", "") or "")
|
||||
@@ -160,17 +197,25 @@ def _execute_profile_sync(
|
||||
item: AccountWorkItem,
|
||||
client_factory: Callable[[str], Any],
|
||||
probe_fetcher: Callable[..., dict[str, Any]],
|
||||
session_checker: Callable[[str], dict[str, Any]] | None = check_douyu_cookie,
|
||||
) -> 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},暂不能同步")
|
||||
_ensure_live_session(
|
||||
db,
|
||||
account,
|
||||
work_type=item.work_type,
|
||||
session_checker=session_checker,
|
||||
)
|
||||
config = ensure_douyu_config(db)
|
||||
client = client_factory(account.cookie)
|
||||
probe = probe_fetcher(
|
||||
account,
|
||||
config,
|
||||
client=client,
|
||||
session_checker=None,
|
||||
**_profile_probe_kwargs(item.work_type, config),
|
||||
)
|
||||
errors = probe.get("errors") or {}
|
||||
@@ -209,6 +254,7 @@ def _execute_exchange_records_sync(
|
||||
account: Account,
|
||||
item: AccountWorkItem,
|
||||
client_factory: Callable[[str], Any],
|
||||
session_checker: Callable[[str], dict[str, Any]] | None = check_douyu_cookie,
|
||||
) -> dict[str, Any]:
|
||||
if not account.cookie:
|
||||
raise RuntimeError("账号没有可用 CK,不能同步兑换记录")
|
||||
@@ -216,6 +262,12 @@ def _execute_exchange_records_sync(
|
||||
raise RuntimeError(
|
||||
f"账号 CK 状态为 {account.ck_check_status},暂不能同步兑换记录"
|
||||
)
|
||||
_ensure_live_session(
|
||||
db,
|
||||
account,
|
||||
work_type=item.work_type,
|
||||
session_checker=session_checker,
|
||||
)
|
||||
config = ensure_douyu_config(db)
|
||||
result = sync_exchange_records(
|
||||
db,
|
||||
@@ -240,6 +292,7 @@ def execute_account_work_item(
|
||||
client_factory: Callable[[str], Any] = activity_client,
|
||||
snapshot_fetcher: Callable[..., StorehomeSnapshot] = fetch_storehome_snapshot,
|
||||
probe_fetcher: Callable[..., dict[str, Any]] = probe_account,
|
||||
session_checker: Callable[[str], dict[str, Any]] | None = check_douyu_cookie,
|
||||
) -> dict[str, Any]:
|
||||
"""在一个短事务中执行单条账号维护工作。"""
|
||||
session = session_factory()
|
||||
@@ -258,6 +311,7 @@ def execute_account_work_item(
|
||||
item=item,
|
||||
client_factory=client_factory,
|
||||
snapshot_fetcher=snapshot_fetcher,
|
||||
session_checker=session_checker,
|
||||
)
|
||||
elif item.work_type == WORK_EXCHANGE_RECORDS_SYNC:
|
||||
result = _execute_exchange_records_sync(
|
||||
@@ -265,6 +319,7 @@ def execute_account_work_item(
|
||||
account=account,
|
||||
item=item,
|
||||
client_factory=client_factory,
|
||||
session_checker=session_checker,
|
||||
)
|
||||
elif item.work_type in ACCOUNT_SYNC_WORK_TYPES:
|
||||
result = _execute_profile_sync(
|
||||
@@ -273,6 +328,7 @@ def execute_account_work_item(
|
||||
item=item,
|
||||
client_factory=client_factory,
|
||||
probe_fetcher=probe_fetcher,
|
||||
session_checker=session_checker,
|
||||
)
|
||||
else:
|
||||
raise RuntimeError(f"不支持的工作类型: {item.work_type}")
|
||||
@@ -353,6 +409,7 @@ def run_account_sync_queue(
|
||||
client_factory: Callable[[str], Any] = activity_client,
|
||||
snapshot_fetcher: Callable[..., StorehomeSnapshot] = fetch_storehome_snapshot,
|
||||
probe_fetcher: Callable[..., dict[str, Any]] = probe_account,
|
||||
session_checker: Callable[[str], dict[str, Any]] | None = check_douyu_cookie,
|
||||
) -> dict[str, Any]:
|
||||
"""领取少量到期工作并通过独立账号同步槽执行。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
@@ -391,6 +448,7 @@ def run_account_sync_queue(
|
||||
client_factory=client_factory,
|
||||
snapshot_fetcher=snapshot_fetcher,
|
||||
probe_fetcher=probe_fetcher,
|
||||
session_checker=session_checker,
|
||||
)
|
||||
if future is not None:
|
||||
futures[future] = work_id
|
||||
|
||||
Reference in New Issue
Block a user