优化虎牙默认配置
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
"""虎牙活动默认配置。"""
|
||||
|
||||
HUYA_DEFAULT_ROOM_PID = "1199650619883"
|
||||
HUYA_DEFAULT_SID = "2203"
|
||||
HUYA_DEFAULT_OUTER_ACT_ID = "9504"
|
||||
HUYA_DEFAULT_BIND_ACT_ID = "17096"
|
||||
HUYA_DEFAULT_PAY_CHANNEL = "Zfb"
|
||||
|
||||
HUYA_CONFIG_DEFAULTS = {
|
||||
"room_pid": HUYA_DEFAULT_ROOM_PID,
|
||||
"sid": HUYA_DEFAULT_SID,
|
||||
"outer_act_id": HUYA_DEFAULT_OUTER_ACT_ID,
|
||||
"bind_act_id": HUYA_DEFAULT_BIND_ACT_ID,
|
||||
"pay_channel": HUYA_DEFAULT_PAY_CHANNEL,
|
||||
}
|
||||
|
||||
HUYA_CONFIG_FIELDS = tuple(HUYA_CONFIG_DEFAULTS)
|
||||
+12
-5
@@ -7,6 +7,13 @@ from sqlalchemy import (
|
||||
from sqlalchemy.orm import relationship
|
||||
from .database import Base
|
||||
from .crypto_storage import EncryptedText
|
||||
from .huya_defaults import (
|
||||
HUYA_DEFAULT_BIND_ACT_ID,
|
||||
HUYA_DEFAULT_OUTER_ACT_ID,
|
||||
HUYA_DEFAULT_PAY_CHANNEL,
|
||||
HUYA_DEFAULT_ROOM_PID,
|
||||
HUYA_DEFAULT_SID,
|
||||
)
|
||||
|
||||
|
||||
def _utcnow():
|
||||
@@ -119,11 +126,11 @@ class HuyaConfig(Base):
|
||||
__tablename__ = "huya_config"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
room_pid = Column(String(64), default="")
|
||||
sid = Column(String(32), default="")
|
||||
outer_act_id = Column(String(32), default="9504")
|
||||
bind_act_id = Column(String(32), default="17096")
|
||||
pay_channel = Column(String(16), default="Zfb")
|
||||
room_pid = Column(String(64), default=HUYA_DEFAULT_ROOM_PID)
|
||||
sid = Column(String(32), default=HUYA_DEFAULT_SID)
|
||||
outer_act_id = Column(String(32), default=HUYA_DEFAULT_OUTER_ACT_ID)
|
||||
bind_act_id = Column(String(32), default=HUYA_DEFAULT_BIND_ACT_ID)
|
||||
pay_channel = Column(String(16), default=HUYA_DEFAULT_PAY_CHANNEL)
|
||||
updated_at = Column(DateTime, default=_utcnow, onupdate=_utcnow)
|
||||
|
||||
|
||||
|
||||
+18
-17
@@ -20,9 +20,12 @@ from ..schemas import (
|
||||
HuyaTaskOut,
|
||||
)
|
||||
from ..services.huya_service import (
|
||||
HUYA_CONFIG_FIELDS,
|
||||
SUPPORTED_TASK_TYPES,
|
||||
apply_huya_config_defaults,
|
||||
create_planned_tasks,
|
||||
ensure_huya_config,
|
||||
huya_config_value,
|
||||
import_huya_cookies,
|
||||
)
|
||||
from ..services.huya_runner import HuyaBatchRunner, huya_batch_registry
|
||||
@@ -79,6 +82,17 @@ def _task_out(task: HuyaTask) -> HuyaTaskOut:
|
||||
)
|
||||
|
||||
|
||||
def _config_out(config: HuyaConfig) -> HuyaConfigOut:
|
||||
return HuyaConfigOut(
|
||||
room_pid=huya_config_value("room_pid", config.room_pid),
|
||||
sid=huya_config_value("sid", config.sid),
|
||||
outer_act_id=huya_config_value("outer_act_id", config.outer_act_id),
|
||||
bind_act_id=huya_config_value("bind_act_id", config.bind_act_id),
|
||||
pay_channel=huya_config_value("pay_channel", config.pay_channel),
|
||||
updated_at=config.updated_at,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/task-types")
|
||||
def task_types(current: User = Depends(require_permission("huya:task"))):
|
||||
"""返回当前规划的虎牙任务类型。"""
|
||||
@@ -154,14 +168,7 @@ def get_config(
|
||||
):
|
||||
"""获取虎牙配置。"""
|
||||
config = ensure_huya_config(db)
|
||||
return HuyaConfigOut(
|
||||
room_pid=config.room_pid or "",
|
||||
sid=config.sid or "",
|
||||
outer_act_id=config.outer_act_id or "9504",
|
||||
bind_act_id=config.bind_act_id or "17096",
|
||||
pay_channel=config.pay_channel or "Zfb",
|
||||
updated_at=config.updated_at,
|
||||
)
|
||||
return _config_out(config)
|
||||
|
||||
|
||||
@router.put("/config", response_model=HuyaConfigOut)
|
||||
@@ -172,21 +179,15 @@ def update_config(
|
||||
):
|
||||
"""更新虎牙配置。"""
|
||||
config = ensure_huya_config(db)
|
||||
for field in ("room_pid", "sid", "outer_act_id", "bind_act_id", "pay_channel"):
|
||||
for field in HUYA_CONFIG_FIELDS:
|
||||
value = getattr(req, field)
|
||||
if value is not None:
|
||||
setattr(config, field, value.strip())
|
||||
apply_huya_config_defaults(config)
|
||||
config.updated_at = datetime.now(timezone.utc)
|
||||
db.commit()
|
||||
db.refresh(config)
|
||||
return HuyaConfigOut(
|
||||
room_pid=config.room_pid or "",
|
||||
sid=config.sid or "",
|
||||
outer_act_id=config.outer_act_id or "9504",
|
||||
bind_act_id=config.bind_act_id or "17096",
|
||||
pay_channel=config.pay_channel or "Zfb",
|
||||
updated_at=config.updated_at,
|
||||
)
|
||||
return _config_out(config)
|
||||
|
||||
|
||||
@router.get("/goods", response_model=list[HuyaGoodsOut])
|
||||
|
||||
+13
-5
@@ -4,6 +4,14 @@ from datetime import datetime, timezone
|
||||
from typing import Optional, Any
|
||||
from pydantic import BaseModel, Field, ConfigDict, model_serializer
|
||||
|
||||
from .huya_defaults import (
|
||||
HUYA_DEFAULT_BIND_ACT_ID,
|
||||
HUYA_DEFAULT_OUTER_ACT_ID,
|
||||
HUYA_DEFAULT_PAY_CHANNEL,
|
||||
HUYA_DEFAULT_ROOM_PID,
|
||||
HUYA_DEFAULT_SID,
|
||||
)
|
||||
|
||||
|
||||
def _ensure_tz(dt: Optional[datetime]) -> Optional[datetime]:
|
||||
"""确保 datetime 带有 UTC 时区信息,无时区的视为 UTC。"""
|
||||
@@ -214,11 +222,11 @@ class HuyaAccountOut(BaseModel):
|
||||
|
||||
|
||||
class HuyaConfigOut(BaseModel):
|
||||
room_pid: str = ""
|
||||
sid: str = ""
|
||||
outer_act_id: str = "9504"
|
||||
bind_act_id: str = "17096"
|
||||
pay_channel: str = "Zfb"
|
||||
room_pid: str = HUYA_DEFAULT_ROOM_PID
|
||||
sid: str = HUYA_DEFAULT_SID
|
||||
outer_act_id: str = HUYA_DEFAULT_OUTER_ACT_ID
|
||||
bind_act_id: str = HUYA_DEFAULT_BIND_ACT_ID
|
||||
pay_channel: str = HUYA_DEFAULT_PAY_CHANNEL
|
||||
updated_at: Optional[datetime] = None
|
||||
|
||||
@model_serializer
|
||||
|
||||
@@ -11,8 +11,8 @@ from sqlalchemy.orm import Session, joinedload
|
||||
|
||||
from core.huya import HuyaHttpClient
|
||||
from ..database import SessionLocal
|
||||
from ..models import HuyaAccount, HuyaConfig, HuyaTask
|
||||
from .huya_service import cookie_value
|
||||
from ..models import HuyaAccount, HuyaTask
|
||||
from .huya_service import HUYA_CONFIG_FIELDS, cookie_value, ensure_huya_config, huya_config_value
|
||||
|
||||
|
||||
class HuyaBatchRunner:
|
||||
@@ -190,12 +190,10 @@ class HuyaBatchRunner:
|
||||
f"虎牙批次 {self.batch_id} 开始,共执行 {self.task_type},并发数: {self.concurrency}",
|
||||
)
|
||||
try:
|
||||
config = self.db.query(HuyaConfig).first()
|
||||
config = ensure_huya_config(self.db)
|
||||
config_info = {
|
||||
"sid": config.sid if config else "",
|
||||
"outer_act_id": config.outer_act_id if config else "",
|
||||
"bind_act_id": config.bind_act_id if config else "",
|
||||
"pay_channel": config.pay_channel if config else "",
|
||||
field: huya_config_value(field, getattr(config, field, None))
|
||||
for field in HUYA_CONFIG_FIELDS
|
||||
}
|
||||
|
||||
tasks = (
|
||||
|
||||
@@ -6,6 +6,7 @@ from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..huya_defaults import HUYA_CONFIG_DEFAULTS, HUYA_CONFIG_FIELDS
|
||||
from ..models import HuyaAccount, HuyaConfig, HuyaTask
|
||||
|
||||
|
||||
@@ -21,6 +22,23 @@ SUPPORTED_TASK_TYPES = {
|
||||
}
|
||||
|
||||
|
||||
def huya_config_value(field: str, value: str | None) -> str:
|
||||
"""读取配置值;空值自动回退到当前活动默认配置。"""
|
||||
text = str(value or "").strip()
|
||||
return text or HUYA_CONFIG_DEFAULTS[field]
|
||||
|
||||
|
||||
def apply_huya_config_defaults(config: HuyaConfig) -> bool:
|
||||
"""补齐虎牙配置默认值,返回是否发生变更。"""
|
||||
changed = False
|
||||
for field in HUYA_CONFIG_FIELDS:
|
||||
normalized = huya_config_value(field, getattr(config, field, None))
|
||||
if getattr(config, field, None) != normalized:
|
||||
setattr(config, field, normalized)
|
||||
changed = True
|
||||
return changed
|
||||
|
||||
|
||||
def cookie_value(cookie: str, key: str) -> str:
|
||||
"""从 Cookie 文本中提取指定 key。"""
|
||||
match = re.search(rf"(?:^|;\s*){re.escape(key)}=([^;]+)", cookie or "")
|
||||
@@ -125,8 +143,11 @@ def ensure_huya_config(db: Session) -> HuyaConfig:
|
||||
"""获取单条虎牙配置,不存在则创建。"""
|
||||
config = db.query(HuyaConfig).first()
|
||||
if config:
|
||||
if apply_huya_config_defaults(config):
|
||||
db.commit()
|
||||
db.refresh(config)
|
||||
return config
|
||||
config = HuyaConfig()
|
||||
config = HuyaConfig(**HUYA_CONFIG_DEFAULTS)
|
||||
db.add(config)
|
||||
db.commit()
|
||||
db.refresh(config)
|
||||
|
||||
@@ -315,12 +315,12 @@ export default function HuyaTasksPage() {
|
||||
<Row gutter={8}>
|
||||
<Col span={12}>
|
||||
<Form.Item label="直播间 ID" name="room_pid">
|
||||
<Input placeholder="roomPid / pid" />
|
||||
<Input placeholder="默认 1199650619883" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="SID" name="sid">
|
||||
<Input placeholder="活动 sid" />
|
||||
<Input placeholder="默认 2203" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
|
||||
Reference in New Issue
Block a user