虎牙网页Cookie来源驱动合并与WSS传输门禁
This commit is contained in:
@@ -72,18 +72,41 @@ class HuyaBatchRunnerCore:
|
||||
return bucket
|
||||
|
||||
def _activity_client(self, uid: int, cookie: str):
|
||||
"""活动任务优先使用最新活动 WSS,建连失败才回退 HTTP。"""
|
||||
from core.huya.http_client import HuyaHttpClient
|
||||
"""创建活动 WSS 会话;网页 Cookie 不完整时直接阻断任务。"""
|
||||
missing = self._web_wss_cookie_missing(cookie)
|
||||
if missing:
|
||||
detail = ", ".join(missing)
|
||||
self._push_log("error", f"[{uid}] Cookie 缺少网页设备态({detail}),已停止活动 HTTP 回退")
|
||||
raise RuntimeError(f"COOKIE_INCOMPLETE: 缺少网页设备态({detail})")
|
||||
session = self._open_wss_session(uid, cookie, "activity")
|
||||
self._push_log("info", f"[{uid}] 活动链路使用 WSS 会话")
|
||||
return session
|
||||
|
||||
try:
|
||||
session = self._open_wss_session(uid, cookie, "activity")
|
||||
self._push_log("info", f"[{uid}] 活动链路使用 WSS 会话")
|
||||
return session
|
||||
except Exception as exc: # noqa: BLE001 - fallback is part of transport policy
|
||||
self._push_log(
|
||||
"warning", f"[{uid}] 活动 WSS 不可用,回退 HTTP: {type(exc).__name__}"
|
||||
)
|
||||
return HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
||||
@staticmethod
|
||||
def _web_wss_cookie_missing(cookie: str) -> tuple[str, ...]:
|
||||
"""返回 WSS 前置字段中缺失的名称,不输出任何 Cookie 值。"""
|
||||
required = (
|
||||
"udb_cred", "udb_biztoken", "udb_uid", "yyuid", "sdid",
|
||||
"guid", "udb_guiddata", "udb_deviceid", "game_did",
|
||||
"_qimei_uuid42", "udb_anobiztoken", "__yamid_new",
|
||||
)
|
||||
return tuple(key for key in required if not cookie_value(cookie, key))
|
||||
|
||||
@classmethod
|
||||
def _has_web_wss_cookie(cls, cookie: str) -> bool:
|
||||
"""9.1 活动 WSS 需要网页设备态,不接受只有 App 认证字段的 Cookie。"""
|
||||
return not cls._web_wss_cookie_missing(cookie)
|
||||
|
||||
def _shop_client(self, uid: int, cookie: str):
|
||||
"""创建商城 WSS 会话;网页 Cookie 不完整时直接阻断任务。"""
|
||||
missing = self._web_wss_cookie_missing(cookie)
|
||||
if missing:
|
||||
detail = ", ".join(missing)
|
||||
self._push_log("error", f"[{uid}] Cookie 缺少网页设备态({detail}),已停止商城 HTTP 回退")
|
||||
raise RuntimeError(f"COOKIE_INCOMPLETE: 缺少网页设备态({detail})")
|
||||
session = self._open_wss_session(uid, cookie, "shop")
|
||||
self._push_log("info", f"[{uid}] 商城链路使用 WSS 会话")
|
||||
return session
|
||||
|
||||
def _close_wss_sessions(self):
|
||||
bucket = self._session_bucket()
|
||||
|
||||
@@ -8,8 +8,6 @@ from typing import TYPE_CHECKING, Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.huya import HuyaHttpClient
|
||||
|
||||
from ..models import HuyaAccount, HuyaRechargeGoodsSnapshot, HuyaTask
|
||||
|
||||
HUYA_RECHARGE_ACT_ID = 25135
|
||||
@@ -43,6 +41,7 @@ class RechargeMixin:
|
||||
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
|
||||
def _update_task_progress(self, *args: Any, **kwargs: Any) -> None: ...
|
||||
def _open_wss_session(self, uid: int, cookie: str, kind: str): ...
|
||||
def _shop_client(self, uid: int, cookie: str): ...
|
||||
|
||||
@staticmethod
|
||||
def _format_local_time(timestamp: int) -> str: ...
|
||||
@@ -205,9 +204,7 @@ class RechargeMixin:
|
||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||
return
|
||||
|
||||
client: Any = HuyaHttpClient(
|
||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
||||
)
|
||||
client: Any = self._shop_client(uid, cookie)
|
||||
task_resp = client.get_act_task_detail(
|
||||
uid=uid, cookie=cookie, act_id=HUYA_RECHARGE_ACT_ID
|
||||
)
|
||||
@@ -403,14 +400,7 @@ class RechargeMixin:
|
||||
)
|
||||
unit_price = int(snapshot.price or 0) if snapshot else 0
|
||||
|
||||
try:
|
||||
client: Any = self._open_wss_session(uid, cookie, "shop")
|
||||
self._push_log("info", f"[{uid}] 商城链路使用 WSS 会话")
|
||||
except Exception as exc: # noqa: BLE001 - HTTP fallback is explicit
|
||||
self._push_log("warning", f"[{uid}] 商城 WSS 不可用,回退 HTTP: {type(exc).__name__}")
|
||||
client = HuyaHttpClient(
|
||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
||||
)
|
||||
client: Any = self._shop_client(uid, cookie)
|
||||
detail_resp = client.get_goods_info(
|
||||
uid=uid,
|
||||
guid="",
|
||||
@@ -579,10 +569,7 @@ class RechargeMixin:
|
||||
account.updated_at = datetime.now(UTC)
|
||||
if payment_status == "paid":
|
||||
account.status = "recharge_paid"
|
||||
score_client = client if hasattr(client, "query_user_score") else HuyaHttpClient(
|
||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
||||
)
|
||||
post_score = score_client.query_user_score(
|
||||
post_score = client.query_user_score(
|
||||
uid=uid, cookie=cookie, sid=self._to_int(config_info.get("sid")) or 2203
|
||||
)
|
||||
if post_score is not None:
|
||||
|
||||
Reference in New Issue
Block a user