Skip to content

Commit 6ab3bc4

Browse files
authored
v2.3.9: 支持默认使用系统代理,优化保存图片部分代码,优化文档。 (#150)
1 parent f4babde commit 6ab3bc4

File tree

9 files changed

+64
-40
lines changed

9 files changed

+64
-40
lines changed

.github/workflows/release_auto.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ name: Auto Release & Publish
33

44
on:
55
workflow_dispatch:
6+
push:
7+
branches:
8+
- master
69

710
jobs:
811
release:
912
runs-on: ubuntu-latest
10-
13+
if: startsWith(github.event.head_commit.message, 'v')
1114
steps:
1215
- uses: actions/checkout@v3
1316

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ client:
1313

1414
postman:
1515
meta_data:
16-
# proxies: 代理配置,默认是 null,表示不使用代理
16+
# proxies: 代理配置,默认是 system,表示使用系统代理
1717
# 以下的写法都可以:
18+
# proxies: null # 不使用代理
1819
# proxies: clash
1920
# proxies: v2ray
2021
# proxies: 127.0.0.1:7890
2122
# proxies:
2223
# http: 127.0.0.1:7890
2324
# https: 127.0.0.1:7890
24-
proxies: null
25+
proxies: system
2526

2627
# cookies: 帐号配置,默认是 null,表示未登录状态访问JM。
2728
# 禁漫的大部分本子,下载是不需要登录的;少部分敏感题材需要登录才能看。

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.8'
5+
__version__ = '2.3.9'
66

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

src/jmcomic/jm_client_interface.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,9 @@ def download_image(self,
247247

248248
return self.save_image_resp(decode_image, img_save_path, img_url, resp, scramble_id)
249249

250+
# noinspection PyMethodMayBeStatic
250251
def save_image_resp(self, decode_image, img_save_path, img_url, resp, scramble_id):
251-
# gif图无需加解密,需要最先判断
252-
if self.img_is_not_need_to_decode(img_url, resp):
253-
# 相当于调用save_directly,但使用save_resp_img可以统一调用入口
254-
JmImageTool.save_resp_img(resp, img_save_path, False)
255-
else:
256-
resp.transfer_to(img_save_path, scramble_id, decode_image, img_url)
252+
resp.transfer_to(img_save_path, scramble_id, decode_image, img_url)
257253

258254
def download_by_image_detail(self,
259255
image: JmImageDetail,

src/jmcomic/jm_config.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def default_raise_exception_executor(msg, _extra):
2121
raise JmModuleConfig.CLASS_EXCEPTION(msg)
2222

2323

24+
def system_proxy():
25+
from common import ProxyBuilder
26+
return ProxyBuilder.system_proxy()
27+
28+
2429
class JmcomicException(Exception):
2530
pass
2631

@@ -249,9 +254,10 @@ def new_postman(cls, session=False, **kwargs):
249254
'x-requested-with': 'XMLHttpRequest',
250255
}
251256

252-
# option 默认配置字典
257+
# option 相关的默认配置
253258
JM_OPTION_VER = '2.1'
254259
CLIENT_IMPL_DEFAULT = 'html'
260+
DEFAULT_PROXIES = system_proxy() # use system proxy by default
255261

256262
default_option_dict: dict = {
257263
'version': JM_OPTION_VER,
@@ -273,6 +279,7 @@ def new_postman(cls, session=False, **kwargs):
273279
'meta_data': {
274280
'impersonate': 'chrome110',
275281
'headers': None,
282+
'proxies': None,
276283
}
277284
},
278285
'impl': None,
@@ -310,6 +317,12 @@ def option_default_dict(cls) -> dict:
310317
if client['impl'] is None:
311318
client['impl'] = cls.CLIENT_IMPL_DEFAULT
312319

320+
# postman proxies
321+
meta_data = client['postman']['meta_data']
322+
if meta_data['proxies'] is None:
323+
# use system proxy by default
324+
meta_data['proxies'] = cls.DEFAULT_PROXIES
325+
313326
# threading photo
314327
dt = option_dict['download']['threading']
315328
if dt['photo'] is None:

src/jmcomic/jm_downloader.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ class JmDownloader(DownloadCallback):
6262

6363
def __init__(self, option: JmOption) -> None:
6464
self.option = option
65-
self.use_cache = self.option.download_cache
66-
self.decode_image = self.option.download_image_decode
67-
6865
# 收集所有下载的image,为plugin提供数据
6966
self.all_downloaded: Dict[JmAlbumDetail, Dict[JmPhotoDetail, List[Tuple[str, JmImageDetail]]]] = {}
7067

@@ -103,12 +100,19 @@ def download_by_image_detail(self, image: JmImageDetail, client: JmcomicClient):
103100
image.is_exists = file_exists(img_save_path)
104101

105102
self.before_image(image, img_save_path)
106-
if self.use_cache is True and image.is_exists:
103+
104+
# let option decide use_cache and decode_image
105+
use_cache = self.option.decide_download_cache(image)
106+
decode_image = self.option.decide_download_image_decode(image)
107+
108+
# skip download
109+
if use_cache is True and image.is_exists:
107110
return
111+
108112
client.download_by_image_detail(
109113
image,
110114
img_save_path,
111-
decode_image=self.decode_image,
115+
decode_image=decode_image,
112116
)
113117
self.after_image(image, img_save_path)
114118

src/jmcomic/jm_entity.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def __init__(self,
101101
def filename_without_suffix(self):
102102
return self.img_file_name
103103

104+
@property
105+
def is_gif(self):
106+
return self.img_file_suffix == '.gif'
107+
104108
@property
105109
def download_url(self) -> str:
106110
"""
@@ -143,12 +147,11 @@ def of(cls,
143147
index=index,
144148
)
145149

146-
"""
147-
below help for debug method
148-
"""
149-
150150
@property
151151
def tag(self) -> str:
152+
"""
153+
this tag is used to print pretty info when debug
154+
"""
152155
return f'{self.aid}/{self.img_file_name}{self.img_file_suffix} [{self.index + 1}/{len(self.from_photo)}]'
153156

154157

src/jmcomic/jm_option.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,8 @@ def __init__(self,
139139

140140
self.call_all_plugin('after_init')
141141

142-
@property
143-
def download_cache(self):
144-
return self.download.cache
145-
146-
@property
147-
def download_image_decode(self):
148-
return self.download.image.decode
149-
150-
@property
151-
def download_image_suffix(self):
152-
return self.download.image.suffix
153-
154142
"""
155-
下面是决定图片保存路径的方法
143+
下面是decide系列方法,为了支持重写和增加程序动态性。
156144
"""
157145

158146
# noinspection PyUnusedLocal
@@ -196,19 +184,28 @@ def decide_album_dir(self, album: JmAlbumDetail) -> str:
196184

197185
def decide_image_suffix(self, image: JmImageDetail):
198186
# 动图则使用原后缀
199-
suffix = image.img_file_suffix
200-
if suffix.endswith("gif"):
201-
return suffix
187+
if image.is_gif:
188+
return image.img_file_suffix
202189

203190
# 非动图,以配置为先
204-
return self.download_image_suffix or suffix
191+
return self.download.image.suffix or image.img_file_suffix
205192

206-
def decide_image_filepath(self, image: JmImageDetail) -> str:
193+
def decide_image_filepath(self, image: JmImageDetail, consider_custom_suffix=True) -> str:
207194
# 通过拼接生成绝对路径
208195
save_dir = self.decide_image_save_dir(image.from_photo)
209-
suffix = self.decide_image_suffix(image)
196+
suffix = self.decide_image_suffix(image) if consider_custom_suffix else image.img_file_suffix
210197
return os.path.join(save_dir, image.filename_without_suffix + suffix)
211198

199+
def decide_download_cache(self, _image: JmImageDetail) -> bool:
200+
return self.download.cache
201+
202+
def decide_download_image_decode(self, image: JmImageDetail) -> bool:
203+
# .gif file needn't be decoded
204+
if image.is_gif:
205+
return False
206+
207+
return self.download.image.decode
208+
212209
"""
213210
下面是创建对象相关方法
214211
"""
@@ -272,6 +269,10 @@ def deconstruct(self) -> Dict:
272269
'client': self.client.src_dict,
273270
}
274271

272+
"""
273+
下面是文件IO方法
274+
"""
275+
275276
@classmethod
276277
def from_file(cls, filepath: str) -> 'JmOption':
277278
dic: dict = PackerUtil.unpack(filepath)[0]
@@ -286,7 +287,7 @@ def to_file(self, filepath=None):
286287
PackerUtil.pack(self.deconstruct(), filepath)
287288

288289
"""
289-
下面是 build 方法
290+
下面是创建客户端的相关方法
290291
"""
291292

292293
@field_cache("__jm_client_cache__")

usage/getting_started.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,8 @@
8484

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

0 commit comments

Comments
 (0)