feat(huya): 统一实时商品目录并增加兑换代理开关
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
"""虎牙工作台:配置、商品快照、任务批次与日志。"""
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi import APIRouter, Response
|
||||
|
||||
from core.huya.cookie_utils import cookie_value
|
||||
from core.huya.http_client import HuyaHttpClient
|
||||
|
||||
from ...schemas import HuyaWorkbenchTransferToCollab
|
||||
from ...services.huya_points_center import (
|
||||
fetch_points_center_catalog,
|
||||
resolve_points_center_config,
|
||||
)
|
||||
from ...services.huya_service import ensure_huya_config
|
||||
from ._common import *
|
||||
|
||||
router = APIRouter()
|
||||
@@ -169,6 +177,7 @@ def list_goods(
|
||||
rows = (
|
||||
db.query(HuyaGoodsSnapshot)
|
||||
.filter(HuyaGoodsSnapshot.account_id.is_not(None))
|
||||
.filter(HuyaGoodsSnapshot.product_id.like("hy-%"))
|
||||
.order_by(
|
||||
HuyaGoodsSnapshot.updated_at.desc(),
|
||||
HuyaGoodsSnapshot.id.desc(),
|
||||
@@ -186,6 +195,65 @@ def list_goods(
|
||||
return list(latest.values())
|
||||
|
||||
|
||||
@router.get("/goods/live", response_model=list[HuyaGoodsOut])
|
||||
def list_live_goods(
|
||||
account_id: int = Query(..., ge=1),
|
||||
response: Response = None,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("huya:task")),
|
||||
):
|
||||
"""按账号 CK 实时读取积分中心商品,不使用数据库快照。"""
|
||||
if response is not None:
|
||||
response.headers["Cache-Control"] = "no-store, max-age=0"
|
||||
_require_huya_task_account_access(db, current, [account_id])
|
||||
account = db.query(HuyaAccount).filter(HuyaAccount.id == account_id).first()
|
||||
if account is None:
|
||||
raise HTTPException(status_code=404, detail="虎牙账号不存在")
|
||||
cookie = normalize_huya_cookie(account.cookie or "")
|
||||
if not cookie:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="账号 Cookie 为空,无法读取实时商品"
|
||||
)
|
||||
uid = (
|
||||
int(account.uid or account.yyuid or 0)
|
||||
or int(cookie_value(cookie, "yyuid") or 0)
|
||||
or int(cookie_value(cookie, "udb_uid") or 0)
|
||||
)
|
||||
if not uid:
|
||||
raise HTTPException(status_code=400, detail="无法解析账号 UID")
|
||||
|
||||
query_options = resolve_points_center_config(db)
|
||||
catalog = fetch_points_center_catalog(
|
||||
uid,
|
||||
cookie,
|
||||
client=HuyaHttpClient(
|
||||
logger=lambda value: logger.debug(f"[huya-live-goods] {value}")
|
||||
),
|
||||
**query_options,
|
||||
)
|
||||
if not catalog.goods:
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=f"实时商品加载失败:{catalog.message}",
|
||||
)
|
||||
now = datetime.now(UTC)
|
||||
sid = catalog.sid or 0
|
||||
return [
|
||||
HuyaGoodsOut(
|
||||
id=-(index + 1),
|
||||
account_id=account.id,
|
||||
sid=sid,
|
||||
product_id=str(item["product_id"]),
|
||||
name=str(item.get("name") or ""),
|
||||
price=item.get("price"),
|
||||
remain_text=str(item.get("remain_text") or ""),
|
||||
raw=item,
|
||||
updated_at=now,
|
||||
)
|
||||
for index, item in enumerate(catalog.goods)
|
||||
]
|
||||
|
||||
|
||||
@router.get("/recharge-goods", response_model=list[HuyaRechargeGoodsOut])
|
||||
def list_recharge_goods(
|
||||
db: Session = Depends(get_db),
|
||||
|
||||
Reference in New Issue
Block a user