feat(image): 新增随机图API支持并优化随机图功能

- 在 `get_image.py` 中新增 `get_anosu_image` 函数,支持从指定API获取随机图
- 在 `image.py` 中优化随机图命令,支持多图下载、过滤及临时文件清理
This commit is contained in:
SlyAimer 2025-03-12 19:39:11 +08:00
parent 207cd33342
commit f19eba1858
2 changed files with 39 additions and 16 deletions

View file

@ -3,7 +3,7 @@ import random
import requests import requests
from src.configs.path_config import image_local_path 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(): 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} 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') random_url = random.choice(requests.get(ju_he_image_list, params=params).json().get('docs', [])).get('url')
return random_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

View file

@ -3,41 +3,54 @@ from pathlib import Path
from nonebot.rule import to_me from nonebot.rule import to_me
from nonebot.plugin import on_command from nonebot.plugin import on_command
from nonebot.adapters.qq import MessageSegment,MessageEvent 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.download_image import download_image
from src.clover_image.delete_file import delete_file from src.clover_image.delete_file import delete_file
from src.configs.path_config import temp_path 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() @image.handle()
async def handle_function(): async def handle_function():
local_image_path = await get_image_names() local_image_path = await get_image_names()
await image.finish(MessageSegment.file_image(Path(local_image_path))) 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 = on_command("随机图", rule=to_me(), priority=10, block=False)
@random_keyword_image.handle() @random_keyword_image.handle()
async def handle_function(message: MessageEvent) -> None: async def handle_function(message: MessageEvent):
value = message.get_plaintext().split(" ")
keyword = ""
if len(value) == 2:
keyword = value[1]
if len(value) > 2:
await random_keyword_image.finish("格式不对捏,请试一下 /随机图+关键字")
filename = f"{message.get_user_id()}{random.randint(0, 10000)}.jpg" values = message.get_plaintext().replace("/随机图", "").split(" ")
image_path = temp_path + filename 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: 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: except Exception as e:
print(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) # search_image = on_command("/搜图", rule=to_me(), priority=10, block=True)