From 2f5ae142e8840d87ae8494715bfbbf4bba18389f Mon Sep 17 00:00:00 2001 From: thsrite Date: Tue, 17 Dec 2024 16:09:32 +0800 Subject: [PATCH] fix --- plugins.v2/{BBDown => BbDown}/__init__.py | 67 +++++++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) rename plugins.v2/{BBDown => BbDown}/__init__.py (79%) diff --git a/plugins.v2/BBDown/__init__.py b/plugins.v2/BbDown/__init__.py similarity index 79% rename from plugins.v2/BBDown/__init__.py rename to plugins.v2/BbDown/__init__.py index 4e030c9..5d54398 100644 --- a/plugins.v2/BBDown/__init__.py +++ b/plugins.v2/BbDown/__init__.py @@ -1,4 +1,5 @@ import subprocess +import time from pathlib import Path from typing import List, Tuple, Dict, Any @@ -10,7 +11,7 @@ from app.schemas.types import EventType class BbDown(_PluginBase): # 插件名称 - plugin_name = "BBDown" + plugin_name = "BbDown" # 插件描述 plugin_desc = "交互下载B站视频,调用BBDown。" # 插件图标 @@ -59,7 +60,7 @@ class BbDown(_PluginBase): userid=event.event_data.get("user")) return - bbdown_path = Path(self._bbdown_path) / "BBDown" + bbdown_path = Path(self._bbdown_path) / "BbDown" ffmpeg_path = Path(self._bbdown_path) / "ffmpeg" if not bbdown_path.exists() or not ffmpeg_path.exists(): self.post_message(channel=event.event_data.get("channel"), @@ -72,7 +73,7 @@ class BbDown(_PluginBase): logger.info(f"赋予执行权限:{bbdown_path} {ffmpeg_path}") # 执行命令 - command = f"cd {self._bbdown_path} && ./BBDown {args} {f'--work-dir {self._save_path}' if self._save_path else ''}" + command = f"cd {self._bbdown_path} && ./BbDown {args} {f'--work-dir {self._save_path}' if self._save_path else ''}" logger.info(f"执行命令:{command}") self.post_message(channel=event.event_data.get("channel"), @@ -80,7 +81,14 @@ class BbDown(_PluginBase): text=f"保存路径:{self._save_path}" if self._save_path else None, userid=event.event_data.get("user")) - output = self.__execute_command(command=command) + # output = self.__execute_command(command=command) + # 创建命令执行对象 + executor = CommandExecutor() + executor.set_input_callback(self.bbdown_input) + + # 执行命令 + output = executor.execute_command(command) + logger.info(f"命令输出:{output}") self.post_message(channel=event.event_data.get("channel"), @@ -88,6 +96,12 @@ class BbDown(_PluginBase): text=f"{output[-1]}", userid=event.event_data.get("user")) + # 外部方法提供输入 + @eventmanager.register(EventType.PluginAction) + def bbdown_input(self, event: Event = None): + time.sleep(5) # 模拟等待其他操作 + return "输入的数据" + def __execute_command(self, command: str): """ 执行命令 @@ -256,3 +270,48 @@ class BbDown(_PluginBase): 退出插件 """ pass + + +class CommandExecutor: + def __init__(self): + self._input_callback = None + self._output = [] + + def set_input_callback(self, callback): + """ 设置外部输入回调函数 """ + self._input_callback = callback + + def execute_command(self, command: str): + """ + 执行命令并等待输入 + :param command: 命令 + """ + result = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + output = [] + + while True: + # 读取标准输出 + output_line = result.stdout.readline().decode('utf-8') + error_line = result.stderr.readline().decode('utf-8') + + # 如果命令结束且没有输出,则退出 + if output_line == '' and error_line == '' and result.poll() is not None: + break + + if output_line: + output.append(output_line.strip()) + logger.info(output_line.strip()) # 输出到控制台 + + if error_line: + output.append(error_line.strip()) + logger.info(error_line.strip()) # 输出到控制台 + + # 如果有需要输入的提示,调用回调函数来获取输入 + if '请选择' in output_line: # 假设命令要求输入时,输出中包含‘请输入’字符串 + if self._input_callback: + input_data = self._input_callback() # 调用回调函数获取输入 + result.stdin.write(input_data.encode('utf-8')) + result.stdin.flush() + + return output \ No newline at end of file