126 lines
5.3 KiB
Python
126 lines
5.3 KiB
Python
from ncatbot.core import BotClient, GroupMessage, PrivateMessage
|
||
from ncatbot.utils import get_log
|
||
from ncatbot.plugin import BasePlugin, CompatibleEnrollment
|
||
from dotenv import load_dotenv
|
||
import requests
|
||
import os
|
||
|
||
# bot = BotClient()
|
||
bot = CompatibleEnrollment
|
||
_log = get_log()
|
||
|
||
|
||
class GetFile(BasePlugin):
|
||
name = "GetFile"
|
||
version = "0.0.1 beta"
|
||
author = "pythagodzilla"
|
||
info = "A plugin to get images and videos"
|
||
|
||
@bot.private_event()
|
||
async def on_receive_images_private_event(self, msg: PrivateMessage):
|
||
|
||
message = msg.raw_message[1: -1]
|
||
|
||
if message.startswith("CQ:image,"):
|
||
divided_message = message.split(',')
|
||
|
||
for item in divided_message:
|
||
if "file=" in item:
|
||
file_name = item.split("=")[1]
|
||
_log.info(f"Name of the image is: {file_name}")
|
||
image_url = await self.api.get_image(file_name)
|
||
_log.info(f"Image's information collected by \"get_image\" is: {image_url}")
|
||
real_url = image_url['data']['url']
|
||
_log.info(f"Image's real url is: {real_url}")
|
||
|
||
response = requests.get(real_url)
|
||
img = response.content
|
||
with open(f"../data/pics/{file_name}", 'wb') as f:
|
||
f.write(img)
|
||
|
||
@bot.group_event()
|
||
async def on_receive_images_group_event(self, msg: GroupMessage):
|
||
|
||
# if "[CQ:image," in msg.raw_message:
|
||
message = msg.raw_message[1: -1]
|
||
|
||
if message.startswith("CQ:image,"):
|
||
divided_message = message.split(',')
|
||
|
||
for item in divided_message:
|
||
# if "file=" in item:
|
||
if item.startswith("file="):
|
||
file_name = item.split("=")[1]
|
||
_log.info(f"Name of the image is: {file_name}")
|
||
image_url = await self.api.get_image(file_name)
|
||
_log.info(f"Image's information collected by \"get_image\" is: {image_url}")
|
||
real_url = image_url['data']['url']
|
||
_log.info(f"Image's real url is: {real_url}")
|
||
|
||
response = requests.get(real_url)
|
||
img = response.content
|
||
with open(f"../data/pics/{file_name}", 'wb') as f:
|
||
f.write(img)
|
||
|
||
@bot.group_event()
|
||
async def on_receive_videos_group_event(self, msg: GroupMessage):
|
||
|
||
message = msg.raw_message[1: -1]
|
||
|
||
if message.startswith("CQ:video,"):
|
||
divided_message = message.split(',')
|
||
for item in divided_message:
|
||
if item.startswith("file="):
|
||
file_name = item.split("=")[1]
|
||
_log.info(f"Name of the video is: {file_name}")
|
||
video_url = await self.api.get_file(file_name)
|
||
_log.info(f"Video's information collected by \"get_video\" is: {video_url}")
|
||
real_url = video_url['data']['url']
|
||
_log.info(f"Video's real url is: {real_url}")
|
||
|
||
response = requests.get(real_url)
|
||
video = response.content
|
||
with open(f"../data/videos/{file_name}", 'wb') as f:
|
||
f.write(video)
|
||
|
||
@bot.private_event()
|
||
async def on_receive_videos_private_event(self, msg: PrivateMessage):
|
||
|
||
message = msg.raw_message[1: -1]
|
||
|
||
if message.startswith("CQ:video,"):
|
||
divided_message = message.split(',')
|
||
for item in divided_message:
|
||
if item.startswith("file="):
|
||
file_name = item.split("=")[1]
|
||
_log.info(f"Name of the video is: {file_name}")
|
||
video_url = await self.api.get_file(file_name)
|
||
_log.info(f"Video's information collected by \"get_video\" is: {video_url}")
|
||
real_url = video_url['data']['url']
|
||
_log.info(f"Video's real url is: {real_url}")
|
||
|
||
response = requests.get(real_url)
|
||
video = response.content
|
||
with open(f"../data/videos/{file_name}", 'wb') as f:
|
||
f.write(video)
|
||
|
||
|
||
"""
|
||
当单独运行这个程序时,会连接napcat并保存图片和视频到本地。
|
||
|
||
"""
|
||
|
||
if __name__ == "__main__":
|
||
load_dotenv(dotenv_path=r"../../../.env")
|
||
bot = BotClient()
|
||
api = bot.run(
|
||
bt_uin=os.getenv("BOT_ACCOUNT"),
|
||
root=os.getenv("ROOT_ACCOUNT"),
|
||
ws_uri=os.getenv("WS_URI"),
|
||
ws_token=os.getenv("WS_TOKEN"),
|
||
webui_uri=os.getenv("WEBUI_URI"),
|
||
webui_token=os.getenv("WEBUI_TOKEN")
|
||
)
|
||
|
||
# [2025-07-25 17:07:40,602] INFO [MainThread|MainProcess] Logger (get_image.py:on_receive_images_group_event:40) | The message's raw message is: [CQ:video,file=3d13c5917a796a92d91d8f9de44b0bf4.mp4,url=https://multimedia.nt.qq.com.cn:443/download?appid=1415&format=origin&orgfmt=t265&spec=0&client_proto=ntv2&client_appid=537298509&client_type=linux&client_ver=3.2.18-36580&client_down_type=auto&client_aio_type=aio&rkey=CAMSoAHGtzhaJAf05S58aBorSh34zdFk2POE-DFhRWajbvwClbIFzBwF7L6O3xQ7vGtHcK3uh3lqvl9-WhxzqeO_EtfFrx6Em_sJMdepoSJKwoBLIZHPA84yP0MoG10RNQylp1U4ZWQFTCG6h6dFARX4rHyJkvpAi1Zit8neB9En2OELgwcSzyUnVqTWluv8OxiQpEW3QmeAhR2O0SSO0v40V_Yi,file_size=2130820]
|