style: 代码格式化和字符串处理优化

- 优化代码缩进和换行,提高可读性
- 统一使用单引号或双引号,保持一致性
- 移除冗余的空格和括号,精简代码
This commit is contained in:
2025-02-15 21:30:24 +08:00
parent 94198f5a51
commit 1570ba320c
34 changed files with 948 additions and 358 deletions

View File

@@ -99,7 +99,12 @@ class CLI:
@staticmethod
@check_value
def read_cookie(ctx: Context, param, value) -> str:
return BrowserCookie.get(value, domains=["xiaohongshu.com", ])
return BrowserCookie.get(
value,
domains=[
"xiaohongshu.com",
],
)
@staticmethod
@check_value
@@ -110,12 +115,24 @@ class CLI:
table.add_column("parameter", no_wrap=True, style="bold")
table.add_column("abbreviation", no_wrap=True, style="bold")
table.add_column("type", no_wrap=True, style="bold")
table.add_column("description", no_wrap=True, )
table.add_column(
"description",
no_wrap=True,
)
options = (
("--url", "-u", "str", _("小红书作品链接")),
("--index", "-i", "str",
fill(_("下载指定序号的图片文件,仅对图文作品生效;多个序号输入示例:\"1 3 5 7\""), width=55),),
(
"--index",
"-i",
"str",
fill(
_(
'下载指定序号的图片文件,仅对图文作品生效;多个序号输入示例:"1 3 5 7"'
),
width=55,
),
),
("--work_path", "-wp", "str", _("作品数据 / 文件保存根路径")),
("--folder_name", "-fn", "str", _("作品文件储存文件夹名称")),
("--name_format", "-nf", "str", _("作品文件名称格式")),
@@ -125,22 +142,51 @@ class CLI:
("--cookie", "-ck", "str", _("小红书网页版 Cookie无需登录")),
("--proxy", "-p", "str", _("网络代理")),
("--timeout", "-t", "int", _("请求数据超时限制,单位:秒")),
("--chunk", "-c", "int", fill(_("下载文件时,每次从服务器获取的数据块大小,单位:字节"), width=55),),
(
"--chunk",
"-c",
"int",
fill(
_("下载文件时,每次从服务器获取的数据块大小,单位:字节"), width=55
),
),
("--max_retry", "-mr", "int", _("请求数据失败时,重试的最大次数")),
("--record_data", "-rd", "bool", _("是否记录作品数据至文件")),
("--image_format", "-if", "choice", _("图文作品文件下载格式支持PNG、WEBP")),
(
"--image_format",
"-if",
"choice",
_("图文作品文件下载格式支持PNG、WEBP"),
),
("--live_download", "-ld", "bool", _("动态图片下载开关")),
("--download_record", "-dr", "bool", _("作品下载记录开关")),
("--folder_mode", "-fm", "bool", _("是否将每个作品的文件储存至单独的文件夹")),
(
"--folder_mode",
"-fm",
"bool",
_("是否将每个作品的文件储存至单独的文件夹"),
),
("--language", "-l", "choice", _("设置程序语言目前支持zh_CN、en_US")),
("--settings", "-s", "str", _("读取指定配置文件")),
("--browser_cookie", "-bc", "choice",
fill(_("从指定的浏览器读取小红书网页版 Cookie支持{0}; 输入浏览器名称或序号").format(
", ".join(f"{i}: {j}" for i, j in enumerate(
BrowserCookie.SUPPORT_BROWSER.keys(),
start=1,
))
), width=55)),
(
"--browser_cookie",
"-bc",
"choice",
fill(
_(
"从指定的浏览器读取小红书网页版 Cookie支持{0}; 输入浏览器名称或序号"
).format(
", ".join(
f"{i}: {j}"
for i, j in enumerate(
BrowserCookie.SUPPORT_BROWSER.keys(),
start=1,
)
)
),
width=55,
),
),
("--update_settings", "-us", "flag", _("是否更新配置文件")),
("--help", "-h", "flag", _("查看详细参数说明")),
("--version", "-v", "flag", _("查看 XHS-Downloader 版本")),
@@ -154,45 +200,123 @@ class CLI:
table,
border_style="bold",
title="XHS-Downloader CLI Parameters",
title_align="left"))
title_align="left",
)
)
@command(name="XHS-Downloader", help=PROJECT)
@option("--url", "-u", )
@option("--index", "-i", )
@option("--work_path",
"-wp",
type=Path(file_okay=False),
)
@option("--folder_name", "-fn", )
@option("--name_format", "-nf", )
@option("--user_agent", "-ua", )
@option("--cookie", "-ck", )
@option("--proxy", "-p", )
@option("--timeout", "-t", type=int, )
@option("--chunk", "-c", type=int, )
@option("--max_retry", "-mr", type=int, )
@option("--record_data", "-rd", type=bool, )
@option("--image_format", "-if", type=Choice(["png", "PNG", "webp", "WEBP"]), )
@option("--live_download", "-ld", type=bool, )
@option("--download_record", "-dr", type=bool, )
@option("--folder_mode", "-fm", type=bool, )
@option("--language", "-l",
type=Choice(["zh_CN", "en_US"]), )
@option("--settings", "-s", type=Path(dir_okay=False), )
@option("--browser_cookie", "-bc", type=Choice(
list(BrowserCookie.SUPPORT_BROWSER.keys()
) + [str(i) for i in range(1, len(BrowserCookie.SUPPORT_BROWSER) + 1)]), callback=CLI.read_cookie, )
@option("--update_settings", "-us", type=bool,
is_flag=True, )
@option("-h",
"--help",
is_flag=True, )
@option("--version", "-v",
is_flag=True,
is_eager=True,
expose_value=False,
callback=CLI.version, )
@option(
"--url",
"-u",
)
@option(
"--index",
"-i",
)
@option(
"--work_path",
"-wp",
type=Path(file_okay=False),
)
@option(
"--folder_name",
"-fn",
)
@option(
"--name_format",
"-nf",
)
@option(
"--user_agent",
"-ua",
)
@option(
"--cookie",
"-ck",
)
@option(
"--proxy",
"-p",
)
@option(
"--timeout",
"-t",
type=int,
)
@option(
"--chunk",
"-c",
type=int,
)
@option(
"--max_retry",
"-mr",
type=int,
)
@option(
"--record_data",
"-rd",
type=bool,
)
@option(
"--image_format",
"-if",
type=Choice(["png", "PNG", "webp", "WEBP"]),
)
@option(
"--live_download",
"-ld",
type=bool,
)
@option(
"--download_record",
"-dr",
type=bool,
)
@option(
"--folder_mode",
"-fm",
type=bool,
)
@option(
"--language",
"-l",
type=Choice(["zh_CN", "en_US"]),
)
@option(
"--settings",
"-s",
type=Path(dir_okay=False),
)
@option(
"--browser_cookie",
"-bc",
type=Choice(
list(BrowserCookie.SUPPORT_BROWSER.keys())
+ [str(i) for i in range(1, len(BrowserCookie.SUPPORT_BROWSER) + 1)]
),
callback=CLI.read_cookie,
)
@option(
"--update_settings",
"-us",
type=bool,
is_flag=True,
)
@option(
"-h",
"--help",
is_flag=True,
)
@option(
"--version",
"-v",
is_flag=True,
is_eager=True,
expose_value=False,
callback=CLI.version,
)
@pass_context
def cli(ctx, help, language, **kwargs):
# Step 1: 切换语言
@@ -217,4 +341,4 @@ if __name__ == "__main__":
from click.testing import CliRunner
runner = CliRunner()
result = runner.invoke(cli, ['-l', 'en_US', '-u', ''])
result = runner.invoke(cli, ["-l", "en_US", "-u", ""])