77 lines
2.9 KiB
Python
77 lines
2.9 KiB
Python
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_MAN) == 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_dir):
|
|
# 确保 save_dir 是一个目录
|
|
if os.path.isfile(save_dir):
|
|
print(f"错误: {save_dir} 是一个文件,不能作为目录使用。")
|
|
return None
|
|
|
|
os.makedirs(save_dir, exist_ok=True)
|
|
save_path = os.path.join(save_dir, "video.mp4")
|
|
|
|
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)
|
|
abs_path = os.path.abspath(save_path)
|
|
print(f"视频已下载至: {abs_path}")
|
|
return abs_path
|
|
else:
|
|
print("无法下载视频,HTTP 状态码:", video_response.status_code)
|
|
else:
|
|
print("API 响应中没有找到视频 URL")
|
|
else:
|
|
print("API 请求失败,HTTP 状态码:", response.status_code)
|
|
return None
|