切换虎牙宝典开通任务到WSS会话

This commit is contained in:
yml2213
2026-09-01 12:20:58 +08:00
parent c195430845
commit aab84e0fb5
5 changed files with 175 additions and 4 deletions
+1
View File
@@ -127,6 +127,7 @@ class HuyaBatchRunner(
self._mark_task(worker_db, task, "error", f"执行异常: {exc}")
self._push_log("error", f"[{current}] {name} 执行异常: {exc}")
finally:
self._close_wss_sessions()
worker_db.close()
def run(self):
+23
View File
@@ -7,6 +7,7 @@ import copy
import threading
import time
from datetime import UTC, datetime
from typing import Any
from loguru import logger
from sqlalchemy.orm import Session
@@ -45,10 +46,32 @@ class HuyaBatchRunnerCore:
self._stop = threading.Event()
self._counter_lock = threading.Lock()
self._started = 0
self._wss_sessions: list[Any] = []
def stop(self):
self._stop.set()
def _open_wss_session(self, uid: int, cookie: str, kind: str):
"""在当前任务线程创建并登记 WSS 会话,统一由 finally 回收。"""
from core.huya.elite_session import HuyaEliteWssSession
session = HuyaEliteWssSession(
uid=uid,
cookie=cookie,
kind=kind,
logger=lambda message: self._push_log("info", f"[{uid}] {message}"),
)
self._wss_sessions.append(session)
return session
def _close_wss_sessions(self):
for session in reversed(self._wss_sessions):
try:
session.close()
except Exception: # noqa: BLE001 - cleanup must not mask task status
logger.debug("[huya] WSS 会话清理异常")
self._wss_sessions.clear()
def _push_log(self, level: str, message: str):
if level == "result":
try:
+13 -4
View File
@@ -42,6 +42,7 @@ class RechargeMixin:
def _push_log(self, level: str, message: str) -> None: ...
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): ...
@staticmethod
def _format_local_time(timestamp: int) -> str: ...
@@ -393,9 +394,14 @@ class RechargeMixin:
)
unit_price = int(snapshot.price or 0) if snapshot else 0
client: Any = HuyaHttpClient(
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
)
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}")
)
detail_resp = client.get_goods_info(
uid=uid,
guid="",
@@ -564,7 +570,10 @@ class RechargeMixin:
account.updated_at = datetime.now(UTC)
if payment_status == "paid":
account.status = "recharge_paid"
post_score = client.query_user_score(
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(
uid=uid, cookie=cookie, sid=self._to_int(config_info.get("sid")) or 2203
)
if post_score is not None: