Skip to content

Commit df41112

Browse files
authored
v2.3.5: 优化章节去重算法,优化实体类字段,增加dispatch工作流的可输入配置 (#146)
1 parent fc3842f commit df41112

14 files changed

+153
-108
lines changed

.github/workflows/download_dispatch.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ on:
1010

1111
JM_PHOTO_IDS:
1212
type: string
13-
description: 章节id(可以单独下载某个本子的某个章节,多个id用-隔开(同上)
13+
description: 章节id(单独下载章节,多个id同上
1414
required: false
1515

1616
DIR_RULE:
1717
type: string
18-
description: 下载文件夹规则,对应option配置文件里的dir_rule.rule。此处可以不填,默认使用repo里的配置文件的`Bd_Aauthor_Atitle_Pindex`。
18+
description: 下载文件夹规则(dir_rule.rule)。此处可以不填,默认使用配置文件的'Bd_Aauthor_Atitle_Pindex'。
19+
default: ''
20+
required: false
21+
22+
CLIENT_IMPL:
23+
type: string
24+
description: 客户端类型(client.impl),[api]=移动端,[html]=网页端,此处可以不填,默认使用'html'。如果你发现默认的下载不了,可以填api试试。
1925
default: ''
2026
required: false
2127

@@ -52,6 +58,7 @@ jobs:
5258
JM_ALBUM_IDS: ${{ github.event.inputs.JM_ALBUM_IDS }}
5359
JM_PHOTO_IDS: ${{ github.event.inputs.JM_PHOTO_IDS }}
5460
DIR_RULE: ${{ github.event.inputs.DIR_RULE }}
61+
CLIENT_IMPL: ${{ github.event.inputs.CLIENT_IMPL }}
5562
ZIP_NAME: ${{ github.event.inputs.ZIP_NAME }}
5663
UPLOAD_NAME: ${{ github.event.inputs.UPLOAD_NAME }}
5764
# secrets

assets/config/option_test_api.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33

44
client:
55
impl: api
6-
domain: [
7-
"www.jmapinode1.cc",
8-
"www.jmapinode2.cc",
9-
"www.jmapinode3.cc",
10-
"www.jmapibranch2.cc"
11-
]
6+
retry_times: 3
127
postman:
138
meta_data:
14-
timeout: 10
9+
timeout: 5
1510

1611
# 插件配置
1712
plugin:

assets/config/option_test_html.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
# 网页端配置
33

44
client:
5+
impl: html
6+
retry_times: 3
7+
postman:
8+
meta_data:
9+
timeout: 5
510
domain:
611
- jmcomic1.me
712
- jmcomic.me
8-
postman:
9-
meta_data:
10-
timeout: 10
1113

1214
# 插件配置
1315
plugin:

assets/config/option_workflow_download.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ dir_rule:
44
rule: Bd_Aauthor_Atitle_Pindex
55

66
client:
7-
impl: api
8-
domain: [
9-
"www.jmapinode1.cc",
10-
"www.jmapinode2.cc",
11-
"www.jmapinode3.cc",
12-
"www.jmapibranch2.cc",
13-
]
7+
domain:
8+
- jmcomic1.me
9+
- jmcomic.me
1410

1511
# 插件配置
1612
plugin:

assets/config/常用配置介绍.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
client:
2+
# impl: 客户端实现类,不配默认是html,表示网页端
3+
impl: html
4+
25
# domain: 域名配置,默认是 [],表示运行时自动获取域名。
36
# 可配置特定域名,如下:
47
# 程序会先用第一个域名,如果第一个域名重试n次失败,则换下一个域名重试,以此类推。

assets/docs/教程:使用GitHub Actions下载禁漫本子.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
直接拉到页面最底部,如下所示:
1818

19+
(最新提示,下图的1可以不做,即直接点绿色的Create fork按钮)
20+
1921
![1](./images/1.png)
2022

2123
## 2. 填写你需要下载的本子id
@@ -30,8 +32,6 @@
3032

3133
![5](./images/5.png)
3234

33-
34-
3535
### 2.2. 方式二
3636

3737
访问下面这个网址:

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.3.4'
5+
__version__ = '2.3.5'
66

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

src/jmcomic/jm_client_impl.py

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def __init__(self,
2424
fallback_domain_list.insert(0, domain)
2525

2626
self.domain_list = fallback_domain_list
27+
self.after_init()
28+
29+
def after_init(self):
30+
pass
2731

2832
def get(self, url, **kwargs):
2933
return self.request_with_retry(self.postman.get, url, **kwargs)
@@ -104,31 +108,35 @@ def before_retry(self, e, kwargs, retry_count, url):
104108
jm_debug('req.error', str(e))
105109

106110
def enable_cache(self, debug=False):
111+
if self.is_cache_enabled():
112+
return
113+
107114
def wrap_func_cache(func_name, cache_dict_name):
108-
import common
109-
if common.VERSION > '0.4.8':
110-
if hasattr(self, cache_dict_name):
111-
return
115+
if hasattr(self, cache_dict_name):
116+
return
112117

113-
cache = common.cache
114-
cache_dict = {}
115-
cache_hit_msg = (f'【缓存命中】{cache_dict_name} ' + '→ [{}]') if debug is True else None
116-
cache_miss_msg = (f'【缓存缺失】{cache_dict_name} ' + '← [{}]') if debug is True else None
117-
cache = cache(
118-
cache_dict=cache_dict,
119-
cache_hit_msg=cache_hit_msg,
120-
cache_miss_msg=cache_miss_msg,
121-
)
122-
setattr(self, cache_dict_name, cache_dict)
118+
if sys.version_info > (3, 9):
119+
import functools
120+
cache = functools.cache
123121
else:
124-
if sys.version_info < (3, 9):
122+
import common
123+
if common.VERSION > '0.4.8':
124+
cache = common.cache
125+
cache_dict = {}
126+
cache_hit_msg = (f'【缓存命中】{cache_dict_name} ' + '→ [{}]') if debug is True else None
127+
cache_miss_msg = (f'【缓存缺失】{cache_dict_name} ' + '← [{}]') if debug is True else None
128+
cache = cache(
129+
cache_dict=cache_dict,
130+
cache_hit_msg=cache_hit_msg,
131+
cache_miss_msg=cache_miss_msg,
132+
)
133+
setattr(self, cache_dict_name, cache_dict)
134+
else:
125135
ExceptionTool.raises('不支持启用JmcomicClient缓存。\n'
126136
'请更新python版本到3.9以上,'
127137
'或更新commonX: `pip install commonX --upgrade`')
128-
import functools
129-
cache = functools.cache
138+
return
130139

131-
# 重载本对象的方法
132140
func = getattr(self, func_name)
133141
wrap_func = cache(func)
134142

@@ -399,14 +407,6 @@ class JmApiClient(AbstractJmClient):
399407
API_CHAPTER = '/chapter'
400408
API_SCRAMBLE = '/chapter_view_template'
401409

402-
def __init__(self,
403-
postman: Postman,
404-
retry_times: int,
405-
domain=None,
406-
fallback_domain_list=None,
407-
):
408-
super().__init__(postman, retry_times, domain, fallback_domain_list)
409-
410410
def search(self,
411411
search_query: str,
412412
page: int,
@@ -445,19 +445,34 @@ def get_album_detail(self, album_id) -> JmAlbumDetail:
445445
)
446446

447447
def get_photo_detail(self, photo_id, fetch_album=True) -> JmPhotoDetail:
448-
photo_id = JmcomicText.parse_to_photo_id(photo_id)
449448
photo: JmPhotoDetail = self.fetch_detail_entity(photo_id,
450449
JmModuleConfig.photo_class(),
451450
)
452-
photo.scramble_id = self.get_scramble_id(photo.photo_id)
451+
self.fetch_photo_additional_field(photo, fetch_album)
453452
return photo
454453

454+
def get_scramble_id(self, photo_id):
455+
"""
456+
带有缓存的fetch_scramble_id,缓存位于JmModuleConfig.SCRAMBLE_CACHE
457+
"""
458+
cache = JmModuleConfig.SCRAMBLE_CACHE
459+
if photo_id in cache:
460+
return cache[photo_id]
461+
462+
scramble_id = self.fetch_scramble_id(photo_id)
463+
cache[photo_id] = scramble_id
464+
return scramble_id
465+
455466
def fetch_detail_entity(self, apid, clazz, **kwargs):
467+
"""
468+
请求实体类
469+
"""
470+
apid = JmcomicText.parse_to_album_id(apid)
456471
url = self.API_ALBUM if issubclass(clazz, JmAlbumDetail) else self.API_CHAPTER
457472
resp = self.get_decode(
458473
url,
459474
params={
460-
'id': JmcomicText.parse_to_album_id(apid),
475+
'id': apid,
461476
**kwargs,
462477
}
463478
)
@@ -466,11 +481,11 @@ def fetch_detail_entity(self, apid, clazz, **kwargs):
466481

467482
return JmApiAdaptTool.parse_entity(resp.res_data, clazz)
468483

469-
def get_scramble_id(self, photo_id):
470-
cache = JmModuleConfig.SCRAMBLE_CACHE
471-
if photo_id in cache:
472-
return cache[photo_id]
473-
484+
def fetch_scramble_id(self, photo_id):
485+
"""
486+
请求scramble_id
487+
"""
488+
photo_id: str = JmcomicText.parse_to_photo_id(photo_id)
474489
resp = self.get_decode(
475490
self.API_SCRAMBLE,
476491
params={
@@ -487,11 +502,43 @@ def get_scramble_id(self, photo_id):
487502
scramble_id = match[1]
488503
else:
489504
jm_debug('api.scramble', '未从响应中匹配到scramble_id,返回默认值220980')
490-
scramble_id = 220980
505+
scramble_id = '220980'
491506

492-
cache[photo_id] = scramble_id
493507
return scramble_id
494508

509+
def fetch_photo_additional_field(self, photo: JmPhotoDetail, fetch_album: bool):
510+
"""
511+
获取章节的额外信息
512+
1. scramble_id
513+
2. album
514+
515+
这里的难点是,是否要采用异步的方式并发请求。
516+
"""
517+
aid = photo.album_id
518+
pid = photo.photo_id
519+
scramble_cache = JmModuleConfig.SCRAMBLE_CACHE
520+
521+
if fetch_album is False and pid in scramble_cache:
522+
# 不用发请求,直接返回
523+
photo.scramble_id = scramble_cache[pid]
524+
return
525+
526+
if fetch_album is True and pid not in scramble_cache:
527+
# 要发起两个请求,这里实现很简易,直接排队请求
528+
# todo: 改进实现
529+
# 1. 直接开两个线程跑
530+
# 2. 开两个线程,但是开之前检查重复性
531+
# 3. 线程池,也要检查重复性
532+
# 23做法要改不止一处地方
533+
photo.from_album = self.get_scramble_id(pid)
534+
photo.scramble_id = self.get_album_detail(aid)
535+
return
536+
537+
if fetch_album is True:
538+
photo.from_album = self.get_album_detail(aid)
539+
else:
540+
photo.scramble_id = self.get_scramble_id(pid)
541+
495542
def get_decode(self, url, **kwargs) -> JmApiResp:
496543
# set headers
497544
headers, key_ts = self.headers_key_ts

0 commit comments

Comments
 (0)