优化虎牙默认配置
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user