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:
@@ -37,13 +37,14 @@ from ..account_capability_service import (
|
||||
record_account_commodity_state,
|
||||
)
|
||||
from ..business_events import record_account_query, record_douyu_task_started
|
||||
from ..douyu_account_state import DouyuLiveAccountState, refresh_douyu_account_state
|
||||
from ..douyu_activity_query import activity_client, query_exchange_eligibility
|
||||
from ..douyu_batch_launcher import start_douyu_batch, stop_douyu_batches
|
||||
from ..douyu_elite_status import (
|
||||
elite_is_currently_open,
|
||||
normalize_elite_expire_time,
|
||||
from ..douyu_account_probe import (
|
||||
CAPABILITY_OK,
|
||||
config_mapping,
|
||||
persist_probe_result,
|
||||
probe_account,
|
||||
)
|
||||
from ..douyu_account_state import DouyuLiveAccountState, refresh_douyu_account_state
|
||||
from ..douyu_batch_launcher import start_douyu_batch, stop_douyu_batches
|
||||
from ..douyu_service import (
|
||||
SUPPORTED_DOUYU_TASK_TYPES,
|
||||
douyu_config_value,
|
||||
@@ -2195,24 +2196,32 @@ def refresh_exchange_eligibility(
|
||||
raise HTTPException(status_code=409, detail="账号 CK 不可用")
|
||||
config = ensure_douyu_config(db)
|
||||
try:
|
||||
cookie = str(account.cookie)
|
||||
client = activity_client(cookie)
|
||||
eligibility = query_exchange_eligibility(
|
||||
client,
|
||||
cookie=cookie,
|
||||
manual_id=str(douyu_config_value("manual_id", config.manual_id)),
|
||||
rid=str(douyu_config_value("rid", config.rid)),
|
||||
uid=str(account.uid or ""),
|
||||
probe = probe_account(
|
||||
account,
|
||||
config_mapping(config),
|
||||
session_checker=None,
|
||||
with_elite_role=False,
|
||||
with_eligibility=True,
|
||||
)
|
||||
elite_expire_time = normalize_elite_expire_time(
|
||||
eligibility.elite_user_info.get("expire_time")
|
||||
if (probe.get("elite") or {}).get("status") != CAPABILITY_OK:
|
||||
raise RuntimeError(
|
||||
(probe.get("errors") or {}).get("elite") or "精英宝典状态查询失败"
|
||||
)
|
||||
if "points" in (probe.get("errors") or {}):
|
||||
raise RuntimeError((probe.get("errors") or {})["points"])
|
||||
limited_ids = frozenset(
|
||||
str(item)
|
||||
for item in (probe.get("eligibility") or {}).get("limited_ids") or []
|
||||
if str(item)
|
||||
)
|
||||
account.elite_opened = elite_is_currently_open(
|
||||
eligibility.elite_user_info.get("manual_type"),
|
||||
elite_expire_time,
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
probe,
|
||||
current=current,
|
||||
source="platform.exchange_eligibility",
|
||||
record_event=True,
|
||||
)
|
||||
account.elite_expire_time = elite_expire_time
|
||||
account.points = eligibility.points
|
||||
except Exception as exc: # noqa: BLE001 外部接口错误要落成可重试的检查失败
|
||||
record_account_query(
|
||||
db,
|
||||
@@ -2240,7 +2249,7 @@ def refresh_exchange_eligibility(
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
if task.commodity_id in eligibility.limited_ids:
|
||||
if task.commodity_id in limited_ids:
|
||||
task.exchange_status = "limited"
|
||||
task.issue_code = "commodity_limit_reached"
|
||||
task.last_error = "该账号已达到此商品兑换上限"
|
||||
@@ -2262,15 +2271,13 @@ def refresh_exchange_eligibility(
|
||||
commodity_id=task.commodity_id,
|
||||
availability=(
|
||||
AVAILABILITY_LIMITED
|
||||
if task.commodity_id in eligibility.limited_ids
|
||||
if task.commodity_id in limited_ids
|
||||
else AVAILABILITY_AVAILABLE
|
||||
),
|
||||
blocking_reason=(
|
||||
"该账号已达此商品兑换上限"
|
||||
if task.commodity_id in eligibility.limited_ids
|
||||
else ""
|
||||
"该账号已达此商品兑换上限" if task.commodity_id in limited_ids else ""
|
||||
),
|
||||
raw_status="1" if task.commodity_id in eligibility.limited_ids else "0",
|
||||
raw_status="1" if task.commodity_id in limited_ids else "0",
|
||||
observed_at=_now(),
|
||||
source="eligibility_check",
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
@@ -2284,7 +2291,7 @@ def refresh_exchange_eligibility(
|
||||
detail={
|
||||
"commodity_id": task.commodity_id,
|
||||
"exchange_status": task.exchange_status,
|
||||
"limited": task.commodity_id in eligibility.limited_ids,
|
||||
"limited": task.commodity_id in limited_ids,
|
||||
"points": account.points,
|
||||
"elite_opened": account.elite_opened,
|
||||
},
|
||||
@@ -2303,7 +2310,7 @@ def refresh_exchange_eligibility(
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"exchangeStatus": task.exchange_status,
|
||||
"limitedCount": len(eligibility.limited_ids),
|
||||
"limitedCount": len(limited_ids),
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
|
||||
Reference in New Issue
Block a user