Files
abot/group_video_man/bot_video_man.py
2025-03-10 09:57:53 +08:00

67 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import logging
import tomllib
import os
import requests
from wcferry import WxMsg, Wcf
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
class BotVideoMan:
def __init__(self, wcf: Wcf, gbm: GroupBotManager):
self.LOG = logging.getLogger(__name__)
self.wcf = wcf # 假设 wcf 对象在此类中初始化
self.gbm = gbm # 权限功能
with open("group_video_man/config.toml", "rb") as f:
plugin_config = tomllib.load(f)
config = plugin_config["VideoMan"]
self.enable = config["enable"]
self.command = config["command"]
self.LOG.info(f"[猛男视频] 组件初始化完成,指令: {self.command}")
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 self.gbm.get_group_permission(message.roomid, Feature.VIDEO) == PermissionStatus.DISABLED:
return
try:
file_abspath = self.download_video("https://api.guiguiya.com/api/video/fuji?type=json",
"group_video_man/down_load_dir")
self.wcf.send_file(file_abspath, (message.roomid if message.from_group() else message.sender))
return
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
def download_video(self, api_url, save_path):
response = requests.get(api_url)
if response.status_code == 200:
data = response.json()
video_url = data.get("url")
if video_url:
video_response = requests.get(video_url, stream=True)
if video_response.status_code == 200:
with open(save_path, "wb") as file:
for chunk in video_response.iter_content(chunk_size=1024):
file.write(chunk)
return os.path.abspath(save_path)
else:
print("无法下载视频HTTP 状态码:", video_response.status_code)
else:
print("API 响应中没有找到视频 URL")
else:
print("API 请求失败HTTP 状态码:", response.status_code)
return None