diff --git a/tests/test_douyu_account_probe.py b/tests/test_douyu_account_probe.py index 6287153..394f888 100644 --- a/tests/test_douyu_account_probe.py +++ b/tests/test_douyu_account_probe.py @@ -136,6 +136,18 @@ class ProbeClient: return _bind("角色A", bound_act=True, bound_role=True) +class LimitedOnlyClient(ProbeClient): + def acf_ccn(self, **_kwargs): + raise AssertionError("推荐探针不应查询积分令牌") + + def query_points(self, **_kwargs): + raise AssertionError("推荐探针不应查询积分接口") + + def query_limited_goods(self, **_kwargs): + self.calls += 1 + return {"limited_goods": [{"commodityId": "limited-goods"}]} + + def test_probe_account_reads_identity_and_does_not_call_activity_for_invalid_ck(): account = Account( id=1, @@ -178,6 +190,34 @@ def test_probe_account_reads_identity_and_does_not_call_activity_for_invalid_ck( assert invalid_client.calls == 0 +def test_probe_limited_goods_does_not_run_full_eligibility_flow(): + account = Account( + id=2, + username="limited-only", + password="p", + email="limited@example.com", + email_password="ep", + cookie="acf_uid=200; acf_nickname=limited", + ) + client = LimitedOnlyClient() + + result = probe_account( + account, + CONFIG, + client=client, + session_checker=lambda _cookie: { + "valid": True, + "message": "有效", + "checked_at": datetime.now(UTC), + }, + with_eligibility=False, + with_limited_goods=True, + ) + + assert result["eligibility"]["limited_ids"] == ["limited-goods"] + assert result["eligibility"]["points"] == 100 + + def test_persist_probe_error_preserves_last_known_role(db): account = Account( username="preserve-role", diff --git a/tests/test_douyu_collab_orders.py b/tests/test_douyu_collab_orders.py index a3fb815..3b5f914 100644 --- a/tests/test_douyu_collab_orders.py +++ b/tests/test_douyu_collab_orders.py @@ -44,6 +44,15 @@ class FakeClient: "user_info": {"nickname": "斗鱼昵称"}, } + def acf_ccn(self, **_): + return "fake-csrf" + + def query_points(self, **_): + return {"points": self.points} + + def query_limited_goods(self, **_): + return {"limited_goods": []} + def bind_info(self, **_): return { "is_bound_account": True, diff --git a/web/backend/services/douyu_account_probe.py b/web/backend/services/douyu_account_probe.py index 0a47e9a..d2e5835 100644 --- a/web/backend/services/douyu_account_probe.py +++ b/web/backend/services/douyu_account_probe.py @@ -588,6 +588,7 @@ def probe_account( ), with_elite: bool = True, with_eligibility: bool = False, + with_limited_goods: bool = False, with_gold: bool = False, with_goods: bool = False, with_elite_role: bool = True, @@ -602,6 +603,7 @@ def probe_account( [ f"elite={int(with_elite)}", f"elig={int(with_eligibility)}", + f"limited={int(with_limited_goods)}", f"gold={int(with_gold)}", f"goods={int(with_goods)}", f"role={int(with_elite_role)}", @@ -734,6 +736,7 @@ def probe_account( points_response = points_result except Exception as exc: # noqa: BLE001 errors["points"] = f"{type(exc).__name__}: {_text(exc)}" + if with_eligibility or with_limited_goods: try: limited_result = query_limited_goods_result( active_client, diff --git a/web/backend/services/douyu_collab_service.py b/web/backend/services/douyu_collab_service.py index a59dda5..879919f 100644 --- a/web/backend/services/douyu_collab_service.py +++ b/web/backend/services/douyu_collab_service.py @@ -14,6 +14,8 @@ from __future__ import annotations from collections.abc import Callable, Mapping, Sequence +from concurrent.futures import ThreadPoolExecutor, as_completed +from concurrent.futures import TimeoutError as FutureTimeoutError from datetime import UTC, datetime from typing import Any @@ -83,7 +85,7 @@ from .file_service import build_file_url from .platform_fulfillment.account_selector import ( acquire_account_lease, active_lease_for_account, - has_recent_exchange, + capability_candidate_accounts, live_candidate_accounts, release_lease, ) @@ -146,7 +148,9 @@ STATUS_FLOW: tuple[str, ...] = ( SOURCE_TYPE_PLATFORM_FULFILLMENT = "platform_fulfillment" ACCOUNT_REPLACE_ROLES = frozenset({"super_admin", "operation"}) -ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT = 30 +ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT = 8 +ACCOUNT_RECOMMEND_CONCURRENCY = 8 +ACCOUNT_RECOMMEND_TIMEOUT_SECONDS = 20 PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES = frozenset( { "lock_goods", @@ -587,6 +591,24 @@ def unlink_account( return order +def _recommendation_account_snapshot(account: Account) -> Account: + """复制探针所需的标量字段,避免工作线程访问 SQLAlchemy Session。""" + return Account( + id=int(account.id), + username=str(account.username or ""), + password="", + email="", + email_password="", + cookie=str(account.cookie or ""), + uid=str(account.uid or ""), + nickname=str(account.nickname or ""), + elite_opened=account.elite_opened, + points=account.points, + gold_balance=account.gold_balance, + points_expire_at=account.points_expire_at, + ) + + def recommend_accounts( db: Session, current: User, @@ -619,155 +641,201 @@ def recommend_accounts( 1, min( int(scan_limit or ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT), - 100, + ACCOUNT_RECOMMEND_CONCURRENCY, ), ) - candidates = live_candidate_accounts( + candidates_by_id: dict[int, Account] = {} + if cid: + for account in capability_candidate_accounts( + db, + limit=effective_scan_limit, + required_points=required, + commodity_id=cid, + scope="elite", + ): + candidates_by_id[int(account.id)] = account + for account in live_candidate_accounts( db, limit=effective_scan_limit, required_points=required, commodity_id=cid, prefer_unbound=True, empty_only=not include_rebindable, + ): + candidates_by_id.setdefault(int(account.id), account) + + candidates = [ + account + for account in candidates_by_id.values() + if (not order.account_id or int(account.id) != int(order.account_id)) + and (include_rebindable or account.role_bind_status == "unbound") + ] + candidates.sort( + key=lambda account: ( + 0 if account.role_bind_status == "unbound" else 1, + 0 if account.elite_opened is True else 1, + 0 if (account.points or 0) >= required else 1, + int(account.id), + ) ) + candidates = candidates[:effective_scan_limit] + config = resolve_config(db) - manual_id = str(config.manual_id or "") - rid = str(config.rid or "") - act_alias = str(config.bind_act_alias or "") - confirm_act_alias = str(config.confirm_act_alias or "") - legacy_act_alias = str(config.legacy_act_alias or "") + config_info = config_mapping( + { + "manual_id": str(config.manual_id or ""), + "rid": str(config.rid or ""), + "bind_act_alias": str(config.bind_act_alias or ""), + "confirm_act_alias": str(config.confirm_act_alias or ""), + "legacy_act_alias": str(config.legacy_act_alias or ""), + } + ) output: list[dict[str, Any]] = [] scanned = 0 + result_limit = max(1, min(int(limit), 10)) - for account in candidates: - if order.account_id and int(account.id) == int(order.account_id): - continue - scanned += 1 + def inspect_candidate(account: Account) -> tuple[Account, dict[str, Any]] | None: try: - probe = _probe_with_client( - account, - manual_id=manual_id, - rid=rid, - act_alias=act_alias, - confirm_act_alias=confirm_act_alias, - legacy_act_alias=legacy_act_alias, - client=client_factory(account) if client_factory else None, - with_goods=bool(cid), + active_client = ( + client_factory(account) + if client_factory + else activity_client( + str(account.cookie or ""), + logger_callback=lambda message: logger.debug( + "[douyu-collab] recommend account={} {}", + account.id, + message, + ), + ) ) + probe = probe_account( + account, + config_info, + client=active_client, + session_checker=( + None if client_factory is not None else check_douyu_cookie + ), + with_elite=True, + with_eligibility=False, + with_limited_goods=bool(cid), + with_gold=False, + with_goods=False, + with_elite_role=True, + use_cache=client_factory is None, + ) + return account, probe except Exception as exc: # noqa: BLE001 - 单账号失败继续扫描 logger.info( "[douyu-collab] 推荐账号跳过 account_id={} error={}", account.id, exc, ) - continue + return None - if str(probe.get("ck_status") or "") != CK_VALID: - continue - role_status = str(probe.get("role_status") or ROLE_UNKNOWN) - canonical = probe.get("_probe_result") - canonical_role = ( - (canonical.get("roles") or {}).get("elite") or {} - if isinstance(canonical, Mapping) - else {} - ) + def accept_candidate(account: Account, probe: dict[str, Any]) -> None: + session = probe.get("session") or {} + if str(session.get("status") or "") != SESSION_VALID: + return + role = (probe.get("roles") or {}).get("elite") or {} + role_status = str(role.get("status") or ROLE_UNKNOWN) bind_info = ( - canonical_role.get("bind_info") - if isinstance(canonical_role.get("bind_info"), dict) - else None + role.get("bind_info") if isinstance(role.get("bind_info"), dict) else None ) if role_status == ROLE_UNBOUND: match_type = "empty" match_label = "未绑定" elif role_status == ROLE_BOUND and can_change_role(bind_info): if not include_rebindable: - continue + return match_type = "rebind" match_label = "可换绑" else: - continue + return - canonical_eligibility = ( - canonical.get("eligibility") or {} - if isinstance(canonical, Mapping) - else {} - ) + eligibility = probe.get("eligibility") or {} limited_ids = { str(item) - for item in canonical_eligibility.get("limited_ids") or [] + for item in eligibility.get("limited_ids") or [] if str(item) } if cid and cid in limited_ids: - continue - if cid and has_recent_exchange( - db, - account_id=int(account.id), - commodity_id=cid, - ): - continue + return - goods = probe.get("goods") or [] - target_goods = next( - ( - item - for item in goods - if isinstance(item, Mapping) - and str(item.get("commodity_id") or "") == cid - ), - None, - ) - if target_goods is not None: - if str(target_goods.get("status") or "") == "1": - continue - if _to_int(target_goods.get("open_status")) == 0: - continue - - live_points = _to_int(probe.get("manual_score")) - elite_opened = probe.get("manual_opened") - elite_opened_value = bool(elite_opened) if elite_opened is not None else None + elite = probe.get("elite") or {} + live_points = _to_int(eligibility.get("points")) + if live_points is None: + live_points = _to_int(elite.get("manual_score")) + elite_opened = bool(elite.get("opened")) ready = bool( - elite_opened_value is True - and live_points is not None - and live_points >= required + elite_opened and live_points is not None and live_points >= required ) reasons = [ "未绑定角色,可直接绑定" if match_type == "empty" else "已绑定角色,当前可换绑" ] - if elite_opened_value is not True: + if not elite_opened: reasons.append("精英宝典未开通") if live_points is None or live_points < required: reasons.append(f"积分不足({live_points or 0}/{required})") - if cid and target_goods is None: - reasons.append("商品列表未返回该礼包,绑定后请复核") + identity = probe.get("identity") or {} output.append( { "account_id": int(account.id), # 后续“使用此账号”会按内部 username 反查,不能用接口返回的展示昵称。 "account_username": str(account.username or ""), - "account_uid": str(probe.get("account_uid") or account.uid or ""), + "account_uid": str(identity.get("uid") or account.uid or ""), "account_nickname": str( - probe.get("account_nickname") + identity.get("nickname") or account.nickname or account.username or "" ), - "ck_status": str(probe.get("ck_status") or ""), + "ck_status": CK_VALID, "role_status": role_status, - "bind_game_role": str(probe.get("bind_game_role") or ""), + "bind_game_role": str(role.get("role_name") or ""), "role_change_available_at": role_change_available_at(bind_info), "points": live_points, - "elite_opened": elite_opened_value, + "elite_opened": elite_opened, "match_type": match_type, "match_type_label": match_label, "ready": ready, "reasons": reasons, } ) - if len(output) >= max(1, min(int(limit), 10)): - break + + snapshots = [_recommendation_account_snapshot(account) for account in candidates] + if snapshots: + executor = ThreadPoolExecutor( + max_workers=min(ACCOUNT_RECOMMEND_CONCURRENCY, len(snapshots)) + ) + futures = { + executor.submit(inspect_candidate, account): account + for account in snapshots + } + scanned = len(futures) + try: + for future in as_completed( + futures, + timeout=ACCOUNT_RECOMMEND_TIMEOUT_SECONDS, + ): + result = future.result() + if result is not None: + accept_candidate(*result) + if len(output) >= result_limit: + break + except FutureTimeoutError: + logger.warning( + "[douyu-collab] 推荐账号实时校验超时,已返回当前结果 scanned={} " + "timeout={}s", + scanned, + ACCOUNT_RECOMMEND_TIMEOUT_SECONDS, + ) + finally: + for future in futures: + future.cancel() + executor.shutdown(wait=False, cancel_futures=True) output.sort( key=lambda item: (