Skip to content

Commit ec08224

Browse files
authored
v2.1.8: 默认启用client-cache、所有Downloader共用一个client,优化代码 (#86)
1 parent 929dd46 commit ec08224

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

src/jmcomic/__init__.py

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

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

7-
# noinspection PyUnresolvedReferences
8-
from common import *
97
from .api import *

src/jmcomic/jm_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def get_jmcomic_url(cls, postman=None):
142142
def get_jmcomic_domain_all(cls, postman=None):
143143
"""
144144
访问禁漫发布页,得到所有禁漫的域名
145-
@return:['18comic.vip', ..., 'jm365.xyz/ZNPJam'], 最后一个是【APP軟件下載】
145+
146+
@return: ['18comic.vip', ..., 'jm365.xyz/ZNPJam'], 最后一个是【APP軟件下載】
146147
"""
147148
postman = postman or cls.new_postman(session=True)
148149

src/jmcomic/jm_downloader.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,40 @@ class JmDownloader(DownloadCallback):
5050
JmDownloader = JmOption + 调度逻辑
5151
"""
5252

53-
def __init__(self, option) -> None:
53+
def __init__(self, option: JmOption) -> None:
5454
self.option = option
5555
self.use_cache = self.option.download_cache
5656
self.decode_image = self.option.download_image_decode
5757

5858
def download_album(self, album_id):
5959
client = self.client_for_album(album_id)
6060
album = client.get_album_detail(album_id)
61-
62-
self.before_album(album)
6361
self.download_by_album_detail(album, client)
64-
self.after_album(album)
6562

6663
def download_by_album_detail(self, album: JmAlbumDetail, client: JmcomicClient):
64+
self.before_album(album)
6765
self.execute_by_condition(
6866
iter_objs=album,
6967
apply=lambda photo: self.download_by_photo_detail(photo, client),
7068
count_batch=self.option.decide_photo_batch_count(album)
7169
)
70+
self.after_album(album)
7271

7372
def download_photo(self, photo_id):
7473
client = self.client_for_photo(photo_id)
7574
photo = client.get_photo_detail(photo_id)
76-
77-
self.before_photo(photo)
7875
self.download_by_photo_detail(photo, client)
79-
self.after_photo(photo)
8076

8177
def download_by_photo_detail(self, photo: JmPhotoDetail, client: JmcomicClient):
8278
client.check_photo(photo)
8379

80+
self.before_photo(photo)
8481
self.execute_by_condition(
8582
iter_objs=photo,
8683
apply=lambda image: self.download_by_image_detail(image, client),
8784
count_batch=self.option.decide_image_batch_count(photo)
8885
)
86+
self.after_photo(photo)
8987

9088
def download_by_image_detail(self, image: JmImageDetail, client: JmcomicClient):
9189
img_save_path = self.option.decide_image_filepath(image)
@@ -123,20 +121,18 @@ def execute_by_condition(self, iter_objs, apply: Callable, count_batch: int):
123121
)
124122

125123
# noinspection PyUnusedLocal
126-
def client_for_album(self, jm_album_id):
124+
def client_for_album(self, jm_album_id) -> JmcomicClient:
127125
"""
128-
默认情况下,每次调用JmDownloader的download_album或download_photo,
129-
都会使用一个新的 JmcomicClient
126+
默认情况下,所有的JmDownloader共用一个JmcomicClient
130127
"""
131-
return self.option.new_jm_client()
128+
return self.option.build_jm_client()
132129

133130
# noinspection PyUnusedLocal
134-
def client_for_photo(self, jm_photo_id):
131+
def client_for_photo(self, jm_photo_id) -> JmcomicClient:
135132
"""
136-
默认情况下,每次调用JmDownloader的download_album或download_photo,
137-
都会使用一个新的 JmcomicClient
133+
默认情况下,所有的JmDownloader共用一个JmcomicClient
138134
"""
139-
return self.option.new_jm_client()
135+
return self.option.build_jm_client()
140136

141137
def __enter__(self):
142138
return self

src/jmcomic/jm_option.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,17 @@ def to_file(self, filepath=None):
230230

231231
@field_cache("__jm_client_cache__")
232232
def build_jm_client(self, **kwargs) -> JmcomicClient:
233+
"""
234+
该方法会首次调用会创建JmcomicClient对象,
235+
然后保存在self.__jm_client_cache__中,
236+
多次调用`不会`创建新的JmcomicClient对象
237+
"""
233238
return self.new_jm_client(**kwargs)
234239

235240
def new_jm_client(self, **kwargs) -> JmcomicClient:
236241
postman_conf: dict = self.client.postman.src_dict
237242

238-
# support overwrite meta_data
243+
# support kwargs overwrite meta_data
239244
if len(kwargs) != 0:
240245
meta_data = postman_conf.get('meta_data', {})
241246
meta_data.update(kwargs)
@@ -256,6 +261,10 @@ def new_jm_client(self, **kwargs) -> JmcomicClient:
256261
fallback_domain_list=domain_list,
257262
)
258263

264+
# enable cache
265+
if self.client.cache is True:
266+
client.enable_cache()
267+
259268
return client
260269

261270
@classmethod
@@ -270,6 +279,7 @@ def default_dict(cls) -> Dict:
270279
'threading': {'batch_count': 30},
271280
},
272281
'client': {
282+
'cache': True,
273283
'domain': [],
274284
'postman': {
275285
'type': 'cffi',

tests/test_jmcomic/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ def setUpClass(cls):
3434
cls.option = option
3535
cls.client = option.build_jm_client()
3636

37-
# 启用 JmClientClient 缓存
38-
cls.enable_client_cache()
39-
4037
# 跨平台设置
4138
cls.adapt_os()
4239

@@ -69,7 +66,3 @@ def adapt_linux(cls):
6966
@classmethod
7067
def adapt_macos(cls):
7168
pass
72-
73-
@classmethod
74-
def enable_client_cache(cls):
75-
cls.client.enable_cache(debug=True)

0 commit comments

Comments
 (0)