Skip to content

Commit 398e1ea

Browse files
authored
v2.5.36: 修复保存gif图的bug,dir_rule配置支持python f-string语法 (#415) (#420)
1 parent ebf0ea6 commit 398e1ea

File tree

7 files changed

+63
-9
lines changed

7 files changed

+63
-9
lines changed

assets/docs/sources/option_file_syntax.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,14 @@ dir_rule:
9696
# 写法:
9797
# 1. 以'Bd'开头,表示根目录
9898
# 2. 文件夹每增加一层,使用 '_' 或者 '/' 区隔
99-
# 3. 用Pxxx或者Ayyy指代文件夹名,意思是 JmPhotoDetail.xxx / JmAlbumDetail的.yyy。xxx和yyy可以写什么需要看源码。
99+
# 3. 用Pxxx或者Ayyy指代文件夹名,意思是 JmPhotoDetail.xxx / JmAlbumDetail的.yyy。
100+
# xxx和yyy可以写什么需要看源码,或通过下面代码打印出所有可用的值
101+
#
102+
# ```python
103+
# import jmcomic
104+
# properties: dict = jmcomic.JmOption.default().new_jm_client().get_album_detail(本子id).get_properties_dict()
105+
# print(properties)
106+
# ```
100107
#
101108
# 下面演示如果要使用禁漫网站的默认下载方式,该怎么写:
102109
# 规则: 根目录 / 本子id / 章节序号 / 图片文件
@@ -105,6 +112,9 @@ dir_rule:
105112

106113
# 默认规则是: 根目录 / 章节标题 / 图片文件
107114
rule: Bd_Ptitle
115+
# jmcomic v2.5.36 以后,支持使用python的f-string的语法组合文件夹名,下为示例
116+
# rule: Bd / Aauthor / (JM{Aid}-{Pindex})-{Pname}
117+
# {}大括号里的内容同样是写 Axxx 或 Pxxx,其他语法自行参考python f-string的语法
108118
```
109119

110120
## 3. option插件配置项

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.5.35'
5+
__version__ = '2.5.36'
66

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

src/jmcomic/jm_client_interface.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ def transfer_to(self,
6262
img_url=None,
6363
):
6464
img_url = img_url or self.url
65+
index = img_url.find("?")
66+
if index != -1:
67+
img_url = img_url[0:index]
6568

6669
if decode_image is False or scramble_id is None:
6770
# 不解密图片,直接保存文件
6871
JmImageTool.save_resp_img(
6972
self,
7073
path,
71-
need_convert=suffix_not_equal(img_url[:img_url.find("?")], path),
74+
need_convert=suffix_not_equal(img_url, path),
7275
)
7376
else:
7477
# 解密图片并保存文件

src/jmcomic/jm_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def new_postman(cls, session=False, **kwargs):
414414
'cache': None, # see CacheRegistry
415415
'domain': [],
416416
'postman': {
417-
'type': 'cffi',
417+
'type': 'curl_cffi',
418418
'meta_data': {
419419
'impersonate': 'chrome110',
420420
'headers': None,

src/jmcomic/jm_entity.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,32 @@ def get_dirname(cls, detail: 'DetailEntity', ref: str) -> str:
164164

165165
return getattr(detail, ref)
166166

167+
def get_properties_dict(self):
168+
import inspect
169+
170+
prefix = self.__class__.__name__[2]
171+
result = {}
172+
173+
# field
174+
for k, v in self.__dict__.items():
175+
result[prefix + k] = v
176+
177+
# property
178+
for cls in inspect.getmro(type(self)):
179+
for name, attr in cls.__dict__.items():
180+
k = prefix + name
181+
if k not in result and isinstance(attr, property):
182+
v = attr.__get__(self, cls)
183+
result[k] = v
184+
185+
# advice
186+
advice_dict = JmModuleConfig.AFIELD_ADVICE if self.is_album() else JmModuleConfig.PFIELD_ADVICE
187+
for name, func in advice_dict.items():
188+
k = prefix + name
189+
result[k] = func(self)
190+
191+
return result
192+
167193

168194
class JmImageDetail(JmBaseEntity, Downloadable):
169195

src/jmcomic/jm_option.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DirRule:
7171
]
7272

7373
Detail = Union[JmAlbumDetail, JmPhotoDetail, None]
74-
RuleFunc = Callable[[Detail], str]
74+
RuleFunc = Callable
7575
RuleSolver = Tuple[str, RuleFunc, str]
7676
RuleSolverList = List[RuleSolver]
7777

@@ -154,6 +154,12 @@ def split_rule_dsl(self, rule_dsl: str) -> List[str]:
154154

155155
@classmethod
156156
def get_rule_solver(cls, rule: str) -> Optional[RuleSolver]:
157+
if '{' in rule:
158+
def format_path(album, photo):
159+
return fix_windir_name(rule.format(**album.get_properties_dict(), **photo.get_properties_dict())).strip()
160+
161+
return 'F', format_path, rule
162+
157163
# 检查dsl
158164
if not rule.startswith(('A', 'P')):
159165
return None
@@ -176,15 +182,17 @@ def apply_rule_solver(cls, album, photo, rule_solver: RuleSolver) -> str:
176182

177183
def choose_detail(key):
178184
if key == 'Bd':
179-
return None
185+
return None,
180186
if key == 'A':
181-
return album
187+
return album,
182188
if key == 'P':
183-
return photo
189+
return photo,
190+
if key == 'F':
191+
return album, photo
184192

185193
key, func, _ = rule_solver
186194
detail = choose_detail(key)
187-
return func(detail)
195+
return func(*detail)
188196

189197
@classmethod
190198
def apply_rule_directly(cls, album, photo, rule: str) -> str:

tests/test_jmcomic/test_jm_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def run_func_async(func):
7979

8080
def test_partial_exception(self):
8181
class TestDownloader(JmDownloader):
82+
def do_filter(self, detail: DetailEntity):
83+
if detail.is_photo():
84+
return detail[0:2]
85+
if detail.is_album():
86+
return detail[0:2]
87+
return super().do_filter(detail)
88+
8289
@catch_exception
8390
def download_by_image_detail(self, image: JmImageDetail):
8491
raise Exception('test_partial_exception')

0 commit comments

Comments
 (0)