-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v2.4.6: 实现对本子名称进行分词并提取原始名称;实现更便捷的自定义下载文件夹名机制、并跟进对应文档;内置一些更简洁的、可用作文件夹名… #172
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# 自定义下载文件夹名 | ||
|
||
|
||
|
||
## 1. DirRule简介 | ||
|
||
当你使用download_album下载本子时,本子会以一定的路径规则(DirRule)下载到你的磁盘上。 | ||
|
||
你可以使用配置文件定制DirRule,例如下面的例子 | ||
|
||
```yml | ||
dir_rule: | ||
base_dir: D:/a/b/c/ | ||
# 规则含义: 根目录 / 章节标题 / 图片文件 | ||
rule: Bd_Ptitle # P表示章节,title表示使用章节的title字段 | ||
``` | ||
|
||
如果一个章节的名称(title)是ddd,则最后的下载文件夹结构为: | ||
|
||
``` | ||
D:/a/b/c/ddd/00001.webp | ||
D:/a/b/c/ddd/00002.webp | ||
D:/a/b/c/ddd/00003.webp | ||
... | ||
``` | ||
|
||
|
||
|
||
## 2. 自定义字段名 | ||
|
||
上述例子使用了title字段,如果你想自定义一个字段,然后在DirRule中使用自定义字段,该怎么做? | ||
|
||
基于v2.4.6,你可以使用如下方式 | ||
|
||
|
||
|
||
1. 给你的自定义字段取个名 | ||
|
||
```yml | ||
dir_rule: # 忽略base_dir配置项 | ||
rule: Bd_Amyname # A表示本子,myname表示本子的一个自定义字段 | ||
``` | ||
|
||
|
||
|
||
2. 在代码中,加入你自定义字段的处理函数 | ||
|
||
```python | ||
from jmcomic import JmModuleConfig | ||
# 你需要写一个函数,把字段名作为key,函数作为value,加到JmModuleConfig.AFIELD_ADVICE这个字典中 | ||
JmModuleConfig.AFIELD_ADVICE['myname'] = lambda album: f'[{album.id}] {album.title}' | ||
``` | ||
|
||
|
||
|
||
这样一来,Amyname这个规则就会交由你的函数进行处理,你便可以返回一个自定义的文件夹名 | ||
|
||
|
||
|
||
|
||
|
||
## 3. 更多的使用例子 | ||
|
||
|
||
|
||
### 完全使用自己的文件夹名 | ||
|
||
```python | ||
from jmcomic import JmModuleConfig | ||
|
||
dic = { | ||
'248965': '社团学姐(爆赞韩漫)' | ||
} | ||
|
||
# Amyname | ||
JmModuleConfig.AFIELD_ADVICE['myname'] = lambda album: dic[album.id] | ||
download_album(248965) | ||
``` | ||
|
||
|
||
|
||
### 文件夹名=作者+标题 | ||
|
||
```python | ||
from jmcomic import JmModuleConfig | ||
# Amyname | ||
JmModuleConfig.AFIELD_ADVICE['myname'] = lambda album: f'【{album.author}】{album.title}' | ||
# album有一个内置字段 authoroname,效果类似 | ||
``` | ||
|
||
|
||
|
||
### 文件夹名=禁漫车号+标题 | ||
|
||
```python | ||
from jmcomic import JmModuleConfig | ||
# Pmyname | ||
JmModuleConfig.PFIELD_ADVICE['myname'] = lambda photo: f'【{photo.id}】{photo.title}' | ||
``` | ||
|
||
|
||
|
||
### 文件夹名=第x话+标题 | ||
|
||
```python | ||
# 直接使用内置字段 indextitle 即可 | ||
dir_rule: | ||
rule: Bd_Pindextitle | ||
``` | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,6 +225,73 @@ def parse_to_abspath(cls, dsl_text: str) -> str: | |
def parse_dsl_text(cls, dsl_text: str) -> str: | ||
return cls.dsl_replacer.parse_dsl_text(dsl_text) | ||
Comment on lines
225
to
226
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
bracket_map = {'(': ')', | ||
'[': ']', | ||
'【': '】', | ||
'(': ')', | ||
} | ||
|
||
@classmethod | ||
def parse_orig_album_name(cls, name: str, default=None): | ||
word_list = cls.tokenize(name) | ||
|
||
for word in word_list: | ||
if word[0] in cls.bracket_map: | ||
continue | ||
|
||
return word | ||
|
||
return default | ||
|
||
@classmethod | ||
def tokenize(cls, title: str) -> List[str]: | ||
""" | ||
繞道#2 [暴碧漢化組] [えーすけ(123)] よりみち#2 (COMIC 快樂天 2024年1月號) [中國翻譯] [DL版] | ||
:return: ['繞道#2', '[暴碧漢化組]', '[えーすけ(123)]', 'よりみち#2', '(COMIC 快樂天 2024年1月號)', '[中國翻譯]', '[DL版]'] | ||
""" | ||
title = title.strip() | ||
ret = [] | ||
bracket_map = cls.bracket_map | ||
|
||
char_list = [] | ||
i = 0 | ||
length = len(title) | ||
|
||
def add(w=None): | ||
if w is None: | ||
w = ''.join(char_list).strip() | ||
|
||
if w == '': | ||
return | ||
|
||
ret.append(w) | ||
char_list.clear() | ||
|
||
while i < length: | ||
c = title[i] | ||
|
||
if c in bracket_map: | ||
# 上一个单词结束 | ||
add() | ||
# 定位右括号 | ||
j = title.find(bracket_map[c], i) | ||
ExceptionTool.require_true(j != -1, f'未闭合的 {c}{bracket_map[c]}: {title[i:]}') | ||
Comment on lines
+277
to
+278
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
# 整个括号的单词结束 | ||
add(title[i:j + 1]) | ||
# 移动指针 | ||
i = j + 1 | ||
else: | ||
char_list.append(c) | ||
i += 1 | ||
|
||
add() | ||
return ret | ||
|
||
@classmethod | ||
def to_zh_cn(cls, s): | ||
import zhconv | ||
return zhconv.convert(s, 'zh_cn') | ||
Comment on lines
+291
to
+293
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
|
||
# 支持dsl: #{???} -> os.getenv(???) | ||
JmcomicText.dsl_replacer.add_dsl_and_replacer(r'\$\{(.*?)\}', JmcomicText.match_os_env) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The addition of
AFIELD_ADVICE
andPFIELD_ADVICE
is consistent with the summary and seems to be implemented correctly. However, it would be beneficial to provide an example or documentation forPFIELD_ADVICE
similar to the one provided forAFIELD_ADVICE
to ensure clarity and ease of use for future developers.