From 20debbadcff5193b5996a82d6e5a21115e1b9d06 Mon Sep 17 00:00:00 2001 From: thsrite Date: Sun, 9 Jun 2024 12:56:30 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=94=AF=E6=8C=81=E4=BA=A4=E4=BA=92?= =?UTF-8?q?=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- package.json | 6 ++- plugins/commandexecute/__init__.py | 83 ++++++++++++++++++++++-------- plugins/sqlexecute/__init__.py | 61 ++++++++++++++++++++-- 4 files changed, 126 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index c6c84bc..595c921 100644 --- a/README.md +++ b/README.md @@ -40,5 +40,5 @@ MoviePilot三方插件市场:https://github.com/thsrite/MoviePilot-Plugins/ - 热门媒体订阅 v1.7 - [HomePage v1.2](docs%2FHomePage.md) - 目录监控(统一入库消息增强版) v1.0 -- Sql执行器 v1.0 -- 命令执行器 v1.0 \ No newline at end of file +- Sql执行器 v1.1 +- 命令执行器 v1.1 \ No newline at end of file diff --git a/package.json b/package.json index 888f5da..b835347 100644 --- a/package.json +++ b/package.json @@ -484,11 +484,12 @@ "name": "Sql执行器", "description": "自定义MoviePilot数据库Sql执行。", "labels": "工具", - "version": "1.0", + "version": "1.1", "icon": "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/sqlite.png", "author": "thsrite", "level": 1, "history": { + "v1.1": "支持交互命令/cmd [command]执行,需主程序1.9.4+", "v1.0": "自定义MoviePilot数据库Sql执行" } }, @@ -496,11 +497,12 @@ "name": "命令执行器", "description": "自定义容器命令执行。", "labels": "工具", - "version": "1.0", + "version": "1.1", "icon": "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/command.png", "author": "thsrite", "level": 1, "history": { + "v1.1": "支持交互命令/sql [sql]执行,需主程序1.9.4+", "v1.0": "自定义容器命令执行" } } diff --git a/plugins/commandexecute/__init__.py b/plugins/commandexecute/__init__.py index c693c2d..b848f9b 100644 --- a/plugins/commandexecute/__init__.py +++ b/plugins/commandexecute/__init__.py @@ -1,8 +1,10 @@ import subprocess +from app.core.event import eventmanager, Event from app.plugins import _PluginBase from typing import Any, List, Dict, Tuple from app.log import logger +from app.schemas.types import EventType class CommandExecute(_PluginBase): @@ -13,7 +15,7 @@ class CommandExecute(_PluginBase): # 插件图标 plugin_icon = "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/command.png" # 插件版本 - plugin_version = "1.0" + plugin_version = "1.1" # 插件作者 plugin_author = "thsrite" # 作者主页 @@ -39,24 +41,8 @@ class CommandExecute(_PluginBase): try: for command in self._command.split("\n"): logger.info(f"开始执行命令 {command}") - result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - last_output = None - last_error = None - while True: - error = result.stderr.readline().decode("utf-8") - if error == '' and result.poll() is not None: - break - if error: - logger.info(error.strip()) - last_error = error.strip() - while True: - output = result.stdout.readline().decode("utf-8") - if output == '' and result.poll() is not None: - break - if output: - logger.info(output.strip()) - last_output = output.strip() - print(last_output if last_output else last_error) + last_output, last_error = self.execute_command(command) + logger.info(last_output if last_output else last_error) except Exception as e: logger.error(f"命令执行失败 {str(e)}") return @@ -67,12 +53,67 @@ class CommandExecute(_PluginBase): "command": self._command }) + @staticmethod + def execute_command(command: str): + """ + 执行命令 + :param command: 命令 + """ + result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + last_output = None + last_error = None + while True: + error = result.stderr.readline().decode("utf-8") + if error == '' and result.poll() is not None: + break + if error: + logger.info(error.strip()) + last_error = error.strip() + while True: + output = result.stdout.readline().decode("utf-8") + if output == '' and result.poll() is not None: + break + if output: + logger.info(output.strip()) + last_output = output.strip() + + return last_output, last_error + + @eventmanager.register(EventType.PluginAction) + def execute(self, event: Event = None): + if event: + event_data = event.event_data + if not event_data or event_data.get("action") != "command_execute": + return + args = event_data.get("args") + if not args: + return + + logger.info(f"收到命令,开始执行命令 ...{args}") + last_output, last_error = self.execute_command(args) + self.post_message(channel=event.event_data.get("channel"), + title="命令执行结果", + text=last_output if last_output else last_error, + userid=event.event_data.get("user")) + def get_state(self) -> bool: - return False + return True @staticmethod def get_command() -> List[Dict[str, Any]]: - pass + """ + 定义远程控制命令 + :return: 命令关键字、事件、描述、附带数据 + """ + return [{ + "cmd": "/cmd", + "event": EventType.PluginAction, + "desc": "自定义命令执行", + "category": "", + "data": { + "action": "command_execute" + } + }] def get_api(self) -> List[Dict[str, Any]]: pass diff --git a/plugins/sqlexecute/__init__.py b/plugins/sqlexecute/__init__.py index 5a9270f..cdcc550 100644 --- a/plugins/sqlexecute/__init__.py +++ b/plugins/sqlexecute/__init__.py @@ -1,8 +1,10 @@ import sqlite3 +from app.core.event import eventmanager, Event from app.plugins import _PluginBase from typing import Any, List, Dict, Tuple from app.log import logger +from app.schemas.types import EventType class SqlExecute(_PluginBase): @@ -13,7 +15,7 @@ class SqlExecute(_PluginBase): # 插件图标 plugin_icon = "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/sqlite.png" # 插件版本 - plugin_version = "1.0" + plugin_version = "1.1" # 插件作者 plugin_author = "thsrite" # 作者主页 @@ -66,12 +68,65 @@ class SqlExecute(_PluginBase): "sql": self._sql }) + @eventmanager.register(EventType.PluginAction) + def execute(self, event: Event = None): + if event: + event_data = event.event_data + if not event_data or event_data.get("action") != "sql_execute": + return + args = event_data.get("args") + if not args: + return + + logger.info(f"收到命令,开始执行SQL ...{args}") + + # 读取sqlite数据 + try: + gradedb = sqlite3.connect("/config/user.db") + except Exception as e: + logger.error(f"数据库链接失败 {str(e)}") + return + + # 创建游标cursor来执行executeSQL语句 + cursor = gradedb.cursor() + + # 执行SQL语句 + try: + for sql in self._sql.split("\n"): + logger.info(f"开始执行SQL语句 {sql}") + # 执行SQL语句 + cursor.execute(sql) + + logger.info(cursor.fetchall()) + self.post_message(channel=event.event_data.get("channel"), + title="SQL执行结果", + text='\n'.join(cursor.fetchall()), + userid=event.event_data.get("user")) + except Exception as e: + logger.error(f"SQL语句执行失败 {str(e)}") + return + finally: + # 关闭游标 + cursor.close() + def get_state(self) -> bool: - return False + return True @staticmethod def get_command() -> List[Dict[str, Any]]: - pass + """ + 定义远程控制命令 + :return: 命令关键字、事件、描述、附带数据 + """ + return [{ + "cmd": "/sql", + "event": EventType.PluginAction, + "desc": "自定义sql执行", + "category": "", + "data": { + "action": "sql_execute" + } + }] def get_api(self) -> List[Dict[str, Any]]: pass