Skip to content

Commit

Permalink
v2.3.9: 支持默认使用系统代理,优化保存图片部分代码,优化文档。 (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
hect0x7 authored Oct 18, 2023
1 parent f4babde commit 6ab3bc4
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 40 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/release_auto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions assets/config/常用配置介绍.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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。
# 禁漫的大部分本子,下载是不需要登录的;少部分敏感题材需要登录才能看。
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.8'
__version__ = '2.3.9'

from .api import *
from .jm_plugin import *
Expand Down
8 changes: 2 additions & 6 deletions src/jmcomic/jm_client_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,9 @@ def download_image(self,

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,
Expand Down
15 changes: 14 additions & 1 deletion src/jmcomic/jm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -249,9 +254,10 @@ def new_postman(cls, session=False, **kwargs):
'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,
Expand All @@ -273,6 +279,7 @@ def new_postman(cls, session=False, **kwargs):
'meta_data': {
'impersonate': 'chrome110',
'headers': None,
'proxies': None,
}
},
'impl': None,
Expand Down Expand Up @@ -310,6 +317,12 @@ def option_default_dict(cls) -> dict:
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:
Expand Down
14 changes: 9 additions & 5 deletions src/jmcomic/jm_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]] = {}

Expand Down Expand Up @@ -103,12 +100,19 @@ def download_by_image_detail(self, image: JmImageDetail, client: JmcomicClient):
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)

Expand Down
11 changes: 7 additions & 4 deletions src/jmcomic/jm_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def __init__(self,
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:
"""
Expand Down Expand Up @@ -143,12 +147,11 @@ def of(cls,
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)}]'


Expand Down
41 changes: 21 additions & 20 deletions src/jmcomic/jm_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,8 @@ def __init__(self,

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
Expand Down Expand Up @@ -196,19 +184,28 @@ def decide_album_dir(self, album: JmAlbumDetail) -> str:

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

"""
下面是创建对象相关方法
"""
Expand Down Expand Up @@ -272,6 +269,10 @@ def deconstruct(self) -> Dict:
'client': self.client.src_dict,
}

"""
下面是文件IO方法
"""

@classmethod
def from_file(cls, filepath: str) -> 'JmOption':
dic: dict = PackerUtil.unpack(filepath)[0]
Expand All @@ -286,7 +287,7 @@ def to_file(self, filepath=None):
PackerUtil.pack(self.deconstruct(), filepath)

"""
下面是 build 方法
下面是创建客户端的相关方法
"""

@field_cache("__jm_client_cache__")
Expand Down
3 changes: 3 additions & 0 deletions usage/getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,8 @@

# 方式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')

0 comments on commit 6ab3bc4

Please sign in to comment.