From accee358d31cdebfb3d604d39a5886ad46a747b6 Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 5 Mar 2025 16:32:29 +0800 Subject: [PATCH] =?UTF-8?q?feature=EF=BC=9A=E5=8A=A0=E5=85=A5=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E9=AC=BC=E9=AC=BCAPI=20=E9=BB=91=E4=B8=9D=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group_video/bot_video.py | 88 ++++++++++++++++++++++++++++++++++++++ group_video/config.toml | 3 ++ robot.py | 9 ++++ robot_cmd/robot_command.py | 2 + 4 files changed, 102 insertions(+) create mode 100644 group_video/bot_video.py create mode 100644 group_video/config.toml diff --git a/group_video/bot_video.py b/group_video/bot_video.py new file mode 100644 index 0000000..c3b84cd --- /dev/null +++ b/group_video/bot_video.py @@ -0,0 +1,88 @@ +import logging +import tomllib +import os +import requests +from wcferry import WxMsg, Wcf + +from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager + + +class BotVideo: + def __init__(self, wcf: Wcf, gbm: GroupBotManager): + self.LOG = logging.getLogger(__name__) + self.wcf = wcf # 假设 wcf 对象在此类中初始化 + self.gbm = gbm # 权限功能 + with open("group_video/config.toml", "rb") as f: + plugin_config = tomllib.load(f) + + config = plugin_config["Video"] + + self.enable = config["enable"] + self.command = config["command"] + self.LOG.info(f"[点视频] 组件初始化完成,指令: {self.command}") + + def download_stream(self, url, save_path): + """ + 从指定URL读取视频流并保存到本地 + :param url: 视频流的URL + :param save_path: 本地保存路径(包含文件名,例如 "video.mp4") + """ + try: + # 发送GET请求,启用流式传输 + response = requests.get(url, stream=True) + + # 检查请求是否成功 + response.raise_for_status() # 如果状态码不是200,将抛出异常 + + # 确保保存路径的目录存在 + os.makedirs(os.path.dirname(save_path) or ".", exist_ok=True) + + # 检查是否是视频流(可选,根据Content-Type判断) + content_type = response.headers.get("Content-Type", "").lower() + if "video" not in content_type and "application/octet-stream" not in content_type: + print(f"警告: 返回的可能不是视频流,Content-Type: {content_type}") + print("响应内容预览:", response.text[:100]) # 打印前100字符查看 + return + + # 以二进制写入模式保存流数据 + with open(save_path, "wb") as file: + for chunk in response.iter_content(chunk_size=1024): # 分块读取,每块1KB + if chunk: # 过滤空块 + file.write(chunk) + print(f"视频已下载到: {save_path}") + return os.path.abspath(save_path) + except requests.RequestException as e: + print(f"请求失败: {e}") + except IOError as e: + print(f"文件写入失败: {e}") + except Exception as e: + print(f"发生未知错误: {e}") + + def get_video(self, message: WxMsg): + if not self.enable: + return + + content = str(message.content).strip() + command = content.split(" ") + + if command[0] not in self.command: + return + + if len(command) == 1: + self.wcf.send_text(f"-----Bot-----\n❌命令格式错误!{self.command_format}", + (message.roomid if message.from_group() else message.sender), message.sender) + return + + # 如果触发了指令,但是没有权限,则返回权限不足 + if self.gbm.get_group_permission(message.roomid, Feature.MOVIES) == PermissionStatus.DISABLED: + return + try: + file_abspath = self.download_stream("https://api.guiguiya.com/api/hook/heisis", "down_load_dir") + except requests.RequestException as e: + self.wcf.send_text(f"-----Bot-----\n❌请求出错:{e}", + (message.roomid if message.from_group() else message.sender), message.sender) + return + + if file_abspath.endswith("mp4"): + self.wcf.send_file(file_abspath, (message.roomid if message.from_group() else message.sender)) + return diff --git a/group_video/config.toml b/group_video/config.toml new file mode 100644 index 0000000..d1086a0 --- /dev/null +++ b/group_video/config.toml @@ -0,0 +1,3 @@ +[Video] +enable = true +command = ["黑丝视频", "黑丝", "来个黑丝","搞个黑丝"] diff --git a/robot.py b/robot.py index 8fe3e85..a2c8a24 100644 --- a/robot.py +++ b/robot.py @@ -30,6 +30,7 @@ from game_task.game_task_encyclopedia import game_process_message, setup_schedul run_random_task_assignment from group_auto.group_auto_invite import get_first_group_id, process_command from group_auto.group_member_change import GroupMemberChange +from group_video.bot_video import BotVideo from message_sign.main import SignInSystem from message_storage.message_to_db import MessageStorage from music.bot_music import BotMusic @@ -84,6 +85,8 @@ class Robot(Job): self.signin = SignInSystem(wcf, self.gbm, self.allContacts, self.db_pool, self.redis_pool) # 积分赠送功能加载 self.trade = PointTrade(wcf, self.gbm, self.db_pool) + # 获取视频模块 + self.video = BotVideo(wcf, self.gbm) if ChatType.is_in_chat_types(chat_type): if chat_type == ChatType.TIGER_BOT.value and TigerBot.value_check(self.config.TIGERBOT): @@ -344,6 +347,12 @@ class Robot(Job): except Exception as e: self.LOG.error(f"point trade error: {e}") + # 加入积分赠与功能 + try: + self.video.get_video(message=msg) + except Exception as e: + self.LOG.error(f"video get_video error: {e}") + if msg.is_at(self.wxid): # 被@ self.toAt(msg) return # 处理完群聊信息,后面就不需要处理了 diff --git a/robot_cmd/robot_command.py b/robot_cmd/robot_command.py index e8e746e..5da4ca3 100644 --- a/robot_cmd/robot_command.py +++ b/robot_cmd/robot_command.py @@ -34,6 +34,8 @@ class Feature(Enum): MUSIC = 10, "点歌功能" SIGNIN = 11, "签到功能" POINT_TRADE = 12, "积分赠送功能" + MOVIES = 14, "视频点播功能" + def __new__(cls, value, description): obj = object.__new__(cls)