Skip to content

Commit 6e465ec

Browse files
authored
v1.3.0: 优化实体类、缓存,简化from_album的使用、decide_image_save_dir的api。 (#10)
* 增加get_photo_detail、get_album_detail的缓存。 * 优化实体类,简化from_album的使用、decide_image_save_dir的api。
1 parent d8349d8 commit 6e465ec

File tree

8 files changed

+52
-62
lines changed

8 files changed

+52
-62
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 <--- service <--- option
44

5-
__version__ = '1.2.0'
5+
__version__ = '1.3.0'
66

77
from .api import *

src/jmcomic/api.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def download_photo(index: int,
2323
photo_detail: JmPhotoDetail,
2424
debug_topic='download_album_photo',
2525
):
26-
jm_client.update(photo_detail)
26+
jm_client.ensure_photo_can_use(photo_detail)
2727

2828
jm_debug(debug_topic,
2929
f"下载第[{index + 1}]集: "
@@ -90,12 +90,7 @@ def download_by_photo_detail(photo_detail: JmPhotoDetail,
9090
# 下载准备
9191
use_cache = option.download_use_disk_cache
9292
decode_image = option.download_image_then_decode
93-
94-
if photo_detail.from_album is None:
95-
jm_client.fill_from_album(photo_detail)
96-
97-
if photo_detail.page_arr is None:
98-
jm_client.update(photo_detail)
93+
jm_client.ensure_photo_can_use(photo_detail)
9994

10095
# 下载每个图片的函数
10196
def download_image(index, img_detail, debug_topic='download_images_of_photo'):
@@ -114,6 +109,7 @@ def download_image(index, img_detail, debug_topic='download_images_of_photo'):
114109
img_save_path,
115110
decode_image=decode_image,
116111
)
112+
117113
jm_debug(debug_topic, f'photo-{img_detail.aid}: '
118114
f'图片{img_detail.filename}下载完成:'
119115
f'[{img_detail.img_url}] → [{img_save_path}]')

src/jmcomic/jm_client.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,32 @@ def get_album_detail(self, album_id) -> JmAlbumDetail:
7878
# 用 JmcomicText 解析 html,返回实体类
7979
return JmcomicText.analyse_jm_album_html(resp.text)
8080

81-
def get_photo_detail(self, jm_photo_id: str) -> JmPhotoDetail:
81+
def get_photo_detail(self, photo_id: str, album=True) -> JmPhotoDetail:
8282
# 参数校验
83-
photo_id = JmcomicText.parse_to_photo_id(jm_photo_id)
83+
photo_id = JmcomicText.parse_to_photo_id(photo_id)
8484

8585
# 请求
8686
resp = self.jm_get(f"/photo/{photo_id}")
8787

8888
# 用 JmcomicText 解析 html,返回实体类
89-
return JmcomicText.analyse_jm_photo_html(resp.text)
89+
photo_detail = JmcomicText.analyse_jm_photo_html(resp.text)
9090

91-
def fill_from_album(self, photo_detail: JmPhotoDetail) -> JmAlbumDetail:
92-
"""
93-
获取 photo_detail 所在的 album。
94-
并把 album_detail 赋值给 photo_detail.from_album
95-
"""
96-
album_detail = self.get_album_detail(photo_detail.album_id)
97-
photo_detail.from_album = album_detail
98-
return album_detail
91+
# 一并获取该章节的所处本子
92+
if album is True:
93+
photo_detail.from_album = self.get_album_detail(photo_detail.album_id)
94+
95+
return photo_detail
96+
97+
def ensure_photo_can_use(self, photo_detail: JmPhotoDetail):
98+
# 检查 from_album
99+
if photo_detail.from_album is None:
100+
photo_detail.from_album = self.get_album_detail(photo_detail.album_id)
99101

100-
def update(self, photo_detail: JmPhotoDetail):
101-
new = self.get_photo_detail(photo_detail.photo_id)
102-
photo_detail.__dict__.update(new.__dict__)
102+
# 检查 page_arr 和 data_original_domain
103+
if photo_detail.page_arr is None or photo_detail.data_original_domain is None:
104+
new = self.get_photo_detail(photo_detail.photo_id, False)
105+
photo_detail.page_arr = new.page_arr
106+
photo_detail.data_original_domain = new.data_original_domain
103107

104108
# -- search --
105109

src/jmcomic/jm_entity.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,16 @@ def save_base_dir(self):
2121
return self.detail_save_base_dir
2222

2323
def save_file_name(self) -> str:
24-
return f"【{self.get_id_prefix_of_filename()}{self.get_id()}{self.get_title()}{self.detail_save_file_suffix}"
24+
def jm_type():
25+
# "JmAlbumDetail" -> "album"
26+
cls_name = self.__class__.__name__
27+
return cls_name[cls_name.index("m") + 1: cls_name.rfind("Detail")].lower()
2528

26-
def get_id_prefix_of_filename(self):
27-
# "JmAlbumDetail" -> "album"
28-
cls_name = self.__class__.__name__
29-
id_prefix = cls_name[cls_name.index("m") + 1: cls_name.rfind("Detail")]
30-
return f'{id_prefix}-'
29+
return '[{}]{}{}'.format(jm_type(), self.get_id(), self.detail_save_file_suffix)
3130

3231
def get_id(self) -> str:
3332
raise NotImplementedError
3433

35-
def get_title(self) -> str:
36-
raise NotImplementedError
37-
3834
def __len__(self):
3935
raise NotImplementedError
4036

@@ -152,15 +148,14 @@ def album_index(self) -> int:
152148

153149
@property
154150
def author(self) -> str:
155-
# self._author 不为空字符串
156-
if self._author is not None and self._author != '':
157-
return self._author.strip()
158-
159-
# self._author 为空,先向上找
151+
# 优先使用 from_album
160152
if self.from_album is not None:
161153
return self.from_album.author
162154

163-
# 无向上元素,使用默认
155+
if self._author is not None and self._author != '':
156+
return self._author.strip()
157+
158+
# 使用默认
164159
return JmModuleConfig.default_author
165160

166161
def create_image_detail(self, index) -> JmImageDetail:
@@ -196,9 +191,6 @@ def __getitem__(self, item) -> JmImageDetail:
196191
def get_id(self):
197192
return self.photo_id
198193

199-
def get_title(self):
200-
return self.title
201-
202194
def __len__(self):
203195
return len(self.page_arr)
204196

@@ -213,13 +205,15 @@ def __init__(self,
213205
page_count,
214206
author_list,
215207
pub_date,
208+
update_date,
216209
):
217210
self.album_id: str = album_id
218211
self.scramble_id: str = scramble_id
219212
self.title: str = title
220213
self.page_count = int(page_count)
221214
self._author_list: List[str] = author_list
222-
self.pub_date = pub_date
215+
self.pub_date: str = pub_date # 发布日期
216+
self.update_date: str = update_date # 更新日期
223217

224218
# 有的 album 没有章节,则自成一章。
225219
if len(episode_list) == 0:
@@ -263,9 +257,6 @@ def author(self):
263257
def get_id(self):
264258
return self.album_id
265259

266-
def get_title(self):
267-
return self.title
268-
269260
def __len__(self):
270261
return len(self.episode_list)
271262

src/jmcomic/jm_option.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ class JmOptionAdvice:
55

66
def decide_image_save_dir(self,
77
option: 'JmOption',
8-
album_detail: Optional[JmAlbumDetail],
98
photo_detail: JmPhotoDetail,
109
) -> StrNone:
1110
"""
1211
决定一个本子图片的下载文件夹
1312
@param option: JmOption对象
14-
@param album_detail: 本子集实体类
1513
@param photo_detail: 本子章节实体类
1614
@return: 下载文件夹,为空表示不处理
1715
"""
@@ -245,19 +243,16 @@ def __init__(self,
245243
下面是决定图片保存路径的方法
246244
"""
247245

248-
def decide_image_save_dir(self, album_detail, photo_detail) -> str:
249-
if album_detail is None and photo_detail is None:
250-
raise AssertionError('album和photo不能同时为None')
251-
246+
def decide_image_save_dir(self, photo_detail) -> str:
252247
# 先检查advice的回调,如果回调支持,则优先使用回调
253248
for advice in JmAdviceRegistry.get_advice(self):
254-
save_dir = advice.decide_image_save_dir(self, album_detail, photo_detail)
249+
save_dir = advice.decide_image_save_dir(self, photo_detail)
255250
if save_dir is not None:
256251
return save_dir
257252

258253
# 使用 self.dir_tree 决定 save_dir
259254
save_dir = self.dir_tree.deside_image_save_dir(
260-
album_detail or photo_detail.from_album,
255+
photo_detail.from_album,
261256
photo_detail
262257
)
263258

@@ -287,7 +282,7 @@ def decide_image_filepath(self, photo_detail: JmPhotoDetail, index: int) -> str:
287282
return filepath
288283

289284
# 通过拼接生成绝对路径
290-
save_dir = self.decide_image_save_dir(photo_detail.from_album, photo_detail)
285+
save_dir = self.decide_image_save_dir(photo_detail)
291286
image: JmImageDetail = photo_detail[index]
292287
suffix = self.decide_image_suffix(image)
293288
return save_dir + image.img_file_name + suffix
@@ -451,15 +446,13 @@ def build_save_path_provider(self,
451446

452447
if use_all_default_save_path is True:
453448
# 不通过请求获取 photo 的信息,相当于使用【空本子】和【空集】
454-
photo_detail, album_detail = None, None
449+
photo_detail = None
455450
else:
456451
# 通过请求获得 photo 的本子信息
457-
client = self.build_jm_client()
458-
photo_detail = client.get_photo_detail(photo_id)
459-
album_detail = client.fill_from_album(photo_detail)
452+
photo_detail = self.build_jm_client().get_photo_detail(photo_id)
460453

461454
def save_path_provider(url, _suffix: str, _index, _is_decode):
462-
return '{0}{1}{2}'.format(self.decide_image_save_dir(album_detail, photo_detail),
455+
return '{0}{1}{2}'.format(self.decide_image_save_dir(photo_detail),
463456
of_file_name(url, trim_suffix=True),
464457
self.download_convert_image_suffix or _suffix)
465458

src/jmcomic/jm_toolkit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class JmcomicText:
2424
'<li.*>\n第(\d+)話\n(.*)\n'
2525
'<[\s\S]*?>(\d+-\d+-\d+).*?')
2626
pattern_html_album_page_count = compile('<span class="pagecount">.*?:(\d+)</span>')
27-
pattern_html_album_pub_date = compile('itemprop="datePublished" content=".*?">更新日期 : (.*?)</span>')
27+
pattern_html_album_pub_date = compile('>上架日期 : (.*?)</span>')
28+
pattern_html_album_update_date = compile('>更新日期 : (.*?)</span>')
2829

2930
# album 作者
3031
pattern_html_album_author_list = [

tests/test_jmcomic/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ def setUpClass(cls):
3535
# 设置 JmOption,JmcomicClient
3636
option = cls.use_option('option_test.yml')
3737
cls.option = option
38-
cls.client = option.build_jm_client()
38+
39+
client = option.build_jm_client()
40+
# enable cache
41+
client.get_photo_detail = enable_cache()(client.get_photo_detail)
42+
client.get_album_detail = enable_cache()(client.get_album_detail)
43+
cls.client = client
3944

4045
# 跨平台设置
4146
cls.adapt_os()

tests/test_jmcomic/test_jm_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ def decide_image_filepath(self,
5757
) -> StrNone:
5858
return workspace(f'{time_stamp()}_{photo_detail[index].img_file_name}.test.png')
5959

60-
option = JmOption.default()
60+
option = self.option
6161
option.register_advice(MyAdvice())
6262
jmcomic.download_album('366867', option)
6363

6464
def test_photo_sort(self):
65-
client = JmOption.default().build_jm_client()
65+
client = self.option.build_jm_client()
6666

6767
# 测试用例 - 单章本子
6868
single_photo_album_is = str_to_list('''

0 commit comments

Comments
 (0)