Skip to content

Commit

Permalink
v2.3.5: 优化章节去重算法,优化实体类字段,增加dispatch工作流的可输入配置 (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
hect0x7 authored Oct 1, 2023
1 parent fc3842f commit df41112
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 108 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/download_dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ on:

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

DIR_RULE:
type: string
description: 下载文件夹规则,对应option配置文件里的dir_rule.rule。此处可以不填,默认使用repo里的配置文件的`Bd_Aauthor_Atitle_Pindex`。
description: 下载文件夹规则(dir_rule.rule)。此处可以不填,默认使用配置文件的'Bd_Aauthor_Atitle_Pindex'。
default: ''
required: false

CLIENT_IMPL:
type: string
description: 客户端类型(client.impl),[api]=移动端,[html]=网页端,此处可以不填,默认使用'html'。如果你发现默认的下载不了,可以填api试试。
default: ''
required: false

Expand Down Expand Up @@ -52,6 +58,7 @@ jobs:
JM_ALBUM_IDS: ${{ github.event.inputs.JM_ALBUM_IDS }}
JM_PHOTO_IDS: ${{ github.event.inputs.JM_PHOTO_IDS }}
DIR_RULE: ${{ github.event.inputs.DIR_RULE }}
CLIENT_IMPL: ${{ github.event.inputs.CLIENT_IMPL }}
ZIP_NAME: ${{ github.event.inputs.ZIP_NAME }}
UPLOAD_NAME: ${{ github.event.inputs.UPLOAD_NAME }}
# secrets
Expand Down
9 changes: 2 additions & 7 deletions assets/config/option_test_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@

client:
impl: api
domain: [
"www.jmapinode1.cc",
"www.jmapinode2.cc",
"www.jmapinode3.cc",
"www.jmapibranch2.cc"
]
retry_times: 3
postman:
meta_data:
timeout: 10
timeout: 5

# 插件配置
plugin:
Expand Down
8 changes: 5 additions & 3 deletions assets/config/option_test_html.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
# 网页端配置

client:
impl: html
retry_times: 3
postman:
meta_data:
timeout: 5
domain:
- jmcomic1.me
- jmcomic.me
postman:
meta_data:
timeout: 10

# 插件配置
plugin:
Expand Down
10 changes: 3 additions & 7 deletions assets/config/option_workflow_download.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ dir_rule:
rule: Bd_Aauthor_Atitle_Pindex

client:
impl: api
domain: [
"www.jmapinode1.cc",
"www.jmapinode2.cc",
"www.jmapinode3.cc",
"www.jmapibranch2.cc",
]
domain:
- jmcomic1.me
- jmcomic.me

# 插件配置
plugin:
Expand Down
3 changes: 3 additions & 0 deletions assets/config/常用配置介绍.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
client:
# impl: 客户端实现类,不配默认是html,表示网页端
impl: html

# domain: 域名配置,默认是 [],表示运行时自动获取域名。
# 可配置特定域名,如下:
# 程序会先用第一个域名,如果第一个域名重试n次失败,则换下一个域名重试,以此类推。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

(最新提示,下图的1可以不做,即直接点绿色的Create fork按钮)

![1](./images/1.png)

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

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



### 2.2. 方式二

访问下面这个网址:
Expand Down
2 changes: 1 addition & 1 deletion src/jmcomic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# 被依赖方 <--- 使用方
# config <--- entity <--- toolkit <--- client <--- option <--- downloader

__version__ = '2.3.4'
__version__ = '2.3.5'

from .api import *
from .jm_plugin import *
119 changes: 83 additions & 36 deletions src/jmcomic/jm_client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def __init__(self,
fallback_domain_list.insert(0, domain)

self.domain_list = fallback_domain_list
self.after_init()

def after_init(self):
pass

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

def enable_cache(self, debug=False):
if self.is_cache_enabled():
return

def wrap_func_cache(func_name, cache_dict_name):
import common
if common.VERSION > '0.4.8':
if hasattr(self, cache_dict_name):
return
if hasattr(self, cache_dict_name):
return

cache = common.cache
cache_dict = {}
cache_hit_msg = (f'【缓存命中】{cache_dict_name} ' + '→ [{}]') if debug is True else None
cache_miss_msg = (f'【缓存缺失】{cache_dict_name} ' + '← [{}]') if debug is True else None
cache = cache(
cache_dict=cache_dict,
cache_hit_msg=cache_hit_msg,
cache_miss_msg=cache_miss_msg,
)
setattr(self, cache_dict_name, cache_dict)
if sys.version_info > (3, 9):
import functools
cache = functools.cache
else:
if sys.version_info < (3, 9):
import common
if common.VERSION > '0.4.8':
cache = common.cache
cache_dict = {}
cache_hit_msg = (f'【缓存命中】{cache_dict_name} ' + '→ [{}]') if debug is True else None
cache_miss_msg = (f'【缓存缺失】{cache_dict_name} ' + '← [{}]') if debug is True else None
cache = cache(
cache_dict=cache_dict,
cache_hit_msg=cache_hit_msg,
cache_miss_msg=cache_miss_msg,
)
setattr(self, cache_dict_name, cache_dict)
else:
ExceptionTool.raises('不支持启用JmcomicClient缓存。\n'
'请更新python版本到3.9以上,'
'或更新commonX: `pip install commonX --upgrade`')
import functools
cache = functools.cache
return

# 重载本对象的方法
func = getattr(self, func_name)
wrap_func = cache(func)

Expand Down Expand Up @@ -399,14 +407,6 @@ class JmApiClient(AbstractJmClient):
API_CHAPTER = '/chapter'
API_SCRAMBLE = '/chapter_view_template'

def __init__(self,
postman: Postman,
retry_times: int,
domain=None,
fallback_domain_list=None,
):
super().__init__(postman, retry_times, domain, fallback_domain_list)

def search(self,
search_query: str,
page: int,
Expand Down Expand Up @@ -445,19 +445,34 @@ def get_album_detail(self, album_id) -> JmAlbumDetail:
)

def get_photo_detail(self, photo_id, fetch_album=True) -> JmPhotoDetail:
photo_id = JmcomicText.parse_to_photo_id(photo_id)
photo: JmPhotoDetail = self.fetch_detail_entity(photo_id,
JmModuleConfig.photo_class(),
)
photo.scramble_id = self.get_scramble_id(photo.photo_id)
self.fetch_photo_additional_field(photo, fetch_album)
return photo

def get_scramble_id(self, photo_id):
"""
带有缓存的fetch_scramble_id,缓存位于JmModuleConfig.SCRAMBLE_CACHE
"""
cache = JmModuleConfig.SCRAMBLE_CACHE
if photo_id in cache:
return cache[photo_id]

scramble_id = self.fetch_scramble_id(photo_id)
cache[photo_id] = scramble_id
return scramble_id

def fetch_detail_entity(self, apid, clazz, **kwargs):
"""
请求实体类
"""
apid = JmcomicText.parse_to_album_id(apid)
url = self.API_ALBUM if issubclass(clazz, JmAlbumDetail) else self.API_CHAPTER
resp = self.get_decode(
url,
params={
'id': JmcomicText.parse_to_album_id(apid),
'id': apid,
**kwargs,
}
)
Expand All @@ -466,11 +481,11 @@ def fetch_detail_entity(self, apid, clazz, **kwargs):

return JmApiAdaptTool.parse_entity(resp.res_data, clazz)

def get_scramble_id(self, photo_id):
cache = JmModuleConfig.SCRAMBLE_CACHE
if photo_id in cache:
return cache[photo_id]

def fetch_scramble_id(self, photo_id):
"""
请求scramble_id
"""
photo_id: str = JmcomicText.parse_to_photo_id(photo_id)
resp = self.get_decode(
self.API_SCRAMBLE,
params={
Expand All @@ -487,11 +502,43 @@ def get_scramble_id(self, photo_id):
scramble_id = match[1]
else:
jm_debug('api.scramble', '未从响应中匹配到scramble_id,返回默认值220980')
scramble_id = 220980
scramble_id = '220980'

cache[photo_id] = scramble_id
return scramble_id

def fetch_photo_additional_field(self, photo: JmPhotoDetail, fetch_album: bool):
"""
获取章节的额外信息
1. scramble_id
2. album
这里的难点是,是否要采用异步的方式并发请求。
"""
aid = photo.album_id
pid = photo.photo_id
scramble_cache = JmModuleConfig.SCRAMBLE_CACHE

if fetch_album is False and pid in scramble_cache:
# 不用发请求,直接返回
photo.scramble_id = scramble_cache[pid]
return

if fetch_album is True and pid not in scramble_cache:
# 要发起两个请求,这里实现很简易,直接排队请求
# todo: 改进实现
# 1. 直接开两个线程跑
# 2. 开两个线程,但是开之前检查重复性
# 3. 线程池,也要检查重复性
# 23做法要改不止一处地方
photo.from_album = self.get_scramble_id(pid)
photo.scramble_id = self.get_album_detail(aid)
return

if fetch_album is True:
photo.from_album = self.get_album_detail(aid)
else:
photo.scramble_id = self.get_scramble_id(pid)

def get_decode(self, url, **kwargs) -> JmApiResp:
# set headers
headers, key_ts = self.headers_key_ts
Expand Down
Loading

0 comments on commit df41112

Please sign in to comment.