diff --git a/README.md b/README.md index 071710a..eb66e05 100644 --- a/README.md +++ b/README.md @@ -29,5 +29,5 @@ MoviePilot三方插件市场:https://github.com/thsrite/MoviePilot-Plugins/ - [源文件恢复 1.2](docs%2FLinkToSrc.md) - [微信消息转发 1.3](docs%2FWeChatForward.md) - [订阅下载统计 1.5](docs%2FSubscribeStatistic.md) -- [自定义命令 1.0](docs%2FCustomCommand.md) +- [自定义命令 1.1](docs%2FCustomCommand.md) diff --git a/docs/CustomCommand.md b/docs/CustomCommand.md index c530db6..b361d2e 100644 --- a/docs/CustomCommand.md +++ b/docs/CustomCommand.md @@ -2,9 +2,11 @@ ### 更新记录 +- 1.1 试试打印命令日志 - 1.0 自定义执行周期执行命令并推送结果 +默认把python脚本最后一个print作为返回值 命令名#0 9 * * *#python main.py 命令名#0 9 * * *#python main.py#1-600 diff --git a/package.json b/package.json index 22c34ce..82bf2ab 100644 --- a/package.json +++ b/package.json @@ -154,7 +154,7 @@ "CustomCommand": { "name": "自定义命令", "description": "自定义执行周期执行命令并推送结果。", - "version": "1.0", + "version": "1.1", "icon": "Ntfy_A.png", "author": "thsrite", "level": 1 diff --git a/plugins/customcommand/__init__.py b/plugins/customcommand/__init__.py index 136a061..940285e 100644 --- a/plugins/customcommand/__init__.py +++ b/plugins/customcommand/__init__.py @@ -22,7 +22,7 @@ class CustomCommand(_PluginBase): # 插件图标 plugin_icon = "Ntfy_A.png" # 插件版本 - plugin_version = "1.0" + plugin_version = "1.1" # 插件作者 plugin_author = "thsrite" # 作者主页 @@ -108,10 +108,26 @@ class CustomCommand(_PluginBase): logger.info(f"随机延时 {random_delay} 秒") time.sleep(random_delay) - result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output, errors = result.communicate() + result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1) + last_output = None + last_error = None + while True: + output = result.stdout.readline().decode("utf-8") + if output == '' and result.poll() is not None: + break + if output: + print(output.strip()) + last_output = output.strip() + + error = result.stderr.readline().decode("utf-8") + if error == '' and result.poll() is not None: + break + if error: + print(error.strip()) + last_error = error.strip() + logger.info( - f"执行命令:{command} 返回值:{errors.decode('utf-8') if errors else output.decode('utf-8')}") + f"执行命令:{command} {'成功' if result.returncode == 0 else '失败'} 返回值:{last_output if last_output else last_error}") if self._notify and self._msgtype: # 发送通知 @@ -121,8 +137,7 @@ class CustomCommand(_PluginBase): self.post_message(title=name, mtype=mtype, - text="执行失败" if not errors and not output else errors.decode( - 'utf-8') if errors else output.decode('utf-8')) + text=last_output if last_output else last_error) def __update_config(self): self.update_config({