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:
@@ -33,8 +33,9 @@ from ..models import Account, AuditLog, LoginTask, User
|
||||
from ..models import ProxyConfig as ProxyConfigModel
|
||||
from ..permissions import get_user_permissions, user_has_permission
|
||||
from ..schemas import CookieReloginRequest
|
||||
from ..services.account_lifecycle import on_ck_cleared, on_ck_verdict
|
||||
from ..services.account_lifecycle import on_ck_cleared
|
||||
from ..services.cookie_check_service import check_douyu_cookie
|
||||
from ..services.douyu_account_probe import persist_probe_result, probe_session
|
||||
from ..services.douyu_service import (
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES,
|
||||
cookie_holding_filter,
|
||||
@@ -117,9 +118,13 @@ def _relogin_display(account: Account, in_flight_batch_id: str = "") -> dict:
|
||||
|
||||
def _check_one_cookie(account: Account) -> dict:
|
||||
"""将共享检测结果补充为 CK 记录的接口返回格式。"""
|
||||
result = check_douyu_cookie(account.cookie or "")
|
||||
session = probe_session(account.cookie or "", checker=check_douyu_cookie)
|
||||
result = dict(session.get("detail") or {})
|
||||
result["id"] = account.id
|
||||
result["checked_at"] = result["checked_at"].isoformat()
|
||||
result["valid"] = session["status"] == "valid"
|
||||
result["message"] = session["message"]
|
||||
result["checked_at"] = session["checked_at"]
|
||||
result["_session_probe"] = session
|
||||
return result
|
||||
|
||||
|
||||
@@ -502,6 +507,12 @@ def _check_cookies(ids: str, db: Session, current: User, *, detailed: bool) -> d
|
||||
"nickname": None,
|
||||
"level": None,
|
||||
"checked_at": datetime.now(UTC).isoformat(),
|
||||
"_session_probe": {
|
||||
"status": "error",
|
||||
"message": f"检测异常: {exc}",
|
||||
"checked_at": datetime.now(UTC).isoformat(),
|
||||
"detail": {},
|
||||
},
|
||||
}
|
||||
)
|
||||
results.sort(key=lambda item: item["id"])
|
||||
@@ -513,15 +524,47 @@ def _check_cookies(ids: str, db: Session, current: User, *, detailed: bool) -> d
|
||||
acc = accounts_by_id.get(item["id"])
|
||||
if not acc:
|
||||
continue
|
||||
# 人工检测结论同样走统一入口(写结论 + 由好变坏记事件 + 重算状态)
|
||||
on_ck_verdict(
|
||||
session_probe = item.pop("_session_probe", None)
|
||||
if not isinstance(session_probe, dict):
|
||||
session_probe = {
|
||||
"status": "valid" if item.get("valid") else "invalid",
|
||||
"message": item.get("message") or "",
|
||||
"checked_at": item.get("checked_at") or checked_at.isoformat(),
|
||||
"detail": {
|
||||
"valid": bool(item.get("valid")),
|
||||
"message": item.get("message") or "",
|
||||
"fish_ball": item.get("fish_ball"),
|
||||
"nickname": item.get("nickname"),
|
||||
"level": item.get("level"),
|
||||
"checked_at": item.get("checked_at") or checked_at,
|
||||
},
|
||||
}
|
||||
# 人工检测走统一探针落库:CK 能力、生命周期与账号时间线一次完成。
|
||||
persist_probe_result(
|
||||
db,
|
||||
acc,
|
||||
"valid" if item["valid"] else "invalid",
|
||||
detail=item,
|
||||
occurred_at=checked_at,
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
{
|
||||
"probe_id": f"ck-check:{checked_at.timestamp()}:{acc.id}",
|
||||
"identity": {
|
||||
"uid": acc.uid,
|
||||
"username": acc.username,
|
||||
"nickname": (
|
||||
(session_probe.get("detail") or {}).get("nickname") or ""
|
||||
),
|
||||
"nickname_source": "session",
|
||||
},
|
||||
"session": session_probe,
|
||||
"elite": {},
|
||||
"eligibility": {},
|
||||
"roles": {},
|
||||
"gold": {},
|
||||
"goods": {},
|
||||
"errors": {},
|
||||
},
|
||||
current=current,
|
||||
source="account.ck_check",
|
||||
record_event=True,
|
||||
commit=False,
|
||||
)
|
||||
# 审计带上账号:单个检测时可直接反查(批量时只能落到 target 文本里)
|
||||
db.add(
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
|
||||
from fastapi import (
|
||||
APIRouter,
|
||||
@@ -57,6 +59,14 @@ from ..services.douyu_collab_events import collab_events
|
||||
router = APIRouter(prefix="/api/douyu", tags=["斗鱼外部单协作"])
|
||||
|
||||
|
||||
def _verify_batch_interval() -> float:
|
||||
try:
|
||||
value = float(os.getenv("DOUYU_COLLAB_VERIFY_BATCH_INTERVAL_SECONDS", "0.2"))
|
||||
except ValueError:
|
||||
value = 0.2
|
||||
return max(0.0, min(value, 5.0))
|
||||
|
||||
|
||||
def _http_error(exc: collab.CollabError) -> ApiError:
|
||||
return ApiError(
|
||||
status_code=exc.status_code,
|
||||
@@ -356,7 +366,10 @@ def verify_external_orders_batch(
|
||||
current: User = Depends(require_permission("douyu:collab:edit")),
|
||||
):
|
||||
results: list[dict] = []
|
||||
for order_id in ids:
|
||||
interval = _verify_batch_interval()
|
||||
for index, order_id in enumerate(ids):
|
||||
if index and interval:
|
||||
time.sleep(interval)
|
||||
try:
|
||||
order = collab.get_order(db, order_id)
|
||||
result = collab.verify_order(db, current, order)
|
||||
|
||||
Reference in New Issue
Block a user