Skip to content

Commit 9f732da

Browse files
authored
v2.0.7: 优化搜索功能、实体类、缓存、请求头,更新代码示例 (#63)
1 parent e7e85a1 commit 9f732da

File tree

7 files changed

+87
-27
lines changed

7 files changed

+87
-27
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.0.6'
5+
__version__ = '2.0.7'
66

77
from .api import *

src/jmcomic/jm_client_impl.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,32 @@ def before_retry(self, e, kwargs, retry_count, url):
7575

7676
def enable_cache(self, debug=False):
7777
def wrap_func_cache(func_name, cache_dict_name):
78+
import common
79+
if common.VERSION > '0.4.8':
80+
cache = common.cache
81+
cache_dict = {}
82+
cache_hit_msg = (f'【缓存命中】{cache_dict_name} ' + '→ [{}]') if debug is True else None
83+
cache_miss_msg = (f'【缓存缺失】{cache_dict_name} ' + '← [{}]') if debug is True else None
84+
cache = cache(
85+
cache_dict=cache_dict,
86+
cache_hit_msg=cache_hit_msg,
87+
cache_miss_msg=cache_miss_msg,
88+
)
89+
setattr(self, cache_dict_name, cache_dict)
90+
else:
91+
if sys.version_info < (3, 9):
92+
raise NotImplementedError('不支持启用JmcomicClient缓存。\n'
93+
'请更新python版本到3.9以上,'
94+
'或更新commonX: `pip install commonX --upgrade`')
95+
import functools
96+
cache = functools.cache
97+
7898
if hasattr(self, cache_dict_name):
7999
return
80100

81-
cache_dict = {}
82-
setattr(self, cache_dict_name, cache_dict)
83-
84101
# 重载本对象的方法
85102
func = getattr(self, func_name)
86-
87-
cache_hit_msg = f'【缓存命中】{cache_dict_name} ' + '→ [{}]' if debug is True else None
88-
cache_miss_msg = f'【缓存缺失】{cache_dict_name} ' + '← [{}]' if debug is True else None
89-
90-
wrap_func = enable_cache(
91-
cache_dict=cache_dict,
92-
cache_hit_msg=cache_hit_msg,
93-
cache_miss_msg=cache_miss_msg,
94-
)(func)
103+
wrap_func = cache(func)
95104

96105
setattr(self, func_name, wrap_func)
97106

@@ -149,18 +158,20 @@ def ensure_photo_can_use(self, photo_detail: JmPhotoDetail):
149158
new.from_album = photo_detail.from_album
150159
photo_detail.__dict__.update(new.__dict__)
151160

152-
def search_album(self, search_query, main_tag=0):
161+
def search_album(self, search_query, main_tag=0, page=1) -> JmSearchPage:
153162
params = {
154163
'main_tag': main_tag,
155164
'search_query': search_query,
165+
'page': page,
156166
}
157167

158168
resp = self.get_jm_html('/search/photos', params=params, allow_redirects=True)
159169

160170
# 检查是否发生了重定向
161171
# 因为如果搜索的是禁漫车号,会直接跳转到本子详情页面
162172
if resp.redirect_count != 0 and '/album/' in resp.url:
163-
return JmcomicText.analyse_jm_album_html(resp.text)
173+
album = JmcomicText.analyse_jm_album_html(resp.text)
174+
return JmSearchPage.wrap_single_album(album)
164175
else:
165176
return JmSearchSupport.analyse_jm_search_html(resp.text)
166177

@@ -284,7 +295,7 @@ def check_special_http_code(cls, resp):
284295
class JmApiClient(AbstractJmClient):
285296
API_SEARCH = '/search'
286297

287-
def search_album(self, search_query: str, main_tag=0) -> JmApiResp:
298+
def search_album(self, search_query, main_tag=0, page=1) -> JmApiResp:
288299
"""
289300
model_data: {
290301
"search_query": "MANA",
@@ -312,6 +323,8 @@ def search_album(self, search_query: str, main_tag=0) -> JmApiResp:
312323
self.API_SEARCH,
313324
params={
314325
'search_query': search_query,
326+
'main_tag': main_tag,
327+
'page': page,
315328
}
316329
)
317330

src/jmcomic/jm_client_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def get_photo_detail(self, photo_id, fetch_album=True) -> JmPhotoDetail:
133133
def ensure_photo_can_use(self, photo_detail: JmPhotoDetail):
134134
raise NotImplementedError
135135

136-
def search_album(self, search_query, main_tag=0) -> Union[JmSearchPage, JmAlbumDetail]:
136+
def search_album(self, search_query: str, main_tag: int = 0, page: int = 1) -> JmSearchPage:
137137
raise NotImplementedError
138138

139139
def of_api_url(self, api_path, domain):

src/jmcomic/jm_config.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,18 @@ def headers(cls, authority=None):
7474
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,'
7575
'application/signed-exchange;v=b3;q=0.7',
7676
'accept-language': 'zh-CN,zh;q=0.9',
77+
'cache-control': 'no-cache',
7778
'referer': 'https://18comic.vip',
78-
'sec-ch-ua': '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
79+
'pragma': 'no-cache',
80+
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
7981
'sec-ch-ua-mobile': '?0',
8082
'sec-ch-ua-platform': '"Windows"',
8183
'sec-fetch-dest': 'document',
8284
'sec-fetch-mode': 'navigate',
83-
'sec-fetch-site': 'same-origin',
85+
'sec-fetch-site': 'none',
8486
'sec-fetch-user': '?1',
8587
'upgrade-insecure-requests': '1',
86-
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 '
88+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 '
8789
'Safari/537.36',
8890
}
8991

src/jmcomic/jm_entity.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,33 @@ def __iter__(self) -> Generator[JmPhotoDetail, Any, None]:
306306

307307
class JmSearchPage(IterableEntity):
308308

309-
def __init__(self, album_info_list):
309+
def __init__(self, album_info_list: List[Tuple[str, str, StrNone, StrNone, List[str]]]):
310310
# (album_id, title, category_none, label_sub_none, tag_list)
311-
self.album_info_list: List[Tuple[str, str, StrNone, StrNone, List[str]]] = album_info_list
311+
self.album_info_list = album_info_list
312312

313313
def __len__(self):
314314
return len(self.album_info_list)
315315

316316
def __getitem__(self, item):
317317
return self.album_info_list[item][0:2]
318+
319+
@property
320+
def single_album(self) -> JmAlbumDetail:
321+
return getattr(self, 'album')
322+
323+
@classmethod
324+
def wrap_single_album(cls, album: JmAlbumDetail) -> 'JmSearchPage':
325+
# ('462257', '[無邪気漢化組] [きょくちょ] 楓と鈴 4.5', '短篇', '漢化', [])
326+
# (album_id, title, category_none, label_sub_none, tag_list)
327+
328+
album_info = (
329+
album.album_id,
330+
album.title,
331+
None,
332+
None,
333+
album.keywords,
334+
)
335+
obj = JmSearchPage([album_info])
336+
337+
setattr(obj, 'album', album)
338+
return obj

tests/test_jmcomic/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ def adapt_macos(cls):
7474

7575
@classmethod
7676
def enable_client_cache(cls):
77-
cls.client.enable_cache()
77+
cls.client.enable_cache(debug=True)

usage/sample_usage.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ def download_jm_album():
2020
@timeit('获取实体类: ')
2121
def get_album_photo_detail():
2222
client = jm_option.build_jm_client()
23+
# 启用缓存,会缓存id → album和photo的实体类
24+
client.enable_cache(debug=True)
25+
2326
album: JmAlbumDetail = client.get_album_detail('427413')
2427

2528
def show(p):
26-
p: JmPhotoDetail = client.get_photo_detail(p.photo_id)
29+
p: JmPhotoDetail = client.get_photo_detail(p.photo_id, False)
2730
for img in p:
2831
img: JmImageDetail
2932
print(img.img_url)
@@ -37,10 +40,31 @@ def show(p):
3740
@timeit('搜索本子: ')
3841
def search_jm_album():
3942
client = jm_option.build_jm_client()
40-
search_album: JmSearchPage = client.search_album(search_query='+MANA +无修正')
41-
for album_id, title, *_args in search_album:
42-
print(f'[{album_id}]:{title}')
4343

44+
# 分页查询
45+
search_page: JmSearchPage = client.search_album(search_query='+MANA +无修正', page=1)
46+
for album_id, title in search_page:
47+
print(f'[{album_id}]: {title}')
48+
49+
# 直接搜索禁漫车号
50+
search_page = client.search_album(search_query='427413')
51+
album: JmAlbumDetail = search_page.single_album
52+
print(album.keywords)
53+
54+
@timeit('搜索并下载本子: ')
55+
def search_and_download():
56+
tag = '無修正'
57+
search_album: JmSearchPage = cl.search_album(tag, main_tag=3)
58+
59+
id_list = []
60+
61+
for arg in search_album.album_info_list:
62+
(album_id, title, category_none, label_sub_none, tag_list) = arg
63+
if tag in tag_list:
64+
print(f'[标签/{tag}] 发现目标: [{album_id}]: [{title}]')
65+
id_list.append(album_id)
66+
67+
download_album(id_list, op)
4468

4569
def main():
4670
search_jm_album()

0 commit comments

Comments
 (0)