v2.3.9: 支持默认使用系统代理,优化保存图片部分代码,优化文档。 (#150)

This commit is contained in:
hect0x7 2023-10-19 03:30:16 +08:00 committed by GitHub
parent f4babde82f
commit 6ab3bc48c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 64 additions and 40 deletions

View File

@ -3,11 +3,14 @@ name: Auto Release & Publish
on:
workflow_dispatch:
push:
branches:
- master
jobs:
release:
runs-on: ubuntu-latest
if: startsWith(github.event.head_commit.message, 'v')
steps:
- uses: actions/checkout@v3

View File

@ -13,15 +13,16 @@ client:
postman:
meta_data:
# proxies: 代理配置,默认是 null表示不使用代理。
# proxies: 代理配置,默认是 system表示使用系统代理。
# 以下的写法都可以:
# proxies: null # 不使用代理
# proxies: clash
# proxies: v2ray
# proxies: 127.0.0.1:7890
# proxies:
# http: 127.0.0.1:7890
# https: 127.0.0.1:7890
proxies: null
proxies: system
# cookies: 帐号配置,默认是 null表示未登录状态访问JM。
# 禁漫的大部分本子,下载是不需要登录的;少部分敏感题材需要登录才能看。

View File

@ -2,7 +2,7 @@
# 被依赖方 <--- 使用方
# config <--- entity <--- toolkit <--- client <--- option <--- downloader
__version__ = '2.3.8'
__version__ = '2.3.9'
from .api import *
from .jm_plugin import *

View File

@ -247,13 +247,9 @@ class JmImageClient:
return self.save_image_resp(decode_image, img_save_path, img_url, resp, scramble_id)
# noinspection PyMethodMayBeStatic
def save_image_resp(self, decode_image, img_save_path, img_url, resp, scramble_id):
# gif图无需加解密需要最先判断
if self.img_is_not_need_to_decode(img_url, resp):
# 相当于调用save_directly但使用save_resp_img可以统一调用入口
JmImageTool.save_resp_img(resp, img_save_path, False)
else:
resp.transfer_to(img_save_path, scramble_id, decode_image, img_url)
resp.transfer_to(img_save_path, scramble_id, decode_image, img_url)
def download_by_image_detail(self,
image: JmImageDetail,

View File

@ -21,6 +21,11 @@ def default_raise_exception_executor(msg, _extra):
raise JmModuleConfig.CLASS_EXCEPTION(msg)
def system_proxy():
from common import ProxyBuilder
return ProxyBuilder.system_proxy()
class JmcomicException(Exception):
pass
@ -249,9 +254,10 @@ class JmModuleConfig:
'x-requested-with': 'XMLHttpRequest',
}
# option 默认配置字典
# option 相关的默认配置
JM_OPTION_VER = '2.1'
CLIENT_IMPL_DEFAULT = 'html'
DEFAULT_PROXIES = system_proxy() # use system proxy by default
default_option_dict: dict = {
'version': JM_OPTION_VER,
@ -273,6 +279,7 @@ class JmModuleConfig:
'meta_data': {
'impersonate': 'chrome110',
'headers': None,
'proxies': None,
}
},
'impl': None,
@ -310,6 +317,12 @@ class JmModuleConfig:
if client['impl'] is None:
client['impl'] = cls.CLIENT_IMPL_DEFAULT
# postman proxies
meta_data = client['postman']['meta_data']
if meta_data['proxies'] is None:
# use system proxy by default
meta_data['proxies'] = cls.DEFAULT_PROXIES
# threading photo
dt = option_dict['download']['threading']
if dt['photo'] is None:

View File

@ -62,9 +62,6 @@ class JmDownloader(DownloadCallback):
def __init__(self, option: JmOption) -> None:
self.option = option
self.use_cache = self.option.download_cache
self.decode_image = self.option.download_image_decode
# 收集所有下载的image为plugin提供数据
self.all_downloaded: Dict[JmAlbumDetail, Dict[JmPhotoDetail, List[Tuple[str, JmImageDetail]]]] = {}
@ -103,12 +100,19 @@ class JmDownloader(DownloadCallback):
image.is_exists = file_exists(img_save_path)
self.before_image(image, img_save_path)
if self.use_cache is True and image.is_exists:
# let option decide use_cache and decode_image
use_cache = self.option.decide_download_cache(image)
decode_image = self.option.decide_download_image_decode(image)
# skip download
if use_cache is True and image.is_exists:
return
client.download_by_image_detail(
image,
img_save_path,
decode_image=self.decode_image,
decode_image=decode_image,
)
self.after_image(image, img_save_path)

View File

@ -101,6 +101,10 @@ class JmImageDetail(JmBaseEntity):
def filename_without_suffix(self):
return self.img_file_name
@property
def is_gif(self):
return self.img_file_suffix == '.gif'
@property
def download_url(self) -> str:
"""
@ -143,12 +147,11 @@ class JmImageDetail(JmBaseEntity):
index=index,
)
"""
below help for debug method
"""
@property
def tag(self) -> str:
"""
this tag is used to print pretty info when debug
"""
return f'{self.aid}/{self.img_file_name}{self.img_file_suffix} [{self.index + 1}/{len(self.from_photo)}]'

View File

@ -139,20 +139,8 @@ class JmOption:
self.call_all_plugin('after_init')
@property
def download_cache(self):
return self.download.cache
@property
def download_image_decode(self):
return self.download.image.decode
@property
def download_image_suffix(self):
return self.download.image.suffix
"""
下面是决定图片保存路径的方法
下面是decide系列方法为了支持重写和增加程序动态性
"""
# noinspection PyUnusedLocal
@ -196,19 +184,28 @@ class JmOption:
def decide_image_suffix(self, image: JmImageDetail):
# 动图则使用原后缀
suffix = image.img_file_suffix
if suffix.endswith("gif"):
return suffix
if image.is_gif:
return image.img_file_suffix
# 非动图,以配置为先
return self.download_image_suffix or suffix
return self.download.image.suffix or image.img_file_suffix
def decide_image_filepath(self, image: JmImageDetail) -> str:
def decide_image_filepath(self, image: JmImageDetail, consider_custom_suffix=True) -> str:
# 通过拼接生成绝对路径
save_dir = self.decide_image_save_dir(image.from_photo)
suffix = self.decide_image_suffix(image)
suffix = self.decide_image_suffix(image) if consider_custom_suffix else image.img_file_suffix
return os.path.join(save_dir, image.filename_without_suffix + suffix)
def decide_download_cache(self, _image: JmImageDetail) -> bool:
return self.download.cache
def decide_download_image_decode(self, image: JmImageDetail) -> bool:
# .gif file needn't be decoded
if image.is_gif:
return False
return self.download.image.decode
"""
下面是创建对象相关方法
"""
@ -272,6 +269,10 @@ class JmOption:
'client': self.client.src_dict,
}
"""
下面是文件IO方法
"""
@classmethod
def from_file(cls, filepath: str) -> 'JmOption':
dic: dict = PackerUtil.unpack(filepath)[0]
@ -286,7 +287,7 @@ class JmOption:
PackerUtil.pack(self.deconstruct(), filepath)
"""
下面是 build 方法
下面是创建客户端的相关方法
"""
@field_cache("__jm_client_cache__")

View File

@ -84,5 +84,8 @@ download_album('xxx', option)
# 方式2. 使用全局配置
JmModuleConfig.default_option_dict['client']['postman']['meta_data']['proxies'] = ProxyBuilder.clash_proxy()
# v2.3.9 以后,支持更简便的代理配置,且不配时默认使用系统代理:
JmModuleConfig.DEFAULT_PROXIES = ProxyBuilder.clash_proxy()
# 调用下载api**不需要**传入option
download_album('xxx')