v2.0.5: 更新禁漫的发布页地址,优化代码 (#59)

This commit is contained in:
hect0x7 2023-06-20 15:53:02 +08:00 committed by GitHub
parent 1b38daf180
commit e6df29212e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 19 additions and 111 deletions

View File

@ -11,10 +11,10 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.7
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.7"
python-version: "3.11"
- name: 构建模块
run: |

View File

@ -2,6 +2,6 @@
# 被依赖方 <--- 使用方
# config <--- entity <--- toolkit <--- client <--- option
__version__ = '2.0.4'
__version__ = '2.0.5'
from .api import *

View File

@ -113,7 +113,7 @@ class JmHtmlClient(AbstractJmClient):
# 用 JmcomicText 解析 html返回实体类
return JmcomicText.analyse_jm_album_html(resp.text)
def get_photo_detail(self, photo_id: str, album=True) -> JmPhotoDetail:
def get_photo_detail(self, photo_id, fetch_album=True) -> JmPhotoDetail:
# 参数校验
photo_id = JmcomicText.parse_to_photo_id(photo_id)
@ -124,7 +124,7 @@ class JmHtmlClient(AbstractJmClient):
photo_detail = JmcomicText.analyse_jm_photo_html(resp.text)
# 一并获取该章节的所处本子
if album is True:
if fetch_album is True:
photo_detail.from_album = self.get_album_detail(photo_detail.album_id)
return photo_detail

View File

@ -127,7 +127,7 @@ class JmDetailClient:
def get_album_detail(self, album_id) -> JmAlbumDetail:
raise NotImplementedError
def get_photo_detail(self, photo_id: str, album=True) -> JmPhotoDetail:
def get_photo_detail(self, photo_id, fetch_album=True) -> JmPhotoDetail:
raise NotImplementedError
def ensure_photo_can_use(self, photo_detail: JmPhotoDetail):
@ -210,4 +210,8 @@ class JmcomicClient(
JmUserClient,
Postman,
):
pass
def get_jmcomic_url(self, postman=None):
return JmModuleConfig.get_jmcomic_url(postman or self)
def get_jmcomic_domain_all(self, postman=None):
return JmModuleConfig.get_jmcomic_domain_all(postman or self)

View File

@ -8,7 +8,7 @@ class JmModuleConfig:
PROT = "https://"
DOMAIN = None
JM_REDIRECT_URL = f'{PROT}jm365.xyz/3YeBdF' # 永久網域,怕走失的小伙伴收藏起来
JM_PUB_URL = f'{PROT}jmcomic1.bet'
JM_PUB_URL = f'{PROT}jmcomic2.bet'
JM_CDN_IMAGE_URL_TEMPLATE = PROT + 'cdn-msp.{domain}/media/photos/{photo_id}/{index:05}{suffix}' # index 从1开始
JM_IMAGE_SUFFIX = ['.jpg', '.webp', '.png', '.gif']
@ -92,7 +92,7 @@ class JmModuleConfig:
"""
if postman is None:
from common import Postmans
postman = Postmans.get_impl_clazz('cffi_Session').create()
postman = Postmans.new_session()
resp = postman.get(cls.JM_REDIRECT_URL)
url = resp.url

View File

@ -204,6 +204,9 @@ class JmPhotoDetail(WorkEntity):
def __len__(self):
return len(self.page_arr)
def __iter__(self) -> Generator[JmImageDetail, Any, None]:
return super().__iter__()
class JmAlbumDetail(WorkEntity):
@ -297,6 +300,9 @@ class JmAlbumDetail(WorkEntity):
return ret
def __iter__(self) -> Generator[JmPhotoDetail, Any, None]:
return super().__iter__()
class JmSearchPage(IterableEntity):

View File

@ -1,68 +1,6 @@
from .jm_client_impl import *
class JmOptionAdvice:
def decide_image_save_dir(self,
option: 'JmOption',
photo_detail: JmPhotoDetail,
) -> StrNone:
"""
决定一个本子图片的下载文件夹
@param option: JmOption对象
@param photo_detail: 本子章节实体类
@return: 下载文件夹为空表示不处理
"""
pass
def decide_image_filepath(self,
option: 'JmOption',
photo_detail: JmPhotoDetail,
index: int,
) -> StrNone:
"""
决定一个本子图片的绝对路径
@param option: JmOption对象
@param photo_detail: 本子章节实体类
@param index: 本子章节里的第几章图片
@return: 下载绝对路径为空表示不处理
"""
pass
def decide_image_suffix(self,
option: 'JmOption',
img_detail: JmImageDetail,
) -> StrNone:
"""
决定一个图片的保存后缀名
@param option: JmOption对象
@param img_detail: 禁漫图片实体类
@return: 保存后缀名为空表示不处理
"""
pass
class JmAdviceRegistry:
advice_registration: Dict[Any, List[JmOptionAdvice]] = {}
@classmethod
def register_advice(cls, base, *advice):
advice_ls = cls.advice_registration.get(base, None)
if advice_ls is None:
advice_ls = list(advice)
cls.advice_registration[base] = advice_ls
else:
for e in advice:
advice_ls.append(e)
return advice
@classmethod
def get_advice(cls, base) -> List[JmOptionAdvice]:
return cls.advice_registration.setdefault(base, [])
class DirRule:
rule_sample = [
# 根目录 / Album-id / Photo-序号 /
@ -205,12 +143,6 @@ class JmOption:
"""
def decide_image_save_dir(self, photo_detail) -> str:
# 先检查advice的回调如果回调支持则优先使用回调
for advice in JmAdviceRegistry.get_advice(self):
save_dir = advice.decide_image_save_dir(self, photo_detail)
if save_dir is not None:
return save_dir
# 使用 self.dir_rule 决定 save_dir
save_dir = self.dir_rule.deside_image_save_dir(
photo_detail.from_album,
@ -221,12 +153,6 @@ class JmOption:
return save_dir
def decide_image_suffix(self, img_detail: JmImageDetail):
# 先检查advice的回调如果回调支持则优先使用回调
for advice in JmAdviceRegistry.get_advice(self):
suffix = advice.decide_image_suffix(self, img_detail)
if suffix is not None:
return suffix
# 动图则使用原后缀
suffix = img_detail.img_file_suffix
if suffix.endswith("gif"):
@ -236,25 +162,12 @@ class JmOption:
return self.download_image_suffix or suffix
def decide_image_filepath(self, photo_detail: JmPhotoDetail, index: int) -> str:
# 先检查advice的回调如果回调支持则优先使用回调
for advice in JmAdviceRegistry.get_advice(self):
filepath = advice.decide_image_filepath(self, photo_detail, index)
if filepath is not None:
return filepath
# 通过拼接生成绝对路径
save_dir = self.decide_image_save_dir(photo_detail)
image: JmImageDetail = photo_detail[index]
suffix = self.decide_image_suffix(image)
return save_dir + image.img_file_name + suffix
"""
下面是对Advice的支持
"""
def register_advice(self, *advice):
JmAdviceRegistry.register_advice(self, *advice)
"""
下面是创建对象相关方法
"""

View File

@ -1,5 +1,3 @@
from PIL import Image
from .jm_entity import *

View File

@ -48,19 +48,6 @@ class Test_Api(JmTestConfigurable):
ret2 = jmcomic.download_album_batch((e for e in album_ls), self.option)
self.assertEqual(len(ret2), len(album_ls), 'Generator')
def test_jm_option_advice(self):
class MyAdvice(JmOptionAdvice):
def decide_image_filepath(self,
option: 'JmOption',
photo_detail: JmPhotoDetail,
index: int,
) -> StrNone:
return workspace(f'advice_{photo_detail[index].img_file_name}.test.png')
option = self.option
option.register_advice(MyAdvice())
jmcomic.download_album('366867', option)
def test_photo_sort(self):
client = self.option.build_jm_client()