Skip to content

Commit aae008a

Browse files
authored
v2.1.9: 支持自定义保存图片,优化debug的topic (#88)
1 parent ec08224 commit aae008a

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

src/jmcomic/__init__.py

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

5-
__version__ = '2.1.8'
5+
__version__ = '2.1.9'
66

77
from .api import *

src/jmcomic/jm_client_impl.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,14 @@ def request_with_retry(self,
6363
pass
6464

6565
if domain_index != 0 or retry_count != 0:
66-
jm_debug(
67-
f'request_retry',
68-
', '.join([
69-
f'次数: [{retry_count}/{self.retry_times}]',
70-
f'域名: [{domain_index} of {self.domain_list}]',
71-
f'路径: [{url}]',
72-
f'参数: [{kwargs if "login" not in url else "#login_form#"}]'
73-
])
74-
)
66+
jm_debug(f'req.retry',
67+
', '.join([
68+
f'次数: [{retry_count}/{self.retry_times}]',
69+
f'域名: [{domain_index} of {self.domain_list}]',
70+
f'路径: [{url}]',
71+
f'参数: [{kwargs if "login" not in url else "#login_form#"}]'
72+
])
73+
)
7574

7675
try:
7776
return request(url, **kwargs)
@@ -85,7 +84,7 @@ def request_with_retry(self,
8584

8685
# noinspection PyMethodMayBeStatic, PyUnusedLocal
8786
def before_retry(self, e, kwargs, retry_count, url):
88-
jm_debug('retry', str(e))
87+
jm_debug('req.err', str(e))
8988

9089
def enable_cache(self, debug=False):
9190
def wrap_func_cache(func_name, cache_dict_name):
@@ -134,7 +133,7 @@ def get_jmcomic_domain_all(self, postman=None):
134133
# noinspection PyUnusedLocal
135134
def fallback(self, request, url, domain_index, retry_count, **kwargs):
136135
msg = f"请求重试全部失败: [{url}], {self.domain_list}"
137-
jm_debug('fallback', "msg")
136+
jm_debug('fallback', msg)
138137
raise AssertionError(msg)
139138

140139

@@ -286,7 +285,7 @@ def album_comment(self,
286285
data['is_reply'] = 1
287286
data['forum_subject'] = 1
288287

289-
jm_debug('album_comment',
288+
jm_debug('album.comment',
290289
f'{video_id}: [{comment}]' +
291290
(f' to ({comment_id})' if comment_id is not None else '')
292291
)
@@ -297,7 +296,7 @@ def album_comment(self,
297296
)
298297

299298
ret = JmAcResp(resp)
300-
jm_debug('album_comment', f'{video_id}: [{comment}] ← ({ret.model().cid})')
299+
jm_debug('album.comment', f'{video_id}: [{comment}] ← ({ret.model().cid})')
301300

302301
return ret
303302

src/jmcomic/jm_downloader.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@ class JmDownloadException(Exception):
99
class DownloadCallback:
1010

1111
def before_album(self, album: JmAlbumDetail):
12-
jm_debug('album-before',
12+
jm_debug('album.before',
1313
f'本子获取成功: [{album.id}], '
1414
f'作者: [{album.author}], '
1515
f'章节数: [{len(album)}], '
1616
f'标题: [{album.title}], '
1717
)
1818

1919
def after_album(self, album: JmAlbumDetail):
20-
jm_debug('album-after', f'本子下载完成: [{album.id}]')
20+
jm_debug('album.after', f'本子下载完成: [{album.id}]')
2121

2222
def before_photo(self, photo: JmPhotoDetail):
23-
jm_debug('photo-before',
23+
jm_debug('photo.before',
2424
f'开始下载章节: {photo.id} ({photo.album_id}[{photo.index}/{len(photo.from_album)}]), '
2525
f'标题: [{photo.title}], '
2626
f'图片数为[{len(photo)}]'
2727
)
2828

2929
def after_photo(self, photo: JmPhotoDetail):
30-
jm_debug('photo-after',
30+
jm_debug('photo.after',
3131
f'章节下载完成: {photo.id} ({photo.album_id}[{photo.index}/{len(photo.from_album)}])')
3232

3333
def before_image(self, image: JmImageDetail, img_save_path):
3434
if image.is_exists:
35-
jm_debug('image-before',
35+
jm_debug('image.before',
3636
f'图片已存在: {image.tag} ← [{img_save_path}]'
3737
)
3838
else:
39-
jm_debug('image-before',
39+
jm_debug('image.before',
4040
f'图片准备下载: {image.tag}, [{image.img_url}] → [{img_save_path}]'
4141
)
4242

4343
def after_image(self, image: JmImageDetail, img_save_path):
44-
jm_debug('image-after',
44+
jm_debug('image.after',
4545
f'图片下载完成: {image.tag}, [{image.img_url}] → [{img_save_path}]')
4646

4747

@@ -139,6 +139,6 @@ def __enter__(self):
139139

140140
def __exit__(self, exc_type, exc_val, exc_tb):
141141
if exc_type is not None:
142-
jm_debug('exception',
142+
jm_debug('dler.exception',
143143
f'{self.__class__.__name__} Exit with exception: {exc_type, exc_val}'
144144
)

src/jmcomic/jm_toolkit.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .jm_entity import *
2+
from PIL import Image
23

34

45
class JmcomicText:
@@ -214,7 +215,7 @@ def save_resp_img(cls, resp: Any, filepath: str, need_convert=True):
214215
如果不需要改变文件的格式,使用need_convert=False可以跳过PIL解析图片,效率更高。
215216
"""
216217
if need_convert is True:
217-
cls.open_Image(resp.content).save(filepath)
218+
cls.save_image(cls.open_Image(resp.content), filepath)
218219
else:
219220
save_resp_content(resp, filepath)
220221

@@ -283,15 +284,18 @@ def decode_and_save(cls,
283284
)
284285

285286
# 保存到新的解密文件
286-
img_decode.save(decoded_save_path)
287+
cls.save_image(img_decode, decoded_save_path)
287288

288289
@classmethod
289290
def open_Image(cls, fp: Union[str, bytes]):
290-
from PIL import Image
291291
from io import BytesIO
292292
fp = fp if isinstance(fp, str) else BytesIO(fp)
293293
return Image.open(fp)
294294

295+
@classmethod
296+
def save_image(cls, image: Image, filepath: str):
297+
image.save(filepath)
298+
295299
@classmethod
296300
def get_num(cls, scramble_id, aid, filename: str) -> int:
297301
"""

0 commit comments

Comments
 (0)