Skip to content

Commit cd3d60a

Browse files
authored
v1.5.2: 优化JM网页的正则表达式,增加对异常网页的提示处理,修复序号sort问题,优化代码。 (#15)
1 parent 8672dd3 commit cd3d60a

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
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.5.1'
5+
__version__ = '1.5.2'
66

77
from .api import *

src/jmcomic/jm_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ def jm_get(self, url, is_api=True, require_200=True, **kwargs):
142142
f'响应文本过长(len={len(resp.text)}),不打印')
143143
)
144144

145-
if is_api is True and resp.text.strip() == JmModuleConfig.JM_SERVER_ERROR_HTML:
146-
raise AssertionError("【JM异常】Could not connect to mysql! Please check your database settings!")
145+
if is_api is True:
146+
JmModuleConfig.check_html(resp.text.strip(), url)
147147

148148
return resp
149149

@@ -158,7 +158,7 @@ def img_is_not_need_to_decode(cls, data_original: str, _resp):
158158
return data_original.endswith('.gif')
159159

160160
# noinspection PyAttributeOutsideInit
161-
def enable_cache(self):
161+
def enable_cache(self, debug=False):
162162
def wrap_func_cache(func_name, cache_dict_name):
163163
if hasattr(self, cache_dict_name):
164164
return
@@ -168,10 +168,14 @@ def wrap_func_cache(func_name, cache_dict_name):
168168

169169
# 重载本对象的方法
170170
func = getattr(self, func_name)
171+
172+
cache_hit_msg = f'【缓存命中】{cache_dict_name} ' + '→ [{}]' if debug is True else None
173+
cache_miss_msg = f'【缓存缺失】{cache_dict_name} ' + '← [{}]' if debug is True else None
174+
171175
wrap_func = enable_cache(
172176
cache_dict=cache_dict,
173-
cache_hit_msg=f'命中 {cache_dict_name} ' + '→ [{}]]',
174-
cache_miss_msg=f'缺失 {cache_dict_name} ' + '← [{}]',
177+
cache_hit_msg=cache_hit_msg,
178+
cache_miss_msg=cache_miss_msg,
175179
)(func)
176180

177181
setattr(self, func_name, wrap_func)

src/jmcomic/jm_config.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ class JmModuleConfig:
55
JM_REDIRECT_URL = f'{PROT}jm365.xyz/3YeBdF' # 永久網域,怕走失的小伙伴收藏起来
66
JM_PUB_URL = f'{PROT}jmcomic1.bet'
77
JM_CDN_IMAGE_URL_TEMPLATE = PROT + 'cdn-msp.{domain}/media/photos/{photo_id}/{index:05}{suffix}' # index 从1开始
8-
JM_SERVER_ERROR_HTML = "Could not connect to mysql! Please check your database settings!"
98
JM_IMAGE_SUFFIX = ['.jpg', '.webp', '.png', '.gif']
109

10+
# 访问JM可能会遇到的异常网页
11+
JM_ERROR_RESPONSE_HTML = {
12+
"Could not connect to mysql! Please check your database settings!": "禁漫服务器内部报错",
13+
"Restricted Access!": "禁漫拒绝你所在ip地区的访问,你可以选择: 换域名/换代理",
14+
}
15+
1116
# 图片分隔相关
1217
SCRAMBLE_0 = 220980
1318
SCRAMBLE_10 = 268850
@@ -73,16 +78,23 @@ def get_jmcomic_url(cls, postman=None):
7378
"""
7479
if postman is None:
7580
from common import Postmans
76-
postman = Postmans.get_impl_clazz('cffi') \
81+
postman = Postmans \
82+
.get_impl_clazz('cffi') \
7783
.create(headers=cls.headers(cls.JM_REDIRECT_URL))
7884

79-
domain = postman.with_wrap_resp() \
80-
.get(cls.JM_REDIRECT_URL, allow_redirects=False) \
81-
.redirect_url
82-
83-
cls.jm_debug('获取禁漫地址', f'获取成功,最新可用的禁漫地址: {domain}')
85+
domain = postman.with_redirect_catching().get(cls.JM_REDIRECT_URL)
86+
cls.jm_debug('获取禁漫地址', f'[{cls.JM_REDIRECT_URL}] → [{domain}]')
8487
return domain
8588

89+
@classmethod
90+
def check_html(cls, html: str, url=None):
91+
html = html.strip()
92+
error_msg = cls.JM_ERROR_RESPONSE_HTML.get(html, None)
93+
if error_msg is None:
94+
return
95+
96+
raise AssertionError(f'{error_msg}' + f': {url}' if url is not None else '')
97+
8698

8799
jm_debug = JmModuleConfig.jm_debug
88100
disable_jm_debug = JmModuleConfig.disable_jm_debug

src/jmcomic/jm_entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def create_photo_detail(self, index) -> Tuple[JmPhotoDetail, Tuple]:
239239
title=photo_title,
240240
keywords='',
241241
series_id=self.album_id,
242-
sort=index + 1,
242+
sort=episode_info[1] if len(self) != 1 else 1,
243243
author=self.author,
244244
from_album=self,
245245
page_arr=None,

src/jmcomic/jm_toolkit.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class JmcomicText:
2929

3030
# album 作者
3131
pattern_html_album_author_list = [
32-
compile('作者: <span itemprop="author" data-type="author">(\s*<a.*?>(.*?)</a>)*\s*</span>'),
33-
compile("<a.*?>(.*?)</a>"),
32+
compile('作者: *<span itemprop="author" data-type="author">([\s\S]*?)</span>'),
33+
compile("<a[\s\S]*?>(.*?)</a>"),
3434
]
3535

3636
@classmethod
@@ -127,7 +127,8 @@ def match_field(field_key: str, pattern: Union[Pattern, List[Pattern]], text):
127127
field_value = match_field(field_name, pattern_value, html)
128128

129129
if field_value is None:
130-
raise AssertionError(f"文本没有匹配上字段:字段名为{field_name},pattern为cls.{pattern_name}")
130+
write_text('./resp.txt', html) # debug
131+
raise AssertionError(f"文本没有匹配上字段:字段名为'{field_name}',pattern: [{pattern_value.pattern}]")
131132

132133
# 保存字段
133134
field_dict[field_name] = field_value

0 commit comments

Comments
 (0)