mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 15:07:17 +08:00
perf: 优化配置文件参数容错机制
This commit is contained in:
@@ -8,60 +8,66 @@ __all__ = ["Settings"]
|
||||
|
||||
|
||||
class Settings:
|
||||
# 默认配置参数
|
||||
default = {
|
||||
"mapping_data": {},
|
||||
"work_path": "",
|
||||
"folder_name": "Download",
|
||||
"name_format": "发布时间 作者昵称 作品标题",
|
||||
"user_agent": USERAGENT,
|
||||
"cookie": "",
|
||||
"proxy": None,
|
||||
"timeout": 10,
|
||||
"chunk": 1024 * 1024 * 2,
|
||||
"max_retry": 5,
|
||||
"record_data": False,
|
||||
"image_format": "PNG",
|
||||
"image_download": True,
|
||||
"video_download": True,
|
||||
"live_download": False,
|
||||
"folder_mode": False,
|
||||
"download_record": True,
|
||||
"author_archive": False,
|
||||
"write_mtime": False,
|
||||
"language": "zh_CN",
|
||||
"mapping_data": {}, # 账号备注映射数据
|
||||
"work_path": "", # 工作目录路径
|
||||
"folder_name": "Download", # 下载文件夹名称
|
||||
"name_format": "发布时间 作者昵称 作品标题", # 文件命名格式
|
||||
"user_agent": USERAGENT, # 请求头
|
||||
"cookie": "", # Cookie
|
||||
"proxy": None, # 代理设置
|
||||
"timeout": 10, # 超时时间(秒)
|
||||
"chunk": 1024 * 1024 * 2, # 下载块大小(字节)
|
||||
"max_retry": 5, # 最大重试次数
|
||||
"record_data": False, # 是否记录作品数据
|
||||
"image_format": "PNG", # 图文作品格式
|
||||
"image_download": True, # 是否下载图文
|
||||
"video_download": True, # 是否下载视频
|
||||
"live_download": False, # 是否下载动图
|
||||
"folder_mode": False, # 文件夹归档模式
|
||||
"download_record": True, # 是否记录下载历史
|
||||
"author_archive": False, # 是否按作者归档
|
||||
"write_mtime": False, # 是否写入修改时间
|
||||
"language": "zh_CN", # 语言设置
|
||||
}
|
||||
# 根据操作系统设置编码格式
|
||||
encode = "UTF-8-SIG" if system() == "Windows" else "UTF-8"
|
||||
|
||||
def __init__(self, root: Path = ROOT):
|
||||
# 设置文件路径
|
||||
self.file = root.joinpath("./settings.json")
|
||||
|
||||
def run(self):
|
||||
# 如果文件存在则读取,否则创建新文件
|
||||
return self.read() if self.file.is_file() else self.create()
|
||||
|
||||
def read(self) -> dict:
|
||||
# 读取设置文件
|
||||
with self.file.open("r", encoding=self.encode) as f:
|
||||
return load(f)
|
||||
return self.compatible(load(f))
|
||||
|
||||
def create(self) -> dict:
|
||||
# 创建新的设置文件
|
||||
with self.file.open("w", encoding=self.encode) as f:
|
||||
dump(self.default, f, indent=4, ensure_ascii=False)
|
||||
return self.default
|
||||
|
||||
def update(self, data: dict):
|
||||
# 更新设置文件
|
||||
with self.file.open("w", encoding=self.encode) as f:
|
||||
dump(data, f, indent=4, ensure_ascii=False)
|
||||
|
||||
@classmethod
|
||||
def check_keys(
|
||||
cls,
|
||||
def compatible(
|
||||
self,
|
||||
data: dict,
|
||||
callback: callable,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> dict:
|
||||
needful_keys = set(cls.default.keys())
|
||||
given_keys = set(data.keys())
|
||||
if not needful_keys.issubset(given_keys):
|
||||
callback(*args, **kwargs)
|
||||
return cls.default
|
||||
# 兼容性检查: 确保所有默认配置都存在
|
||||
update = False
|
||||
for i, j in self.default.items():
|
||||
if i not in data:
|
||||
data[i] = j
|
||||
update = True
|
||||
if update:
|
||||
self.update(data)
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user