- 手机号列:外部 CK 的 `----手机号` 本来就落在 huya_accounts.game_phone,
列表一直没展示;现在单独成列,并纳入搜索
- 精英宝典列:显示「已开通/未开通/未查询」+ 开通时间 + 有效期
· 开通状态 = 积分中心 getRedeemNeedGoods 的 tag5(真机对照网页:1=已开通,
0=未开通,此时 tag7 会列出开通套餐),拿不准时为 NULL(显示「未查询」)
· 开通时间 = revenueWebUI.queryUserOrderList 里已支付的宝典订单 pay_time
(按 SPU 白名单 + 订单名含「精英宝典」识别,未支付订单不计,取最近一次)
· 有效期 = PlayMallUI.queryAllocation 页面配置的本赛季节点 tag10/tag11
(2026-10-15 抓包:精英宝典福利季丨Q4,2026-10-09 ~ 2026-12-31 23:59:59),
不写死索引,递归找带 tag10/tag11 的节点
- 新任务 query_all「一键查询」:勾选账号一次跑完角色绑定 + 宝典开通 + 积分,
按部分失败如实汇总(某个接口挂了不影响其它结论),并把宝典状态写回账号
- 迁移 20261015_0053:huya_accounts 新增 elite_opened / elite_open_time /
elite_expire_time(时间存 Unix 秒,与斗鱼 elite_expire_time 同约定)
- 开通商品按页面「赠送指定礼物得积分」那一排对齐成 6 款(去掉页面已不用的
旧版精英宝典 hy-5879340),列表接口只返回账号级快照并按 spu 去重
- 建 WSS 会话失败(积分中心端点静默不回包)时回退 HTTP cdnws,
不再让整批任务判失败
- 列表列宽改按比例分配,避免宽屏下被拉出奇怪的宽度;最近操作不再重复状态
- 新增 tests/test_huya_elite_query.py(10 条);同步迁移头版本断言
真机验证:#65 已开通(10-13 20:41:45,有效期 12-31)、#70 新开通
(10-15 16:37:37)判定与订单/网页一致
225 lines
8.9 KiB
Python
225 lines
8.9 KiB
Python
"""虎牙任务批次执行器(入口聚合;功能域已拆分到 huya_runner_*.py)。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from core.huya.cookie_utils import normalize_huya_cookie
|
|
|
|
from ..database import SessionLocal
|
|
from ..models import HuyaAccount, HuyaTask
|
|
from .huya_runner_bind import BindMixin
|
|
from .huya_runner_core import ( # noqa: F401 (huya_batch_registry 供 routers 重导出)
|
|
HuyaBatchRunnerCore,
|
|
huya_batch_registry,
|
|
)
|
|
from .huya_runner_elite import EliteMixin
|
|
from .huya_runner_goods import GoodsMixin
|
|
from .huya_runner_recharge import RechargeMixin
|
|
from .huya_service import HUYA_CONFIG_FIELDS, ensure_huya_config, huya_config_value
|
|
|
|
|
|
class HuyaBatchRunner(
|
|
HuyaBatchRunnerCore,
|
|
BindMixin,
|
|
GoodsMixin,
|
|
RechargeMixin,
|
|
EliteMixin,
|
|
):
|
|
"""批量执行虎牙任务(功能域 Mixin 聚合 + 批次调度)。"""
|
|
|
|
def _execute_one(
|
|
self, task_id: int, account_info: dict, config_info: dict, total: int
|
|
):
|
|
worker_db = SessionLocal()
|
|
try:
|
|
task = worker_db.query(HuyaTask).filter(HuyaTask.id == task_id).first()
|
|
account = (
|
|
worker_db.query(HuyaAccount)
|
|
.filter(HuyaAccount.id == account_info["account_id"])
|
|
.first()
|
|
)
|
|
if not task or not account:
|
|
return
|
|
|
|
self._wss_sessions.account_id = account.id
|
|
|
|
if self._stop.is_set():
|
|
self._mark_task(worker_db, task, "stopped", "任务已停止")
|
|
return
|
|
|
|
task.status = "running"
|
|
task.message = "执行中"
|
|
task.finished_at = None
|
|
worker_db.commit()
|
|
|
|
with self._counter_lock:
|
|
self._started += 1
|
|
current = self._started
|
|
|
|
name = self._account_name(account_info)
|
|
self._push_log("info", f"[{current}/{total}] 开始虎牙任务: {name}")
|
|
|
|
if self.task_type not in {
|
|
"query_act_tasks",
|
|
"query_points",
|
|
"query_all",
|
|
"get_bind_qr",
|
|
"confirm_bind",
|
|
"query_game_name",
|
|
"query_exchange_records",
|
|
"refresh_goods",
|
|
"refresh_recharge_goods",
|
|
"exchange_goods",
|
|
"create_recharge_order",
|
|
}:
|
|
self._mark_task(worker_db, task, "failed", "该虎牙任务执行器暂未实现")
|
|
self._push_log(
|
|
"warning", f"[{current}] {name} 暂未实现: {self.task_type}"
|
|
)
|
|
return
|
|
|
|
try:
|
|
if self.task_type == "query_act_tasks":
|
|
self._execute_query_act_tasks(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "query_points":
|
|
self._execute_query_points(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "query_all":
|
|
self._execute_query_all(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "refresh_goods":
|
|
self._execute_refresh_goods(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "refresh_recharge_goods":
|
|
self._execute_refresh_recharge_goods(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "exchange_goods":
|
|
self._execute_exchange_goods(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "create_recharge_order":
|
|
self._execute_create_recharge_order(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "get_bind_qr":
|
|
self._execute_get_bind_qr(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "confirm_bind":
|
|
self._execute_confirm_bind(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "query_game_name":
|
|
self._execute_query_game_name(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
elif self.task_type == "query_exchange_records":
|
|
self._execute_query_exchange_records(
|
|
worker_db, task, account, account_info, config_info
|
|
)
|
|
worker_db.refresh(task)
|
|
if task.status == "success":
|
|
self._push_log("success", f"[{current}] {name} {task.message}")
|
|
else:
|
|
self._push_log("error", f"[{current}] {name} {task.message}")
|
|
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
|
self._mark_task(worker_db, task, "error", f"执行异常: {exc}")
|
|
self._push_log("error", f"[{current}] {name} 执行异常: {exc}")
|
|
finally:
|
|
self._wss_sessions.account_id = None
|
|
self._close_wss_sessions()
|
|
worker_db.close()
|
|
|
|
def run(self):
|
|
"""在线程中执行虎牙批次任务。"""
|
|
self._push_log(
|
|
"info",
|
|
f"虎牙批次 {self.batch_id} 开始,共执行 {self.task_type},并发数: {self.concurrency}",
|
|
)
|
|
try:
|
|
config = ensure_huya_config(self.db)
|
|
config_info = {
|
|
field: huya_config_value(field, getattr(config, field, None))
|
|
for field in HUYA_CONFIG_FIELDS
|
|
}
|
|
|
|
tasks = (
|
|
self.db.query(HuyaTask)
|
|
.options(joinedload(HuyaTask.account))
|
|
.filter(HuyaTask.batch_id == self.batch_id)
|
|
.order_by(HuyaTask.id.asc())
|
|
.all()
|
|
)
|
|
|
|
task_infos = []
|
|
for task in tasks:
|
|
account = task.account
|
|
if not account:
|
|
task.status = "error"
|
|
task.message = "账号不存在"
|
|
task.finished_at = datetime.now(UTC)
|
|
continue
|
|
task.status = "pending"
|
|
task.message = "等待执行"
|
|
task.finished_at = None
|
|
task_infos.append(
|
|
{
|
|
"task_id": task.id,
|
|
"account_info": {
|
|
"account_id": account.id,
|
|
"uid": account.uid or "",
|
|
"yyuid": account.yyuid or "",
|
|
"username": account.username or "",
|
|
"nickname": account.nickname or "",
|
|
"cookie": normalize_huya_cookie(account.cookie or ""),
|
|
},
|
|
}
|
|
)
|
|
self.db.commit()
|
|
|
|
total = len(task_infos)
|
|
if total == 0:
|
|
self._push_log("warning", "没有可执行的虎牙任务")
|
|
self._push_log("result", "")
|
|
return
|
|
|
|
with ThreadPoolExecutor(max_workers=self.concurrency) as executor:
|
|
futures = []
|
|
for item in task_infos:
|
|
if self._stop.is_set():
|
|
self._push_log("warning", "任务已停止,跳过剩余账号")
|
|
break
|
|
futures.append(
|
|
executor.submit(
|
|
self._execute_one,
|
|
item["task_id"],
|
|
item["account_info"],
|
|
config_info,
|
|
total,
|
|
)
|
|
)
|
|
|
|
for future in as_completed(futures):
|
|
try:
|
|
future.result()
|
|
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
|
self._push_log("error", f"虎牙 Worker 异常: {exc}")
|
|
|
|
self._push_log("info", f"虎牙批次 {self.batch_id} 完成")
|
|
self._push_log("result", "")
|
|
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
|
self._push_log("error", f"虎牙批次执行异常: {exc}")
|
|
self._push_log("result", "")
|
|
finally:
|
|
self.db.close()
|