perf: 优化配置文件参数容错机制

This commit is contained in:
Quan 2025-08-01 09:35:12 +08:00
parent e5d628ba93
commit 0a9f2f3059
7 changed files with 52 additions and 64 deletions

View File

@ -19,13 +19,12 @@
<br>
<p>🔥 <b>小红书链接提取/作品采集工具</b>:提取账号发布、收藏、点赞、专辑作品链接;提取搜索结果作品链接、用户链接;采集小红书作品信息;提取小红书作品下载地址;下载小红书无水印作品文件!</p>
<p>🔥 “小红书”、“XiaoHongShu”、“RedNote” 含义相同,本项目统称为 “小红书”</p>
<p>⭐ 本项目完全免费开源,无任何收费功能,请勿上当受骗!</p>
<h1>📑 项目功能</h1>
<ul><b>程序功能</b>
<li>✅ 采集小红书作品信息</li>
<li>✅ 提取小红书作品下载地址</li>
<li>✅ 下载小红书无水印作品文件</li>
<li>✅ 下载小红书 livePhoto 文件(非无水印)</li>
<li>✅ 下载小红书 livePhoto 文件(水印)</li>
<li>✅ 自动跳过已下载的作品文件</li>
<li>✅ 作品文件完整性处理机制</li>
<li>✅ 自定义图文作品文件下载格式</li>
@ -265,7 +264,7 @@ async def example():
record_data = False # 是否保存作品数据至文件
image_format = "WEBP" # 图文作品文件下载格式支持AUTO、PNG、WEBP、JPEG、HEIC
folder_mode = False # 是否将每个作品的文件储存至单独的文件夹
image_download = True # 图文作品文件下载开关
image_download = True # 图文、图集作品文件下载开关
video_download = True # 视频作品文件下载开关
live_download = False # 图文动图文件下载开关
download_record = True # 是否记录下载成功的作品 ID
@ -406,7 +405,7 @@ async def example():
<tr>
<td align="center">image_download</td>
<td align="center">bool</td>
<td align="center">图文作品文件下载开关</td>
<td align="center">图文、图集作品文件下载开关</td>
<td align="center">true</td>
</tr>
<tr>

View File

@ -19,14 +19,13 @@
<br>
<p>🔥 <b>RedNote Link Extraction/Content Collection Tool</b>Extract account-published, favorites, and liked works links; extract search result works links and user links; collect RedNote works information; extract RedNote works download addresses; download RedNote watermark-free works files!</p>
<p>🔥 "RedNote", "XiaoHongShu" and "小红书" have the same meaning, and this project is collectively referred to as "RedNote".</p>
<p>⭐ This project is completely free and open-source, with no paid features. Please do not be deceived!</p>
<p>⭐ Due to the author's limited energy, I was unable to update the English document in a timely manner, and the content may have become outdated, partial translation is machine translation, the translation result may be incorrect, Suggest referring to Chinese documentation. If you want to contribute to translation, we warmly welcome you.</p>
<h1>📑 Project Features</h1>
<ul><b>Program Features</b>
<li>✅ Collect RedNote works information</li>
<li>✅ Extract RedNote works download addresses</li>
<li>✅ Download RedNote watermark-free works files</li>
<li>✅ Download RedNote livePhoto files (non-watermark-free)</li>
<li>✅ Download RedNote livePhoto files (watermark)</li>
<li>✅ Automatically skip already downloaded works files</li>
<li>✅ works file integrity handling mechanism</li>
<li>✅ Customizable image works file download format</li>
@ -271,7 +270,7 @@ async def example():
record_data = False # 是否保存作品数据至文件
image_format = "WEBP" # 图文作品文件下载格式支持AUTO、PNG、WEBP、JPEG、HEIC
folder_mode = False # 是否将每个作品的文件储存至单独的文件夹
image_download = True # 图文作品文件下载开关
image_download = True # 图文、图集作品文件下载开关
video_download = True # 视频作品文件下载开关
live_download = False # 图文动图文件下载开关
download_record = True # 是否记录下载成功的作品 ID
@ -412,7 +411,7 @@ async def example():
<tr>
<td align="center">image_download</td>
<td align="center">bool</td>
<td align="center">Switch for downloading image works files</td>
<td align="center">Switch for downloading image and atlas works files</td>
<td align="center">true</td>
</tr>
<tr>

View File

@ -24,7 +24,7 @@ async def example():
record_data = False # 是否保存作品数据至文件
image_format = "WEBP" # 图文作品文件下载格式支持AUTO、PNG、WEBP、JPEG、HEIC
folder_mode = False # 是否将每个作品的文件储存至单独的文件夹
image_download = True # 图文作品文件下载开关
image_download = True # 图文、图集作品文件下载开关
video_download = True # 视频作品文件下载开关
live_download = False # 图文动图文件下载开关
download_record = True # 是否记录下载成功的作品 ID

View File

@ -1,14 +1,10 @@
from textual.app import App
from textual.widgets import RichLog
from ..application import XHS
from ..module import (
ERROR,
ROOT,
Settings,
logging,
)
from ..translation import _
from .about import About
from .index import Index
from .loading import Loading
@ -66,16 +62,6 @@ class XHSDownloader(App):
name="record",
)
await self.push_screen("index")
self.SETTINGS.check_keys(
self.parameter,
logging,
self.screen.query_one(RichLog),
_(
"配置文件 settings.json 缺少必要的参数,请删除该文件,然后重新运行程序,自动生成默认配置文件!"
)
+ f"\n{'>' * 50}",
ERROR,
)
async def action_settings(self):
async def save_settings(data: dict) -> None:

View File

@ -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

View File

@ -15,10 +15,7 @@ RELEASES = "https://github.com/JoeanAmier/XHS-Downloader/releases/latest"
USERSCRIPT = "https://raw.githubusercontent.com/JoeanAmier/XHS-Downloader/master/static/XHS-Downloader.js"
USERAGENT = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 "
"Safari/537.36"
)
USERAGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"
HEADERS = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"

View File

@ -4,11 +4,12 @@
2. 修改服务器模式请求路径为 `/xhs/detail`
3. 修改服务器模式默认端口为 `5556`
4. 服务器模式新增 MCP 模式
5. 优化提取链接的正则表达式
6. 修改服务器模式启动命令
7. 支持更多作品链接格式
8. 支持音乐图集作品下载
9. 其他细节优化
5. 优化配置文件参数容错机制
6. 优化提取链接的正则表达式
7. 修改服务器模式启动命令
8. 支持更多作品链接格式
9. 支持音乐图集作品下载
10. 其他细节优化
*****