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:
@@ -17,7 +17,7 @@ from sqlalchemy import and_, func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import Account, AutomationJob
|
||||
from ...account_lifecycle import MANUAL_STATES, on_ck_verdict
|
||||
from ...account_lifecycle import MANUAL_STATES
|
||||
from ...account_value import (
|
||||
HIGH_VALUE_TIER_MAX,
|
||||
high_value_recheck_seconds,
|
||||
@@ -27,7 +27,11 @@ from ...cookie_check_service import (
|
||||
check_douyu_cookie,
|
||||
)
|
||||
from ...cookie_check_service import (
|
||||
classify_douyu_cookie_result as classify_result,
|
||||
classify_douyu_cookie_result as classify_result, # noqa: F401 - 兼容诊断入口
|
||||
)
|
||||
from ...douyu_account_probe import (
|
||||
persist_probe_result,
|
||||
probe_session,
|
||||
)
|
||||
from ...douyu_service import cookie_holding_filter, cookie_operable_filter
|
||||
|
||||
@@ -109,15 +113,16 @@ def count_remaining(db: Session, job: AutomationJob, now: datetime) -> int:
|
||||
|
||||
def _detect(account: Account) -> dict[str, Any]:
|
||||
try:
|
||||
return check_douyu_cookie(account.cookie or "")
|
||||
return probe_session(
|
||||
account.cookie or "",
|
||||
checker=check_douyu_cookie,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 单个账号失败不影响整轮
|
||||
return {
|
||||
"valid": False,
|
||||
"status": "error",
|
||||
"message": f"检测异常: {exc}",
|
||||
"fish_ball": None,
|
||||
"nickname": None,
|
||||
"level": None,
|
||||
"checked_at": datetime.now(UTC),
|
||||
"checked_at": datetime.now(UTC).isoformat(),
|
||||
"detail": {},
|
||||
}
|
||||
|
||||
|
||||
@@ -153,16 +158,39 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
result = results.get(account.id)
|
||||
if result is None:
|
||||
continue
|
||||
verdict = classify_result(result)
|
||||
verdict = str(result.get("status") or "error")
|
||||
if verdict == "valid":
|
||||
valid += 1
|
||||
elif verdict == "invalid":
|
||||
invalid += 1
|
||||
else:
|
||||
errors += 1
|
||||
# 结论落定 = 写 ck_check_* + 由好变坏时记事件 + 重算生命周期状态。
|
||||
# 三件事的**顺序与口径**收在 `on_ck_verdict` 里,四个检测入口共用。
|
||||
on_ck_verdict(db, account, verdict, detail=result, occurred_at=now)
|
||||
# 自动巡检不额外刷 account_probe 事件;统一落库仍会处理 CK 状态变化
|
||||
# 和生命周期事件,避免每 6 小时给所有账号写一条重复查询事件。
|
||||
detail = result.get("detail") if isinstance(result.get("detail"), dict) else {}
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
{
|
||||
"probe_id": f"ck-health:{now.timestamp()}:{account.id}",
|
||||
"identity": {
|
||||
"uid": account.uid,
|
||||
"username": account.username,
|
||||
"nickname": detail.get("nickname") or "",
|
||||
"nickname_source": "session",
|
||||
},
|
||||
"session": result,
|
||||
"elite": {},
|
||||
"eligibility": {},
|
||||
"roles": {},
|
||||
"gold": {},
|
||||
"goods": {},
|
||||
"errors": {},
|
||||
},
|
||||
source="automation.ck_health",
|
||||
record_event=False,
|
||||
commit=False,
|
||||
)
|
||||
|
||||
if index % FLUSH_EVERY == 0:
|
||||
db.commit()
|
||||
|
||||
Reference in New Issue
Block a user