Skip to content

Commit e0cc0b0

Browse files
authored
v2.2.7: 修复获取评论数的正则表达式,兼容评论为空的情况 (#130),优化测试 (#131)
1 parent 2d23306 commit e0cc0b0

File tree

5 files changed

+173
-161
lines changed

5 files changed

+173
-161
lines changed

src/jmcomic/__init__.py

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

5-
__version__ = '2.2.6'
5+
__version__ = '2.2.7'
66

77
from .api import *
88
from .jm_plugin import *

src/jmcomic/jm_entity.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def __init__(self,
321321

322322
self.likes: str = likes # [1K] 點擊喜歡
323323
self.views: str = views # [40K] 次觀看
324-
self.comment_count = int(comment_count)
324+
self.comment_count: int = self.__parse_comment_count(comment_count) # 评论数
325325
self.work_list: List[str] = work_list # 作品
326326
self.actor_list: List[str] = actor_list # 登場人物
327327
self.tag_list: List[str] = tag_list # 標籤
@@ -368,6 +368,21 @@ def not_exist(episode):
368368

369369
return ret
370370

371+
# noinspection PyMethodMayBeStatic
372+
def __parse_comment_count(self, comment_count: str) -> int:
373+
if comment_count == '':
374+
return 0
375+
376+
try:
377+
from .jm_toolkit import JmcomicText
378+
match = JmcomicText.pattern_total_video_comments.search(comment_count)
379+
if match is None:
380+
return 0
381+
return int(match[1])
382+
except ValueError:
383+
jm_debug('regular.error', f'评论数匹配失败: {comment_count}')
384+
return 0
385+
371386
def create_photo_detail(self, index) -> Tuple[JmPhotoDetail, Tuple]:
372387
# 校验参数
373388
length = len(self.episode_list)
@@ -407,7 +422,7 @@ def __iter__(self) -> Generator[JmPhotoDetail, Any, None]:
407422
return super().__iter__()
408423

409424

410-
class JmSearchPage(JmBaseEntity, IterableEntity):
425+
class JmSearchPage(JmBaseEntity):
411426

412427
def __init__(self, album_info_list: List[Tuple[str, str, StrNone, StrNone, List[str]]]):
413428
# (album_id, title, category_none, label_sub_none, tag_list)
@@ -416,12 +431,9 @@ def __init__(self, album_info_list: List[Tuple[str, str, StrNone, StrNone, List[
416431
def __len__(self):
417432
return len(self.album_info_list)
418433

419-
def __getitem__(self, item):
434+
def __getitem__(self, item) -> Tuple[str, str]:
420435
return self.album_info_list[item][0:2]
421436

422-
def __iter__(self) -> Generator[List[str], Any, None]:
423-
return super().__iter__()
424-
425437
@property
426438
def single_album(self) -> JmAlbumDetail:
427439
return getattr(self, 'album')

src/jmcomic/jm_toolkit.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ class JmcomicText:
5252
pattern_html_album_likes = compile('<span id="albim_likes_\d+">(.*?)</span>')
5353
# 觀看
5454
pattern_html_album_views = compile('<span>(.*?)</span> 次觀看')
55-
# 評論
56-
pattern_html_album_comment_count = compile('<div class="badge" id="total_video_comments">(\d+)</div></a></li>')
55+
# 評論(div)
56+
pattern_html_album_comment_count = compile(
57+
'forum-open btn btn-primary" .*>\w{2}\n'
58+
'(<div class="badge" id="total_video_comments">(\d+)</div>|)?'
59+
)
60+
# 评论(number)
61+
pattern_total_video_comments = compile('<div class="badge" id="total_video_comments">(\d+)</div>')
5762

5863
@classmethod
5964
def parse_to_jm_domain(cls, text: str):

tests/test_jmcomic/test_jm_api.py

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -43,59 +43,6 @@ def test_batch(self):
4343
ret2 = jmcomic.download_album((e for e in album_ls), self.option)
4444
self.assertEqual(len(ret2), len(album_ls), 'Generator')
4545

46-
def test_photo_sort(self):
47-
client = self.option.build_jm_client()
48-
49-
# 测试用例 - 单章本子
50-
single_photo_album_is = str_to_list('''
51-
430371
52-
438696
53-
432888
54-
''')
55-
56-
# 测试用例 - 多章本子
57-
multi_photo_album_is = str_to_list('''
58-
400222
59-
122061
60-
''')
61-
62-
photo_dict: Dict[str, JmPhotoDetail] = multi_call(client.get_photo_detail, single_photo_album_is)
63-
album_dict: Dict[str, JmAlbumDetail] = multi_call(client.get_album_detail, single_photo_album_is)
64-
65-
for each in photo_dict.values():
66-
each: JmPhotoDetail
67-
self.assertEqual(each.album_index, 1)
68-
69-
for each in album_dict.values():
70-
each: JmAlbumDetail
71-
self.assertEqual(each[0].album_index, 1)
72-
73-
print_eye_catching('【通过】测试用例 - 单章本子')
74-
multi_photo_album_dict: Dict[JmAlbumDetail, List[JmPhotoDetail]] = {}
75-
76-
def run(aid):
77-
album = client.get_album_detail(aid)
78-
79-
photo_dict = multi_call(
80-
client.get_photo_detail,
81-
(photo.photo_id for photo in album),
82-
launcher=thread_pool_executor,
83-
)
84-
85-
multi_photo_album_dict[album] = list(photo_dict.values())
86-
87-
multi_thread_launcher(
88-
iter_objs=multi_photo_album_is,
89-
apply_each_obj_func=run,
90-
)
91-
92-
for album, photo_ls in multi_photo_album_dict.items():
93-
self.assertListEqual(
94-
sorted([each.sort for each in album]),
95-
sorted([ans.sort for ans in photo_ls]),
96-
album.album_id
97-
)
98-
9946
def test_get_jmcomic_url(self):
10047
func_list = {
10148
self.client.get_jmcomic_url,
@@ -125,80 +72,3 @@ def run_func_async(func):
12572
print(e)
12673

12774
raise AssertionError(exception_list)
128-
129-
def test_getitem_and_slice(self):
130-
cl: JmcomicClient = self.client
131-
cases = [
132-
['400222', 0, [400222]],
133-
['400222', 1, [413446]],
134-
['400222', (None, 1), [400222]],
135-
['400222', (1, 3), [413446, 413447]],
136-
['413447', (1, 3), [2, 3], []],
137-
]
138-
139-
for [jmid, slicearg, *args] in cases:
140-
ans = args[0]
141-
142-
if len(args) == 1:
143-
func = cl.get_album_detail
144-
else:
145-
func = cl.get_photo_detail
146-
147-
jmentity = func(jmid)
148-
149-
ls: List[Union[JmPhotoDetail, JmImageDetail]]
150-
if isinstance(slicearg, int):
151-
ls = [jmentity[slicearg]]
152-
elif len(slicearg) == 2:
153-
ls = jmentity[slicearg[0]: slicearg[1]]
154-
else:
155-
ls = jmentity[slicearg[0]: slicearg[1]: slicearg[2]]
156-
157-
if len(args) == 1:
158-
self.assertListEqual(
159-
list1=[int(e.id) for e in ls],
160-
list2=ans,
161-
)
162-
else:
163-
self.assertListEqual(
164-
list1=[int(e.img_file_name) for e in ls],
165-
list2=ans,
166-
)
167-
168-
def test_search_advanced(self):
169-
elist = []
170-
171-
def search_and_test(expected_result, params):
172-
try:
173-
page = self.client.search_site(**params)
174-
print(page)
175-
assert int(page[0][0]) == expected_result
176-
except Exception as e:
177-
elist.append(e)
178-
179-
# 定义测试用例
180-
cases = {
181-
152637: {
182-
'search_query': '无修正',
183-
'order_by': JmSearchAlbumClient.ORDER_BY_LIKE,
184-
'time': JmSearchAlbumClient.TIME_ALL,
185-
},
186-
147643: {
187-
'search_query': '无修正',
188-
'order_by': JmSearchAlbumClient.ORDER_BY_PICTURE,
189-
'time': JmSearchAlbumClient.TIME_ALL,
190-
},
191-
}
192-
193-
multi_thread_launcher(
194-
iter_objs=cases.items(),
195-
apply_each_obj_func=search_and_test,
196-
)
197-
198-
if len(elist) == 0:
199-
return
200-
201-
for e in elist:
202-
print(e)
203-
204-
raise AssertionError(elist)

0 commit comments

Comments
 (0)