huya_runner.py (1664行) → core/bind/goods/recharge 四个 Mixin + 入口聚合: - huya_runner_core.py: 批次状态/日志/任务落库/注册表 + 共用常量 sourceId - huya_runner_bind.py: 绑定状态机/扫码确认绑定/轮询 - huya_runner_goods.py: 积分/兑换记录/商品刷新/兑换 - huya_runner_recharge.py: 充值商品/下单/到账轮询 - 对外 API 不变 (HuyaBatchRunner/huya_batch_registry), routers 零改动 - pyflakes 全净 (含 TYPE_CHECKING 注解/跨域常量导入修复), 99 单测通过
268 lines
9.7 KiB
Python
268 lines
9.7 KiB
Python
"""虎牙任务执行器:积分与商城(由 huya_runner.py 按功能域拆分)。"""
|
|
|
|
from __future__ import annotations
|
|
import time
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.huya import HuyaHttpClient
|
|
from ..models import HuyaAccount, HuyaGoodsSnapshot, HuyaTask
|
|
|
|
class GoodsMixin:
|
|
"""积分与商城域:积分/兑换记录/商品刷新/兑换。"""
|
|
def _execute_query_points(
|
|
self,
|
|
worker_db: Session,
|
|
task: HuyaTask,
|
|
account: HuyaAccount,
|
|
account_info: dict,
|
|
config_info: dict,
|
|
):
|
|
sid = str(self.payload.get("sid") or config_info.get("sid") or "").strip()
|
|
if not sid:
|
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙活动 SID")
|
|
return
|
|
|
|
sid_int = self._to_int(sid)
|
|
if not sid_int:
|
|
self._mark_task(worker_db, task, "failed", f"虎牙活动 SID 无效: {sid}")
|
|
return
|
|
|
|
uid = self._resolve_uid(account_info)
|
|
if not uid:
|
|
self._mark_task(worker_db, task, "failed", "无法从账号或 Cookie 解析 yyuid")
|
|
return
|
|
|
|
cookie = account_info.get("cookie") or ""
|
|
if not cookie:
|
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
|
return
|
|
|
|
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
|
response = client.query_user_score(uid=uid, cookie=cookie, sid=sid_int)
|
|
if response is None:
|
|
self._mark_task(worker_db, task, "error", "虎牙积分接口无响应")
|
|
return
|
|
|
|
result = response.to_dict()
|
|
result["sid"] = sid_int
|
|
if response.status != 200:
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
response.msg or f"虎牙积分查询失败: {response.status}",
|
|
result,
|
|
)
|
|
return
|
|
|
|
points = response.available_score
|
|
account.points = points
|
|
account.status = "points_queried"
|
|
account.updated_at = datetime.now(timezone.utc)
|
|
self._mark_task(worker_db, task, "success", f"积分: {points}", result)
|
|
|
|
def _execute_query_exchange_records(
|
|
self,
|
|
worker_db: Session,
|
|
task: HuyaTask,
|
|
account: HuyaAccount,
|
|
account_info: dict,
|
|
config_info: dict,
|
|
):
|
|
sid = str(self.payload.get("sid") or config_info.get("sid") or "").strip()
|
|
if not sid:
|
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙活动 SID")
|
|
return
|
|
|
|
sid_int = self._to_int(sid)
|
|
if not sid_int:
|
|
self._mark_task(worker_db, task, "failed", f"虎牙活动 SID 无效: {sid}")
|
|
return
|
|
|
|
uid = self._resolve_uid(account_info)
|
|
if not uid:
|
|
self._mark_task(worker_db, task, "failed", "无法从账号或 Cookie 解析 yyuid")
|
|
return
|
|
|
|
cookie = account_info.get("cookie") or ""
|
|
if not cookie:
|
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
|
return
|
|
|
|
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
|
response = client.get_user_prize_records(uid=uid, cookie=cookie, sid=sid_int)
|
|
if response is None:
|
|
self._mark_task(worker_db, task, "error", "虎牙兑换记录接口无响应")
|
|
return
|
|
|
|
result = response.to_dict()
|
|
result["sid"] = sid_int
|
|
if response.status != 200:
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
response.msg or f"虎牙兑换记录查询失败: {response.status}",
|
|
result,
|
|
)
|
|
return
|
|
|
|
records = result.get("records", [])
|
|
for index, item in enumerate(records, start=1):
|
|
item["index"] = index
|
|
item["exchange_time_text"] = self._format_local_time(int(item.get("exchange_time") or 0))
|
|
if item.get("score") is not None:
|
|
item["score_text"] = f"{int(item.get('score') or 0)}积分"
|
|
|
|
account.status = "exchange_records_queried"
|
|
account.updated_at = datetime.now(timezone.utc)
|
|
count = len(records)
|
|
message = f"兑换记录 {count} 条" if count else "暂无兑换记录"
|
|
self._mark_task(worker_db, task, "success", message, result)
|
|
|
|
def _execute_refresh_goods(
|
|
self,
|
|
worker_db: Session,
|
|
task: HuyaTask,
|
|
account: HuyaAccount,
|
|
account_info: dict,
|
|
config_info: dict,
|
|
):
|
|
sid = str(self.payload.get("sid") or config_info.get("sid") or "").strip()
|
|
if not sid:
|
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙活动 SID")
|
|
return
|
|
|
|
sid_int = self._to_int(sid)
|
|
if not sid_int:
|
|
self._mark_task(worker_db, task, "failed", f"虎牙活动 SID 无效: {sid}")
|
|
return
|
|
|
|
uid = self._resolve_uid(account_info)
|
|
if not uid:
|
|
self._mark_task(worker_db, task, "failed", "无法从账号或 Cookie 解析 yyuid")
|
|
return
|
|
|
|
cookie = account_info.get("cookie") or ""
|
|
if not cookie:
|
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
|
return
|
|
|
|
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
|
response = client.get_act_prize_list(uid=uid, cookie=cookie, sid=sid_int)
|
|
if response is None:
|
|
self._mark_task(worker_db, task, "error", "虎牙商品列表接口无响应")
|
|
return
|
|
|
|
result = response.to_dict()
|
|
result["sid"] = sid_int
|
|
if response.status != 200:
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
response.msg or f"虎牙商品列表刷新失败: {response.status}",
|
|
result,
|
|
)
|
|
return
|
|
|
|
goods = [
|
|
item for item in result.get("goods", [])
|
|
if item.get("product_id") and item.get("name")
|
|
]
|
|
now = datetime.now(timezone.utc)
|
|
worker_db.query(HuyaGoodsSnapshot).delete(synchronize_session=False)
|
|
for item in goods:
|
|
worker_db.add(HuyaGoodsSnapshot(
|
|
product_id=item["product_id"],
|
|
name=item["name"],
|
|
price=item["price"],
|
|
remain_text=item["remain_text"],
|
|
raw=item,
|
|
updated_at=now,
|
|
))
|
|
|
|
account.status = "goods_refreshed"
|
|
account.updated_at = now
|
|
message = f"已刷新商品 {len(goods)} 个"
|
|
self._mark_task(worker_db, task, "success", message, {**result, "goods": goods})
|
|
|
|
def _execute_exchange_goods(
|
|
self,
|
|
worker_db: Session,
|
|
task: HuyaTask,
|
|
account: HuyaAccount,
|
|
account_info: dict,
|
|
config_info: dict,
|
|
):
|
|
sid = str(self.payload.get("sid") or config_info.get("sid") or "").strip()
|
|
if not sid:
|
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙活动 SID")
|
|
return
|
|
|
|
sid_int = self._to_int(sid)
|
|
if not sid_int:
|
|
self._mark_task(worker_db, task, "failed", f"虎牙活动 SID 无效: {sid}")
|
|
return
|
|
|
|
product_id = self._to_int(self.payload.get("product_id"))
|
|
if not product_id:
|
|
self._mark_task(worker_db, task, "failed", "请选择兑换商品")
|
|
return
|
|
|
|
uid = self._resolve_uid(account_info)
|
|
if not uid:
|
|
self._mark_task(worker_db, task, "failed", "无法从账号或 Cookie 解析 yyuid")
|
|
return
|
|
|
|
cookie = account_info.get("cookie") or ""
|
|
if not cookie:
|
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
|
return
|
|
|
|
snapshot = worker_db.query(HuyaGoodsSnapshot).filter(
|
|
HuyaGoodsSnapshot.product_id == str(product_id)
|
|
).first()
|
|
product_name = str(self.payload.get("product_name") or (snapshot.name if snapshot else "") or product_id)
|
|
scheduled_at = self._parse_scheduled_time(self.payload.get("scheduled_at"))
|
|
if self.payload.get("scheduled_at") and scheduled_at is None:
|
|
self._mark_task(worker_db, task, "failed", "定时兑换时间格式无效")
|
|
return
|
|
if scheduled_at and scheduled_at.timestamp() > time.time():
|
|
if not self._wait_until(scheduled_at, uid):
|
|
self._mark_task(worker_db, task, "stopped", "兑换任务已停止")
|
|
return
|
|
|
|
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
|
response = client.score_exchange_prize(uid=uid, cookie=cookie, sid=sid_int, pid=product_id)
|
|
if response is None:
|
|
self._mark_task(worker_db, task, "error", "虎牙兑换接口无响应")
|
|
return
|
|
|
|
result = response.to_dict()
|
|
result.update({
|
|
"sid": sid_int,
|
|
"product_id": str(product_id),
|
|
"product_name": product_name,
|
|
"scheduled_at": scheduled_at.isoformat() if scheduled_at else "",
|
|
"executed_at": datetime.now(timezone.utc).isoformat(),
|
|
"goods": snapshot.raw if snapshot else None,
|
|
})
|
|
if response.status != 200:
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
response.msg or f"虎牙兑换失败: {response.status}",
|
|
result,
|
|
)
|
|
return
|
|
|
|
account.status = "goods_exchanged"
|
|
account.updated_at = datetime.now(timezone.utc)
|
|
message = response.msg or f"兑换成功: {product_name}"
|
|
self._mark_task(worker_db, task, "success", message, result)
|
|
|