Skip to content

Commit 92f8b26

Browse files
authored
v2.6.10: 文件夹名称支持繁简转换,dir_rule增加normalize_zh配置 (#486) (#485)
1 parent f2adb33 commit 92f8b26

File tree

9 files changed

+77
-7
lines changed

9 files changed

+77
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ jmcomic 123
142142

143143
- 不配置也能使用,十分方便
144144
- 配置可以从配置文件生成,支持多种文件格式
145-
- 配置点有:`请求域名` `客户端实现` `是否使用磁盘缓存` `同时下载的章节/图片数量` `图片格式转换` `下载路径规则` `请求元信息(headers,cookies,proxies`
145+
- 配置点有:`请求域名` `客户端实现` `是否使用磁盘缓存` `同时下载的章节/图片数量` `图片格式转换` `下载路径规则` `请求元信息(headers,cookies,proxies)` `中文繁/简转换`
146146
147147
- **可扩展性强**
148148

assets/docs/sources/option_file_syntax.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ dir_rule:
115115
# rule: Bd / Aauthor / (JM{Aid}-{Pindex})-{Pname}
116116
# {}大括号里的内容同样是写 Axxx 或 Pxxx,其他语法自行参考python f-string的语法
117117
# 另外,rule开头的Bd可忽略不写,因为程序会自动插入Bd
118+
119+
# normalize_zh: 可选。控制是否对目录/文件名中的中文进行繁简体规范化。
120+
# - None(默认):不做任何转换,保持历史行为
121+
# - zh-cn:将中文文本规范为简体
122+
# - zh-tw:将中文文本规范为繁体
123+
# 该功能依赖可选库 `zhconv`(非必需),若未安装或转换失败,程序会回退到原字符串并继续工作,不会影响下载流程。
124+
# 示例:
125+
# dir_rule:
126+
# base_dir: D:/a/b/c/
127+
# rule: Bd / Ptitle
128+
# normalize_zh: zh-cn
118129
```
119130

120131
## 3. option插件配置项

assets/docs/sources/tutorial/4_module_custom.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def custom_album_photo_image_detail_class():
102102
dir_rule:
103103
base_dir: ${workspace}
104104
rule: Bd_Acustom_Pcustom
105+
# 可选:对目录名进行繁/简体规范化(None/zh-cn/zh-tw),默认不启用
106+
# normalize_zh: zh-cn
105107
106108
上面的Acustom,Pcustom都是自定义字段
107109
如果你想要使用这种自定义字段,你就需要替换默认的实体类,方式如下

assets/docs/sources/tutorial/9_custom_download_dir_name.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ D:/a/b/c/ddd/00003.webp
5858
除了Pxxx,你还可以写Axxx,表示这个章节所在的本子的属性xxx,详见本子实体类 JmAlbumDetail。
5959
6060
61+
## 1.1 简繁体统一(normalize_zh)
62+
63+
在一些源站中,同一作品或章节名称可能存在简体/繁体差异,导致在不同环境下生成重复或不一致的文件夹名。v2.6.10 引入了 `dir_rule.normalize_zh` 配置,用于可选地对目录名进行繁/简体规范化。
64+
65+
示例用法:
66+
67+
```yaml
68+
dir_rule:
69+
base_dir: D:/a/b/c/
70+
rule: Bd / Ptitle
71+
normalize_zh: zh-cn # 可选值:None(默认,不转换)/ zh-cn / zh-tw
72+
```
73+
74+
说明:
75+
76+
-`normalize_zh``zh-cn` 时,会把目录名中的中文规范为简体;为 `zh-tw` 时规范为繁体;为 `None` 或不配置时维持历史行为(不转换)。
77+
78+
- 该功能依赖可选库 `zhconv`(非必需),若未安装或转换失败,系统会回退为原始字符串并继续下载,不会导致失败。
79+
80+
6181
## 2. 自定义字段名
6282

6383
上述例子使用了title字段,如果你想自定义一个字段,然后在DirRule中使用自定义字段,该怎么做?

assets/option/option_test_api.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# GitHub Actions 测试用
22
# 移动端配置
33
dir_rule:
4+
normalize_zh: zh-cn
45
rule: Bd_Aauthor_Aid_Pindextitle
56

67
client:

src/jmcomic/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# 被依赖方 <--- 使用方
33
# config <--- entity <--- toolkit <--- client <--- option <--- downloader
44

5-
__version__ = '2.6.9'
5+
__version__ = '2.6.10'
66

77
from .api import *
88
from .jm_plugin import *

src/jmcomic/jm_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def new_postman(cls, session=False, **kwargs):
411411

412412
DEFAULT_OPTION_DICT: dict = {
413413
'log': None,
414-
'dir_rule': {'rule': 'Bd_Pname', 'base_dir': None},
414+
'dir_rule': {'rule': 'Bd_Pname', 'base_dir': None, 'normalize_zh': None},
415415
'download': {
416416
'cache': True,
417417
'image': {'decode': True, 'suffix': None},

src/jmcomic/jm_option.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ def enable_client_cache_on_condition(cls,
6060
class DirRule:
6161
RULE_BASE_DIR = 'Bd'
6262

63-
def __init__(self, rule: str, base_dir=None):
63+
def __init__(self, rule: str, base_dir=None, normalize_zh=None):
64+
"""
65+
:param rule: DSL rule
66+
:param base_dir: base directory
67+
:param normalize_zh: 'zh-cn'|'zh-tw'| or None. 控制是否以及如何进行繁简体归一化,默认 None
68+
"""
6469
base_dir = JmcomicText.parse_to_abspath(base_dir)
6570
self.base_dir = base_dir
6671
self.rule_dsl = rule
72+
self.normalize_zh = normalize_zh
6773
self.parser_list: List[Tuple[str, Callable]] = self.get_rule_parser_list(rule)
6874

6975
def decide_image_save_dir(self,
@@ -88,7 +94,9 @@ def apply_rule_to_path(self, album, photo, only_album_rules=False) -> str:
8894
jm_log('dir_rule', f'路径规则"{rule}"的解析出错: {e}, album={album}, photo={photo}')
8995
raise e
9096
if parser != self.parse_bd_rule:
91-
path = fix_windir_name(str(path)).strip()
97+
# 根据配置 normalize_zh 进行繁简体统一
98+
conv_path = JmcomicText.to_zh(str(path), self.normalize_zh)
99+
path = fix_windir_name(conv_path).strip()
92100

93101
path_ls.append(path)
94102

@@ -201,6 +209,7 @@ def copy_option(self):
201209
dir_rule={
202210
'rule': self.dir_rule.rule_dsl,
203211
'base_dir': self.dir_rule.base_dir,
212+
'normalize_zh': self.dir_rule.normalize_zh,
204213
},
205214
download=self.download.src_dict,
206215
client=self.client.src_dict,
@@ -326,6 +335,7 @@ def deconstruct(self) -> Dict:
326335
'dir_rule': {
327336
'rule': self.dir_rule.rule_dsl,
328337
'base_dir': self.dir_rule.base_dir,
338+
'normalize_zh': self.dir_rule.normalize_zh,
329339
},
330340
'download': self.download.src_dict,
331341
'client': self.client.src_dict,

src/jmcomic/jm_toolkit.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,34 @@ def find_right_pair(left_pair, i):
329329

330330
@classmethod
331331
def to_zh_cn(cls, s):
332-
import zhconv
333-
return zhconv.convert(s, 'zh-cn')
332+
# 兼容旧接口,默认转换为简体
333+
return cls.to_zh(s, 'zh-cn')
334+
335+
@classmethod
336+
def to_zh(cls, s, target=None):
337+
"""
338+
通用的繁简体转换接口。
339+
340+
:param s: 待转换字符串
341+
:param target: 目标编码: 'zh-cn'(简体), 'zh-tw'(繁体),或 None 表示不转换
342+
:return: 转换后的字符串(若转换失败或未安装 zhconv,返回原始字符串)
343+
"""
344+
if s is None:
345+
return s
346+
347+
if not target:
348+
return s
349+
350+
try:
351+
import zhconv
352+
return zhconv.convert(s, target)
353+
except ImportError as e:
354+
jm_log('zhconv.error', '繁简转换失败,未安装zhconv,请先使用命令安装: [pip install zhconv]')
355+
return s
356+
except Exception as e:
357+
# 如果 zhconv 不可用或转换失败,则回退原字符串
358+
jm_log('zhconv.error', f'error: [{e}], s: [{s}]')
359+
return s
334360

335361
@classmethod
336362
def try_mkdir(cls, save_dir: str):

0 commit comments

Comments
 (0)