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