From cce444335088a0bc6a017d58363a24eccac86b0a Mon Sep 17 00:00:00 2001 From: HallowDem <75336799+Cytrogen@users.noreply.github.com> Date: Fri, 10 Apr 2026 12:08:11 -0400 Subject: [PATCH] feat(utils): shared parse_chapter_range + sanitize_filename Used by CLI, web download handler, and background task worker. --- src/kobo_manga/utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/kobo_manga/utils.py diff --git a/src/kobo_manga/utils.py b/src/kobo_manga/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..af663e05e1c64fcf2cff3532345cf5b91419e532 --- /dev/null +++ b/src/kobo_manga/utils.py @@ -0,0 +1,20 @@ +"""共享工具函数""" + + +def sanitize_filename(name: str) -> str: + """清理文件名中的非法字符。""" + return "".join( + c if c.isalnum() or c in " _-()()【】" else "_" for c in name + ) + + +def parse_chapter_range(chapters: str) -> tuple[float, float]: + """解析章节范围字符串。 + + 支持格式: "1-10", "5", "0-2.5" + """ + if "-" in chapters: + parts = chapters.split("-", 1) + return float(parts[0]), float(parts[1]) + num = float(chapters) + return num, num