Skip to content

Commit

Permalink
v2.5.8: 新增插件【订阅本子更新插件】,可以自动订阅本子的章节更新,并下载和发送邮件通知; 优化代码。 (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
hect0x7 authored Mar 16, 2024
1 parent 147c796 commit ee23037
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 14 deletions.
17 changes: 16 additions & 1 deletion assets/docs/sources/option_file_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
因此,在配置option时,不需要配置全部的值,只需要配置特定部分即可。
* 你可以使用下面的代码来得到option的默认值,你可以删除其中的大部分配置项,只保留你要覆盖的配置项

* **下面的插件配置,kwargs参数支持引用环境变量,语法为 ${环境变量名}**

```python
from jmcomic import JmOption
JmOption.default().to_file('./option.yml') # 创建默认option,导出为option.yml文件
Expand Down Expand Up @@ -84,6 +86,8 @@ download:
# 文件夹规则配置,决定图片文件存放在你的电脑上的哪个文件夹
dir_rule:
# base_dir: 根目录。
# 此配置也支持引用环境变量,例如
# base_dir: ${JM_DIR}/下载文件夹/
base_dir: D:/a/b/c/

# rule: 规则dsl。
Expand All @@ -106,7 +110,6 @@ dir_rule:
```yml
# 插件的配置示例
# 当kwargs的值为字符串类型时,支持使用环境变量,语法为 ${环境变量名}
plugins:
after_init:
- plugin: usage_log # 实时打印硬件占用率的插件
Expand Down Expand Up @@ -160,6 +163,18 @@ plugins:
bg.jpg: D:/浏览器的背景图
m_bg.jpeg: D:/移动设备浏览器的背景图

- plugin: subscribe_album_update # 自动订阅本子并下载、发送邮件通知的插件
kwargs:
download_if_has_update: true
email_notify: # 见下【发送qq邮件插件】
msg_from: x
msg_to: x
password: x
title: album update !!!
content: album update !!!
album_photo_dict:
324930: 424507

after_album:
- plugin: zip # 压缩文件插件
kwargs:
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.5.7'
__version__ = '2.5.8'

from .api import *
from .jm_plugin import *
Expand Down
8 changes: 4 additions & 4 deletions src/jmcomic/jm_client_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def json(self) -> Dict:
except Exception as e:
ExceptionTool.raises_resp(f'json解析失败: {e}', self, JsonResolveFailException)

def model(self) -> AdvancedEasyAccessDict:
return AdvancedEasyAccessDict(self.json())
def model(self) -> AdvancedDict:
return AdvancedDict(self.json())


class JmApiResp(JmJsonResp):
Expand Down Expand Up @@ -118,9 +118,9 @@ def res_data(self) -> Any:
return loads(self.decoded_data)

@property
def model_data(self) -> AdvancedEasyAccessDict:
def model_data(self) -> AdvancedDict:
self.require_success()
return AdvancedEasyAccessDict(self.res_data)
return AdvancedDict(self.res_data)


# album-comment
Expand Down
2 changes: 2 additions & 0 deletions src/jmcomic/jm_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def __init__(self, msg: str, context: dict):
def from_context(self, key):
return self.context[key]

def __str__(self):
return self.msg

class ResponseUnexpectedException(JmcomicException):
description = '响应不符合预期异常'
Expand Down
7 changes: 4 additions & 3 deletions src/jmcomic/jm_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ def __init__(self,
# 路径规则配置
self.dir_rule = DirRule(**dir_rule)
# 客户端配置
self.client = AdvancedEasyAccessDict(client)
self.client = AdvancedDict(client)
# 下载配置
self.download = AdvancedEasyAccessDict(download)
self.download = AdvancedDict(download)
# 插件配置
self.plugins = AdvancedEasyAccessDict(plugins)
self.plugins = AdvancedDict(plugins)
# 其他配置
self.filepath = filepath

Expand Down Expand Up @@ -376,6 +376,7 @@ def deconstruct(self) -> Dict:
@classmethod
def from_file(cls, filepath: str) -> 'JmOption':
dic: dict = PackerUtil.unpack(filepath)[0]
dic.setdefault('filepath', filepath)
return cls.construct(dic)

def to_file(self, filepath=None):
Expand Down
60 changes: 60 additions & 0 deletions src/jmcomic/jm_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,63 @@ def build(cls, option: JmOption) -> 'JmOptionPlugin':
instance = JmServerPlugin(option)
setattr(cls, field_name, instance)
return instance


class SubscribeAlbumUpdatePlugin(JmOptionPlugin):
plugin_key = 'subscribe_album_update'

def invoke(self,
album_photo_dict=None,
email_notify=None,
download_if_has_update=True,
auto_update_after_download=True,
) -> None:
if album_photo_dict is None:
return

album_photo_dict: Dict
for album_id, photo_id in album_photo_dict.copy().items():
# check update
try:
has_update, photo_new_list = self.check_photo_update(album_id, photo_id)
except JmcomicException as e:
self.log('Exception happened: ' + str(e), 'check_update.error')
continue

if has_update is False:
continue

self.log(f'album={album_id},发现新章节: {photo_new_list},准备开始下载')

# send email
try:
if email_notify:
SendQQEmailPlugin.build(self.option).invoke(**email_notify)
except PluginValidationException:
# ignore
pass

# download new photo
if has_update and download_if_has_update:
self.option.download_photo(photo_new_list)

if auto_update_after_download:
album_photo_dict[album_id] = photo_new_list[-1]
self.option.to_file()

def check_photo_update(self, album_id: str, photo_id: str):
client = self.option.new_jm_client()
album = client.get_album_detail(album_id)

photo_new_list = []
is_new_photo = False
sentinel = int(photo_id)

for photo in album:
if is_new_photo:
photo_new_list.append(photo.photo_id)

if int(photo.photo_id) == sentinel:
is_new_photo = True

return len(photo_new_list) != 0, photo_new_list
10 changes: 5 additions & 5 deletions src/jmcomic/jm_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def parse_html_to_favorite_page(cls, html: str) -> JmFavoritePage:
return JmFavoritePage(content, folder_list, total)

@classmethod
def parse_api_to_search_page(cls, data: AdvancedEasyAccessDict) -> JmSearchPage:
def parse_api_to_search_page(cls, data: AdvancedDict) -> JmSearchPage:
"""
model_data: {
"search_query": "MANA",
Expand Down Expand Up @@ -501,7 +501,7 @@ def parse_api_to_search_page(cls, data: AdvancedEasyAccessDict) -> JmSearchPage:
return JmSearchPage(content, total)

@classmethod
def parse_api_to_favorite_page(cls, data: AdvancedEasyAccessDict) -> JmFavoritePage:
def parse_api_to_favorite_page(cls, data: AdvancedDict) -> JmFavoritePage:
"""
{
"list": [
Expand Down Expand Up @@ -546,7 +546,7 @@ def parse_api_to_favorite_page(cls, data: AdvancedEasyAccessDict) -> JmFavoriteP

@classmethod
def adapt_content(cls, content):
def adapt_item(item: AdvancedEasyAccessDict):
def adapt_item(item: AdvancedDict):
item: dict = item.src_dict
item.setdefault('tags', [])
return item
Expand Down Expand Up @@ -673,7 +673,7 @@ def post_adapt_album(cls, data: dict, _clazz: type, fields: dict):
series = data['series']
episode_list = []
for chapter in series:
chapter = AdvancedEasyAccessDict(chapter)
chapter = AdvancedDict(chapter)
# photo_id, photo_index, photo_title, photo_pub_date
episode_list.append(
(chapter.id, chapter.sort, chapter.name, None)
Expand All @@ -688,7 +688,7 @@ def post_adapt_photo(cls, data: dict, _clazz: type, fields: dict):
sort = 1
series: list = data['series'] # series中的sort从1开始
for chapter in series:
chapter = AdvancedEasyAccessDict(chapter)
chapter = AdvancedDict(chapter)
if int(chapter.id) == int(data['id']):
sort = chapter.sort
break
Expand Down

0 comments on commit ee23037

Please sign in to comment.