新增配置文件

This commit is contained in:
JoeamAmier 2023-08-30 22:43:49 +08:00
parent dd3a8d373d
commit c9aa2d3d1a
2 changed files with 17 additions and 3 deletions

View File

@ -1,8 +1,9 @@
from source import Settings
from source import XHS
def example():
"""使用示例"""
"""代码示例"""
# 测试链接
error_demo = "https://www.xiaohongshu.com/explore/"
image_demo = "https://www.xiaohongshu.com/explore/64d1b406000000000103ee8d"
@ -29,7 +30,7 @@ def example():
def main():
xhs = XHS()
xhs = XHS(**Settings().run()) # 配置文件生效
while True:
if url := input("请输入小红书作品链接:"):
xhs.extract(url, download=True)

View File

@ -1,3 +1,5 @@
from json import dump
from json import load
from pathlib import Path
@ -6,8 +8,19 @@ class Settings:
default = {
"path": "./",
"folder": "Download",
"user-agent": None,
"proxies": None,
"timeout": 10,
"chunk": 256 * 1024,
}
def run(self):
return self.read() if self.path.is_file() else self.create()
def read(self):
with self.path.open("r", encoding="utf-8") as f:
return load(f)
def create(self):
with self.path.open("w", encoding="utf-8") as f:
dump(self.default, f, indent=2)
return self.default