68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
"""搜图 —— 搜索编排(Service 层)。"""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from nonebot import get_driver
|
||
|
|
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent
|
||
|
|
from nonebot.log import logger
|
||
|
|
|
||
|
|
from ..config import CHAIN_REPLY, SAUCENAO_KEY, threshold
|
||
|
|
from .image import get_image_data_ascii, get_image_data_sauce
|
||
|
|
from .state import lmtd
|
||
|
|
|
||
|
|
_driver = get_driver()
|
||
|
|
SUPERUSERS = list(_driver.config.superusers)
|
||
|
|
_NICKNAMES = list(_driver.config.nickname) if _driver.config.nickname else ["竹竹"]
|
||
|
|
NICKNAME = _NICKNAMES[0]
|
||
|
|
|
||
|
|
|
||
|
|
async def chain_reply(bot: Bot, ev: GroupMessageEvent, chain, msg):
|
||
|
|
if not CHAIN_REPLY:
|
||
|
|
return chain
|
||
|
|
data = {
|
||
|
|
"type": "node",
|
||
|
|
"data": {
|
||
|
|
"name": str(NICKNAME) if str(NICKNAME) else "竹竹",
|
||
|
|
"uin": str(ev.self_id),
|
||
|
|
"content": str(msg),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
chain.append(data)
|
||
|
|
return chain
|
||
|
|
|
||
|
|
|
||
|
|
async def picfinder(bot: Bot, ev: GroupMessageEvent, image_data):
|
||
|
|
uid = ev.user_id
|
||
|
|
chain = []
|
||
|
|
result = await get_image_data_sauce(image_data, SAUCENAO_KEY)
|
||
|
|
image_data_report = result[0]
|
||
|
|
simimax = result[1]
|
||
|
|
if "Index #" in image_data_report:
|
||
|
|
su = next(iter(SUPERUSERS))
|
||
|
|
await bot.send_private_msg(user_id=int(su), message="发生index解析错误")
|
||
|
|
await bot.send_private_msg(user_id=int(su), message=image_data)
|
||
|
|
await bot.send_private_msg(user_id=int(su), message=image_data_report)
|
||
|
|
chain = await chain_reply(bot, ev, chain, image_data_report)
|
||
|
|
|
||
|
|
if float(simimax) > float(threshold):
|
||
|
|
lmtd.increase(uid)
|
||
|
|
else:
|
||
|
|
if simimax != 0:
|
||
|
|
chain = await chain_reply(bot, ev, chain, "相似度过低,换用ascii2d检索中…")
|
||
|
|
else:
|
||
|
|
logger.error("SauceNao not found imageInfo")
|
||
|
|
chain = await chain_reply(bot, ev, chain, "SauceNao检索失败,换用ascii2d检索中…")
|
||
|
|
|
||
|
|
image_data_report = await get_image_data_ascii(image_data)
|
||
|
|
if image_data_report[0]:
|
||
|
|
chain = await chain_reply(bot, ev, chain, image_data_report[0])
|
||
|
|
lmtd.increase(uid)
|
||
|
|
if image_data_report[1]:
|
||
|
|
chain = await chain_reply(bot, ev, chain, image_data_report[1])
|
||
|
|
if not (image_data_report[0] or image_data_report[1]):
|
||
|
|
logger.error("ascii2d not found imageInfo")
|
||
|
|
chain = await chain_reply(bot, ev, chain, "ascii2d检索失败…")
|
||
|
|
|
||
|
|
if CHAIN_REPLY:
|
||
|
|
await bot.send_group_forward_msg(group_id=ev.group_id, messages=chain)
|