优化虎牙默认配置
This commit is contained in:
@@ -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