Skip to content

Commit e2d9232

Browse files
authored
v2.1.1: 优化代码和Github工作流脚本 (#75)
1 parent 58f1b8b commit e2d9232

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
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
44

5-
__version__ = '2.1.0'
5+
__version__ = '2.1.1'
66

77
from .api import *

src/jmcomic/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def download_album(jm_album_id, option=None):
2525
def download_photo(photo: JmPhotoDetail,
2626
debug_topic='photo',
2727
):
28-
jm_client.ensure_photo_can_use(photo)
28+
jm_client.check_photo(photo)
2929

3030
jm_debug(debug_topic,
3131
f'开始下载章节: {photo.id} ({photo.album_id}[{photo.index}/{len(album)}]), '
@@ -88,7 +88,7 @@ def download_by_photo_detail(photo_detail: JmPhotoDetail,
8888
# 下载准备
8989
use_cache = option.download_cache
9090
decode_image = option.download_image_decode
91-
jm_client.ensure_photo_can_use(photo_detail)
91+
jm_client.check_photo(photo_detail)
9292

9393
# 下载每个图片的函数
9494
def download_image(index, image: JmImageDetail, debug_topic='image'):

src/jmcomic/jm_client_impl.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def request_with_retry(self,
4949
@param kwargs: 请求方法的kwargs
5050
"""
5151
if domain_index >= len(self.domain_list):
52-
raise AssertionError(f"请求重试全部失败: [{url}], {self.domain_list}")
52+
self.fallback(request, url, domain_index, retry_count, **kwargs)
5353

5454
if url.startswith('/'):
5555
# path
@@ -81,7 +81,6 @@ def request_with_retry(self,
8181
else:
8282
return self.request_with_retry(request, url, domain_index + 1, 0, **kwargs)
8383

84-
8584
# noinspection PyMethodMayBeStatic, PyUnusedLocal
8685
def before_retry(self, e, kwargs, retry_count, url):
8786
jm_debug('error', str(e))
@@ -130,6 +129,9 @@ def get_jmcomic_url(self, postman=None):
130129
def get_jmcomic_domain_all(self, postman=None):
131130
return JmModuleConfig.get_jmcomic_domain_all(postman or self.get_root_postman())
132131

132+
def fallback(self, request, url, domain_index, retry_count, **kwargs):
133+
raise AssertionError(f"请求重试全部失败: [{url}], {self.domain_list}")
134+
133135

134136
# 基于网页实现的JmClient
135137
class JmHtmlClient(AbstractJmClient):
@@ -160,17 +162,6 @@ def get_photo_detail(self, photo_id, fetch_album=True) -> JmPhotoDetail:
160162

161163
return photo_detail
162164

163-
def ensure_photo_can_use(self, photo_detail: JmPhotoDetail):
164-
# 检查 from_album
165-
if photo_detail.from_album is None:
166-
photo_detail.from_album = self.get_album_detail(photo_detail.album_id)
167-
168-
# 检查 page_arr 和 data_original_domain
169-
if photo_detail.page_arr is None or photo_detail.data_original_domain is None:
170-
new = self.get_photo_detail(photo_detail.photo_id, False)
171-
new.from_album = photo_detail.from_album
172-
photo_detail.__dict__.update(new.__dict__)
173-
174165
def search_album(self, search_query, main_tag=0, page=1) -> JmSearchPage:
175166
params = {
176167
'main_tag': main_tag,
@@ -226,7 +217,7 @@ def get_jm_html(self, url, require_200=True, **kwargs):
226217
resp = self.get(url, **kwargs)
227218

228219
if require_200 is True and resp.status_code != 200:
229-
write_text('./resp.html', resp.text)
220+
# write_text('./resp.html', resp.text)
230221
self.check_special_http_code(resp)
231222
self.raise_request_error(resp)
232223

src/jmcomic/jm_client_interface.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ def get_album_detail(self, album_id) -> JmAlbumDetail:
151151
def get_photo_detail(self, photo_id, fetch_album=True) -> JmPhotoDetail:
152152
raise NotImplementedError
153153

154-
def ensure_photo_can_use(self, photo_detail: JmPhotoDetail):
155-
raise NotImplementedError
156-
157154
def search_album(self, search_query: str, main_tag: int = 0, page: int = 1) -> JmSearchPage:
158155
raise NotImplementedError
159156

@@ -163,6 +160,16 @@ def of_api_url(self, api_path, domain):
163160
def enable_cache(self, debug=False):
164161
raise NotImplementedError
165162

163+
def check_photo(self, photo_detail: JmPhotoDetail):
164+
# 检查 from_album
165+
if photo_detail.from_album is None:
166+
photo_detail.from_album = self.get_album_detail(photo_detail.album_id)
167+
168+
# 检查 page_arr 和 data_original_domain
169+
if photo_detail.page_arr is None or photo_detail.data_original_domain is None:
170+
new = self.get_photo_detail(photo_detail.photo_id, False)
171+
new.from_album = photo_detail.from_album
172+
photo_detail.__dict__.update(new.__dict__)
166173

167174
class JmUserClient:
168175

usage/workflow_download.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
452859
55
66
7+
8+
79
'''
810

911

@@ -48,19 +50,19 @@ def hook_debug(option):
4850
jm_download_dir = get_env('JM_DOWNLOAD_DIR') or workspace()
4951
mkdir_if_not_exists(jm_download_dir)
5052

51-
class HookDebugClient(JmHtmlClient):
53+
class RaiseErrorAwareClient(JmHtmlClient):
5254

5355
@classmethod
54-
def raise_request_error(cls, resp, msg):
56+
def raise_request_error(cls, resp, msg=None):
5557
from common import write_text, fix_windir_name, format_ts
5658
write_text(
57-
f'{jm_download_dir}/[请求失败的响应内容]_[{format_ts()}]_[{fix_windir_name(resp.url)}].html',
59+
f'{jm_download_dir}/{fix_windir_name(resp.url)}',
5860
resp.text
5961
)
6062

6163
return super().raise_request_error(resp, msg)
6264

63-
option.jm_client_impl_mapping['html'] = HookDebugClient
65+
option.jm_client_impl_mapping['html'] = RaiseErrorAwareClient
6466

6567

6668
def get_env(name):

0 commit comments

Comments
 (0)