diff --git a/app/chain/system.py b/app/chain/system.py index 8930d4c3..2191f570 100644 --- a/app/chain/system.py +++ b/app/chain/system.py @@ -41,7 +41,7 @@ class SystemChain(ChainBase): }, self._restart_file) SystemUtils.restart() - def update(self, channel: MessageChannel, userid: Union[int, str]): + def update(self, channel: MessageChannel = None, userid: Union[int, str] = None): """ 重启系统 """ @@ -56,16 +56,17 @@ class SystemChain(ChainBase): # 重启系统 os.system("bash /usr/local/bin/mp_update") - self.post_message(Notification(channel=channel, - title="暂无新版本!", userid=userid)) - self.remove_cache(self._update_file) + if channel and userid: + self.post_message(Notification(channel=channel, + title="暂无新版本!", userid=userid)) + self.remove_cache(self._update_file) def version(self, channel: MessageChannel, userid: Union[int, str]): """ 查看当前版本、远程版本 """ release_version = self.__get_release_version() - local_version = self.__get_local_version() + local_version = self.get_local_version() if release_version == local_version: title = f"当前版本:{local_version},已是最新版本" else: @@ -110,7 +111,7 @@ class SystemChain(ChainBase): if channel and userid: # 版本号 release_version = self.__get_release_version() - local_version = self.__get_local_version() + local_version = self.get_local_version() if release_version == local_version: title = f"当前版本:{local_version}" else: @@ -135,7 +136,7 @@ class SystemChain(ChainBase): return None @staticmethod - def __get_local_version(): + def get_local_version(): """ 查看当前版本 """ diff --git a/app/plugins/invitessignin/__init__.py b/app/plugins/invitessignin/__init__.py index f8bd39fd..ac6f9511 100644 --- a/app/plugins/invitessignin/__init__.py +++ b/app/plugins/invitessignin/__init__.py @@ -30,7 +30,7 @@ class InvitesSignin(_PluginBase): # 作者主页 author_url = "https://github.com/thsrite" # 插件配置项ID前缀 - plugin_config_prefix = "invitessignin" + plugin_config_prefix = "invitessignin_" # 加载顺序 plugin_order = 24 # 可使用的用户级别 diff --git a/app/plugins/moviepilotupdatenotify/__init__.py b/app/plugins/moviepilotupdatenotify/__init__.py new file mode 100644 index 00000000..214ab7ff --- /dev/null +++ b/app/plugins/moviepilotupdatenotify/__init__.py @@ -0,0 +1,213 @@ +from apscheduler.schedulers.background import BackgroundScheduler +from apscheduler.triggers.cron import CronTrigger + +from app.chain.system import SystemChain +from app.core.config import settings +from app.plugins import _PluginBase +from typing import Any, List, Dict, Tuple, Optional +from app.log import logger +from app.schemas import NotificationType +from app.utils.http import RequestUtils + + +class MoviePilotUpdateNotify(_PluginBase): + # 插件名称 + plugin_name = "MoviePilot更新推送" + # 插件描述 + plugin_desc = "MoviePilot推送release更新通知、自动更新。" + # 插件图标 + plugin_icon = "update.png" + # 主题色 + plugin_color = "#4179F4" + # 插件版本 + plugin_version = "1.0" + # 插件作者 + plugin_author = "thsrite" + # 作者主页 + author_url = "https://github.com/thsrite" + # 插件配置项ID前缀 + plugin_config_prefix = "moviepilotupdatenotify_" + # 加载顺序 + plugin_order = 25 + # 可使用的用户级别 + auth_level = 1 + + # 私有属性 + _enabled = False + # 任务执行间隔 + _cron = None + _update = False + _notify = False + + # 定时器 + _scheduler: Optional[BackgroundScheduler] = None + + def init_plugin(self, config: dict = None): + # 停止现有任务 + self.stop_service() + + if config: + self._enabled = config.get("enabled") + self._cron = config.get("cron") + self._update = config.get("update") + self._notify = config.get("notify") + + # 加载模块 + if self._enabled: + # 定时服务 + self._scheduler = BackgroundScheduler(timezone=settings.TZ) + + if self._cron: + try: + self._scheduler.add_job(func=self.__check_update, + trigger=CronTrigger.from_crontab(self._cron), + name="检查MoviePilot更新") + except Exception as err: + logger.error(f"定时任务配置错误:{err}") + + # 启动任务 + if self._scheduler.get_jobs(): + self._scheduler.print_jobs() + self._scheduler.start() + + def __check_update(self): + """ + 检查MoviePilot更新 + """ + release_version, description, update_time = self.__get_release_version() + if not release_version: + logger.error("最新版本获取失败,停止运行") + return + + # 本地版本 + local_version = SystemChain().get_local_version() + if release_version == local_version: + logger.info(f"当前版本:{local_version} 远程版本:{release_version} 停止运行") + return + + # 推送更新消息 + if self._notify: + self.post_message( + mtype=NotificationType.SiteMessage, + title="【MoviePilot更新通知】", + text=f"{release_version} \n" + f"\n" + f"{description} \n" + f"{update_time}") + + # 自动更新 + if self._update: + logger.info("开始执行自动更新…") + SystemChain().update() + + @staticmethod + def __get_release_version(): + """ + 获取最新版本 + """ + version_res = RequestUtils(proxies=settings.PROXY).get_res( + "https://api.github.com/repos/jxxghp/MoviePilot/releases/latest") + if version_res: + ver_json = version_res.json() + version = f"{ver_json['tag_name']}" + description = f"{ver_json['body']}" + update_time = f"{ver_json['published_at']}" + return version, description, update_time + else: + return None, None, None + + def get_state(self) -> bool: + return self._enabled + + @staticmethod + def get_command() -> List[Dict[str, Any]]: + pass + + def get_api(self) -> List[Dict[str, Any]]: + pass + + def get_form(self) -> Tuple[List[dict], Dict[str, Any]]: + """ + 拼装插件配置页面,需要返回两块数据:1、页面配置;2、数据结构 + """ + return [ + { + 'component': 'VForm', + 'content': [ + { + 'component': 'VRow', + 'content': [ + { + 'component': 'VCol', + 'props': { + 'cols': 12, + 'md': 4 + }, + 'content': [ + { + 'component': 'VSwitch', + 'props': { + 'model': 'enabled', + 'label': '启用插件', + } + } + ] + }, + { + 'component': 'VCol', + 'props': { + 'cols': 12, + 'md': 4 + }, + 'content': [ + { + 'component': 'VSwitch', + 'props': { + 'model': 'update', + 'label': '自动更新', + } + } + ] + }, + { + 'component': 'VCol', + 'props': { + 'cols': 12, + 'md': 4 + }, + 'content': [ + { + 'component': 'VSwitch', + 'props': { + 'model': 'onlyonce', + 'label': '立即运行一次', + } + } + ] + } + ] + } + ] + } + ], { + "enabled": False, + "update": False, + "notify": False, + "cron": "0 9 * * *" + } + + def get_page(self) -> List[dict]: + pass + + def stop_service(self): + """ + 退出插件 + """ + try: + if self._scheduler: + self._scheduler.remove_all_jobs() + if self._scheduler.running: + self._scheduler.shutdown() + self._scheduler = None + except Exception as e: + logger.error("退出插件失败:%s" % str(e))