From f19eba1858328144c3643e4e2f0dbe240a9f1f95 Mon Sep 17 00:00:00 2001 From: SlyAimer <2289782085@qq.com> Date: Wed, 12 Mar 2025 19:39:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(image):=20=E6=96=B0=E5=A2=9E=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E5=9B=BEAPI=E6=94=AF=E6=8C=81=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=9A=8F=E6=9C=BA=E5=9B=BE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 `get_image.py` 中新增 `get_anosu_image` 函数,支持从指定API获取随机图 - 在 `image.py` 中优化随机图命令,支持多图下载、过滤及临时文件清理 --- src/clover_image/get_image.py | 12 +++++++++- src/plugins/image.py | 43 +++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/clover_image/get_image.py b/src/clover_image/get_image.py index bacf496..2bf4445 100644 --- a/src/clover_image/get_image.py +++ b/src/clover_image/get_image.py @@ -3,7 +3,7 @@ import random import requests from src.configs.path_config import image_local_path -from src.configs.api_config import smms_token,smms_image_upload_history,ju_he_token,ju_he_image_list +from src.configs.api_config import smms_token,smms_image_upload_history,ju_he_token,ju_he_image_list,anosu_url """本地图片""" async def get_image_names(): @@ -30,3 +30,13 @@ async def get_juhe_image_url(): params = {"token": ju_he_token,"f": "json","categories": "猫羽雫","page": 1, "size": 400} random_url = random.choice(requests.get(ju_he_image_list, params=params).json().get('docs', [])).get('url') return random_url + +""" +随机图api +""" + +async def get_anosu_image(keyword : str ,is_r18 : int,num : int,proxy : str = "i.pixiv.re"): + url= anosu_url+f"?keyword={keyword}&r18={is_r18}&num={num}&proxy={proxy}" + data = requests.get(url).json() + urls = [item['url'] for item in data] + return urls \ No newline at end of file diff --git a/src/plugins/image.py b/src/plugins/image.py index 0a138bd..a2e5370 100644 --- a/src/plugins/image.py +++ b/src/plugins/image.py @@ -3,41 +3,54 @@ from pathlib import Path from nonebot.rule import to_me from nonebot.plugin import on_command from nonebot.adapters.qq import MessageSegment,MessageEvent -from src.clover_image.get_image import get_image_names +from src.clover_image.get_image import get_image_names,get_anosu_image from src.clover_image.download_image import download_image from src.clover_image.delete_file import delete_file from src.configs.path_config import temp_path -image = on_command("图", rule=to_me(), priority=10) +image = on_command("图", rule=to_me(), priority=10,block=False) @image.handle() async def handle_function(): local_image_path = await get_image_names() await image.finish(MessageSegment.file_image(Path(local_image_path))) + + random_keyword_image = on_command("随机图", rule=to_me(), priority=10, block=False) @random_keyword_image.handle() -async def handle_function(message: MessageEvent) -> None: - value = message.get_plaintext().split(" ") - keyword = "" - if len(value) == 2: - keyword = value[1] - if len(value) > 2: - await random_keyword_image.finish("格式不对捏,请试一下 /随机图+关键字") +async def handle_function(message: MessageEvent): - filename = f"{message.get_user_id()}{random.randint(0, 10000)}.jpg" - image_path = temp_path + filename + values = message.get_plaintext().replace("/随机图", "").split(" ") + keyword,is_r18,num = "",0,1 - await download_image(f"https://image.anosu.top/pixiv/direct?keyword={keyword}", image_path) + for value in values: + if value.isdigit(): + num = int(value) + elif value.lower() == "r18": + is_r18 = 1 + else: + keyword = value + urls = await get_anosu_image(keyword=keyword,is_r18=is_r18,num=num) + file_paths = [] + for url in urls: + filename = f"{message.get_user_id()}{random.randint(0, 10000)}.jpg" + image_path = temp_path + filename + file_paths.append(image_path) + await download_image(url,image_path) try: - await random_keyword_image.send(MessageSegment.file_image(Path(image_path))) + for file_path in file_paths: + await random_keyword_image.send(MessageSegment.file_image(Path(file_path))) except Exception as e: print(e) - await random_keyword_image.finish("图被外星人抢走啦,请重试") + await random_keyword_image.send("图被外星人抢走啦,请重试") + finally: + # 删除所有临时文件 + for file_path in file_paths: + await delete_file(file_path) - await delete_file(image_path) # search_image = on_command("/搜图", rule=to_me(), priority=10, block=True)