切换虎牙宝典活动任务到WSS会话
This commit is contained in:
@@ -28,6 +28,7 @@ class HuyaEliteWssSession:
|
|||||||
):
|
):
|
||||||
self.uid = int(uid or 0)
|
self.uid = int(uid or 0)
|
||||||
self.cookie = cookie or ""
|
self.cookie = cookie or ""
|
||||||
|
self.guid = HuyaHttpClient._resolve_cookie_guid(self.cookie)
|
||||||
self.kind = kind
|
self.kind = kind
|
||||||
self.logger = logger or (lambda _message: None)
|
self.logger = logger or (lambda _message: None)
|
||||||
self.loop = asyncio.new_event_loop()
|
self.loop = asyncio.new_event_loop()
|
||||||
@@ -35,9 +36,8 @@ class HuyaEliteWssSession:
|
|||||||
self._closed = False
|
self._closed = False
|
||||||
|
|
||||||
if kind == "activity":
|
if kind == "activity":
|
||||||
guid = HuyaHttpClient._resolve_cookie_guid(self.cookie)
|
|
||||||
baseinfo = urllib.parse.unquote(
|
baseinfo = urllib.parse.unquote(
|
||||||
generate_http_baseinfo(self.uid, guid, "")
|
generate_http_baseinfo(self.uid, self.guid, "")
|
||||||
)
|
)
|
||||||
connect_host = host or ACTIVITY_WS_HOST
|
connect_host = host or ACTIVITY_WS_HOST
|
||||||
origin = "https://zt.huya.com"
|
origin = "https://zt.huya.com"
|
||||||
@@ -52,7 +52,7 @@ class HuyaEliteWssSession:
|
|||||||
self.client.connect(host=connect_host, origin=origin, cookie=self.cookie)
|
self.client.connect(host=connect_host, origin=origin, cookie=self.cookie)
|
||||||
)
|
)
|
||||||
initialized = self.loop.run_until_complete(
|
initialized = self.loop.run_until_complete(
|
||||||
self.client.initialize_activity(self.uid, "", self.cookie)
|
self.client.initialize_activity(self.uid, self.guid, self.cookie)
|
||||||
if kind == "activity"
|
if kind == "activity"
|
||||||
else self.client.initialize(self.uid, "", self.cookie)
|
else self.client.initialize(self.uid, "", self.cookie)
|
||||||
)
|
)
|
||||||
@@ -96,6 +96,9 @@ class HuyaEliteWssSession:
|
|||||||
def get_user_score(self, uid: int, cookie: str, sid: int):
|
def get_user_score(self, uid: int, cookie: str, sid: int):
|
||||||
return self._run(self.client.get_user_score(uid, cookie, sid))
|
return self._run(self.client.get_user_score(uid, cookie, sid))
|
||||||
|
|
||||||
|
def query_user_score(self, uid: int, cookie: str, sid: int):
|
||||||
|
return self.get_user_score(uid, cookie, sid)
|
||||||
|
|
||||||
def get_act_prize_list(self, uid: int, cookie: str, sid: int):
|
def get_act_prize_list(self, uid: int, cookie: str, sid: int):
|
||||||
return self._run(self.client.get_act_prize_list(uid, cookie, sid))
|
return self._run(self.client.get_act_prize_list(uid, cookie, sid))
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,20 @@ class HuyaBatchRunnerCore:
|
|||||||
self._wss_sessions.append(session)
|
self._wss_sessions.append(session)
|
||||||
return session
|
return session
|
||||||
|
|
||||||
|
def _activity_client(self, uid: int, cookie: str):
|
||||||
|
"""活动任务优先使用最新活动 WSS,建连失败才回退 HTTP。"""
|
||||||
|
from core.huya.http_client import HuyaHttpClient
|
||||||
|
|
||||||
|
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}"))
|
||||||
|
|
||||||
def _close_wss_sessions(self):
|
def _close_wss_sessions(self):
|
||||||
for session in reversed(self._wss_sessions):
|
for session in reversed(self._wss_sessions):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ from typing import TYPE_CHECKING, Any
|
|||||||
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from core.huya import HuyaHttpClient
|
|
||||||
|
|
||||||
from ..models import HuyaAccount, HuyaGoodsSnapshot, HuyaTask
|
from ..models import HuyaAccount, HuyaGoodsSnapshot, HuyaTask
|
||||||
|
|
||||||
|
|
||||||
@@ -27,6 +25,7 @@ class GoodsMixin:
|
|||||||
|
|
||||||
def _resolve_uid(self, account_info: dict) -> int: ...
|
def _resolve_uid(self, account_info: dict) -> int: ...
|
||||||
def _push_log(self, level: str, message: str) -> None: ...
|
def _push_log(self, level: str, message: str) -> None: ...
|
||||||
|
def _activity_client(self, uid: int, cookie: str): ...
|
||||||
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
|
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
|
||||||
def _wait_until(self, when: datetime, uid: int) -> bool: ...
|
def _wait_until(self, when: datetime, uid: int) -> bool: ...
|
||||||
def _parse_scheduled_time(self, value: Any) -> datetime | None: ...
|
def _parse_scheduled_time(self, value: Any) -> datetime | None: ...
|
||||||
@@ -49,9 +48,7 @@ class GoodsMixin:
|
|||||||
if not act_id:
|
if not act_id:
|
||||||
self._mark_task(worker_db, task, "failed", "精英宝典活动 ID 无效")
|
self._mark_task(worker_db, task, "failed", "精英宝典活动 ID 无效")
|
||||||
return
|
return
|
||||||
client: Any = HuyaHttpClient(
|
client: Any = self._activity_client(uid, cookie)
|
||||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
|
||||||
)
|
|
||||||
response = client.get_act_task_detail(uid=uid, cookie=cookie, act_id=act_id)
|
response = client.get_act_task_detail(uid=uid, cookie=cookie, act_id=act_id)
|
||||||
if response is None:
|
if response is None:
|
||||||
self._mark_task(worker_db, task, "error", "虎牙活动任务接口无响应")
|
self._mark_task(worker_db, task, "error", "虎牙活动任务接口无响应")
|
||||||
@@ -111,9 +108,7 @@ class GoodsMixin:
|
|||||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||||
return
|
return
|
||||||
|
|
||||||
client: Any = HuyaHttpClient(
|
client: Any = self._activity_client(uid, cookie)
|
||||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
|
||||||
)
|
|
||||||
response = client.query_user_score(uid=uid, cookie=cookie, sid=sid_int)
|
response = client.query_user_score(uid=uid, cookie=cookie, sid=sid_int)
|
||||||
if response is None:
|
if response is None:
|
||||||
self._mark_task(worker_db, task, "error", "虎牙积分接口无响应")
|
self._mark_task(worker_db, task, "error", "虎牙积分接口无响应")
|
||||||
@@ -165,9 +160,7 @@ class GoodsMixin:
|
|||||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||||
return
|
return
|
||||||
|
|
||||||
client: Any = HuyaHttpClient(
|
client: Any = self._activity_client(uid, cookie)
|
||||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
|
||||||
)
|
|
||||||
response = client.get_user_prize_records(uid=uid, cookie=cookie, sid=sid_int)
|
response = client.get_user_prize_records(uid=uid, cookie=cookie, sid=sid_int)
|
||||||
if response is None:
|
if response is None:
|
||||||
self._mark_task(worker_db, task, "error", "虎牙兑换记录接口无响应")
|
self._mark_task(worker_db, task, "error", "虎牙兑换记录接口无响应")
|
||||||
@@ -228,9 +221,7 @@ class GoodsMixin:
|
|||||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||||
return
|
return
|
||||||
|
|
||||||
client: Any = HuyaHttpClient(
|
client: Any = self._activity_client(uid, cookie)
|
||||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
|
||||||
)
|
|
||||||
response = client.get_act_prize_list(uid=uid, cookie=cookie, sid=sid_int)
|
response = client.get_act_prize_list(uid=uid, cookie=cookie, sid=sid_int)
|
||||||
if response is None:
|
if response is None:
|
||||||
self._mark_task(worker_db, task, "error", "虎牙商品列表接口无响应")
|
self._mark_task(worker_db, task, "error", "虎牙商品列表接口无响应")
|
||||||
@@ -327,9 +318,7 @@ class GoodsMixin:
|
|||||||
self._mark_task(worker_db, task, "stopped", "兑换任务已停止")
|
self._mark_task(worker_db, task, "stopped", "兑换任务已停止")
|
||||||
return
|
return
|
||||||
|
|
||||||
client: Any = HuyaHttpClient(
|
client: Any = self._activity_client(uid, cookie)
|
||||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
|
||||||
)
|
|
||||||
detail = client.get_act_prize_detail(
|
detail = client.get_act_prize_detail(
|
||||||
uid=uid, cookie=cookie, sid=sid_int, pid=product_id
|
uid=uid, cookie=cookie, sid=sid_int, pid=product_id
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user