实现虎牙刷新商品列表
This commit is contained in:
@@ -196,7 +196,7 @@ def list_goods(
|
||||
current: User = Depends(require_permission("huya:task")),
|
||||
):
|
||||
"""查看已缓存的虎牙商品快照。"""
|
||||
rows = db.query(HuyaGoodsSnapshot).order_by(HuyaGoodsSnapshot.updated_at.desc()).all()
|
||||
rows = db.query(HuyaGoodsSnapshot).order_by(HuyaGoodsSnapshot.id.asc()).all()
|
||||
return rows
|
||||
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ from sqlalchemy.orm import Session, joinedload
|
||||
|
||||
from core.huya import HuyaHttpClient
|
||||
from ..database import SessionLocal
|
||||
from ..models import HuyaAccount, HuyaTask
|
||||
from ..models import HuyaAccount, HuyaGoodsSnapshot, HuyaTask
|
||||
from .huya_service import HUYA_CONFIG_FIELDS, cookie_value, ensure_huya_config, huya_config_value
|
||||
|
||||
|
||||
@@ -197,6 +197,73 @@ class HuyaBatchRunner:
|
||||
account.updated_at = datetime.now(timezone.utc)
|
||||
self._mark_task(worker_db, task, "success", f"积分: {points}", 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_get_bind_qr(
|
||||
self,
|
||||
worker_db: Session,
|
||||
@@ -556,7 +623,13 @@ class HuyaBatchRunner:
|
||||
name = self._account_name(account_info)
|
||||
self._push_log("info", f"[{current}/{total}] 开始虎牙任务: {name}")
|
||||
|
||||
if self.task_type not in {"query_points", "get_bind_qr", "confirm_bind", "query_game_name"}:
|
||||
if self.task_type not in {
|
||||
"query_points",
|
||||
"get_bind_qr",
|
||||
"confirm_bind",
|
||||
"query_game_name",
|
||||
"refresh_goods",
|
||||
}:
|
||||
self._mark_task(worker_db, task, "failed", "该虎牙任务执行器暂未实现")
|
||||
self._push_log("warning", f"[{current}] {name} 暂未实现: {self.task_type}")
|
||||
return
|
||||
@@ -564,6 +637,8 @@ class HuyaBatchRunner:
|
||||
try:
|
||||
if self.task_type == "query_points":
|
||||
self._execute_query_points(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 == "get_bind_qr":
|
||||
self._execute_get_bind_qr(worker_db, task, account, account_info, config_info)
|
||||
elif self.task_type == "confirm_bind":
|
||||
|
||||
@@ -172,6 +172,9 @@ def create_planned_tasks(
|
||||
batch_id = uuid.uuid4().hex[:12]
|
||||
payload = payload or {}
|
||||
accounts = db.query(HuyaAccount).filter(HuyaAccount.id.in_(account_ids)).all()
|
||||
if task_type == "refresh_goods" and accounts:
|
||||
# 商品列表是全局快照,使用一个可用 CK 刷新即可。
|
||||
accounts = accounts[:1]
|
||||
for account in accounts:
|
||||
db.add(HuyaTask(
|
||||
batch_id=batch_id,
|
||||
|
||||
Reference in New Issue
Block a user