zhenxun_bot/plugins/upload_img/__init__.py

100 lines
4.2 KiB
Python
Raw Normal View History

2021-05-20 19:25:51 +08:00
from nonebot import on_command
from configs.path_config import IMAGE_PATH
from services.log import logger
import os
from nonebot.rule import to_me
from nonebot.typing import T_State
2021-07-30 21:21:51 +08:00
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
2021-06-30 19:50:55 +08:00
from utils.utils import get_message_imgs, get_message_text
2021-05-20 19:25:51 +08:00
import aiohttp
import aiofiles
2021-06-30 19:50:55 +08:00
from utils.utils import cn2py
2021-05-20 19:25:51 +08:00
from configs.config import IMAGE_DIR_LIST
2021-07-30 21:21:51 +08:00
__plugin_name__ = "上传图片"
__plugin_usage__ = (
"上传图片帮助:\n\t"
"1.查看列表 --> 指令: 上传图片 列表/目录\n\t"
"2.上传图片 [序号] [图片], 即在相应目录下添加图片\n\t\t示例: 上传图片 1 [图片]"
)
2021-05-20 19:25:51 +08:00
upload_img = on_command("上传图片", rule=to_me(), priority=5, block=True)
@upload_img.args_parser
2021-07-30 21:21:51 +08:00
async def parse(bot: Bot, event: MessageEvent, state: T_State):
if str(event.get_message()) in ["取消", "算了"]:
2021-05-20 19:25:51 +08:00
await upload_img.finish("已取消操作..", at_sender=True)
2021-07-30 21:21:51 +08:00
if state["_current_key"] in ["path"]:
2021-05-20 19:25:51 +08:00
if str(event.get_message()) not in IMAGE_DIR_LIST:
await upload_img.reject("此目录不正确,请重新输入目录!")
state[state["_current_key"]] = str(event.get_message())
2021-07-30 21:21:51 +08:00
if state["_current_key"] in ["imgs"]:
2021-05-20 19:25:51 +08:00
if not get_message_imgs(event.json()):
await upload_img.reject("图呢图呢图呢图呢GKD")
2021-07-30 21:21:51 +08:00
state[state["_current_key"]] = get_message_imgs(event.json())
2021-05-20 19:25:51 +08:00
@upload_img.handle()
2021-07-30 21:21:51 +08:00
async def _(bot: Bot, event: MessageEvent, state: T_State):
2021-05-20 19:25:51 +08:00
raw_arg = get_message_text(event.json())
img_list = get_message_imgs(event.json())
if raw_arg:
2021-07-30 21:21:51 +08:00
if str(event.get_message()) in ["帮助"]:
2021-05-20 19:25:51 +08:00
await upload_img.finish(__plugin_usage__)
if raw_arg.split("[")[0] in IMAGE_DIR_LIST:
2021-07-30 21:21:51 +08:00
state["path"] = raw_arg.split("[")[0]
2021-05-20 19:25:51 +08:00
if img_list:
2021-07-30 21:21:51 +08:00
state["imgs"] = img_list
2021-05-20 19:25:51 +08:00
@upload_img.got("path", prompt="要将图片上传至什么图库呢?")
@upload_img.got("imgs", prompt="图呢图呢图呢图呢GKD")
2021-07-30 21:21:51 +08:00
async def _(bot: Bot, event: MessageEvent, state: T_State):
path = IMAGE_PATH + cn2py(state["path"])
img_list = state["imgs"]
2021-05-20 19:25:51 +08:00
img_id = len(os.listdir(path))
failed_list = []
success_id = ""
async with aiohttp.ClientSession() as session:
for img_url in img_list:
try:
async with session.get(img_url, timeout=7) as response:
if response.status == 200:
2021-07-30 21:21:51 +08:00
async with aiofiles.open(
path + str(img_id) + ".jpg", "wb"
) as f:
2021-05-20 19:25:51 +08:00
await f.write(await response.read())
success_id += str(img_id) + ""
img_id += 1
else:
failed_list.append(img_url)
logger.warning(f"图片:{img_url} 下载失败....")
except TimeoutError as e:
logger.warning(f"图片:{img_url} 下载超时....e:{e}")
if img_url not in failed_list:
failed_list.append(img_url)
failed_result = ""
for img in failed_list:
failed_result += str(img) + "\n"
2021-07-30 21:21:51 +08:00
logger.info(
f"USER {event.user_id} GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'}"
f" 上传图片至 {state['path']}{len(img_list)} 张,失败 {len(failed_list)}id={success_id[:-1]}"
)
2021-05-20 19:25:51 +08:00
if failed_result:
2021-07-30 21:21:51 +08:00
await upload_img.finish(
f"这次一共为 {state['path']}库 添加了 {len(img_list) - len(failed_list)} 张图片\n"
f"依次的Id为{success_id[:-1]}\n"
f"上传失败:{failed_result[:-1]}\n"
f"小真寻感谢您对图库的扩充!WW",
at_sender=True,
)
2021-05-20 19:25:51 +08:00
else:
2021-07-30 21:21:51 +08:00
await upload_img.finish(
f"这次一共为 {state['path']}库 添加了 {len(img_list)} 张图片\n"
f"依次的Id为{success_id[:-1]}\n"
f"小真寻感谢您对图库的扩充!WW",
at_sender=True,
)