From 27e97214bde78b230730405901efe69f80a69080 Mon Sep 17 00:00:00 2001 From: Cinte Date: Mon, 22 Aug 2022 09:49:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=85=E4=BB=85=E6=98=AF=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=9B=BE=E7=89=87hash=E5=B0=B1=E5=B0=86=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=88=B0=E7=A3=81=E7=9B=98=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E6=9C=89=E4=BA=9B=E6=B5=AA=E8=B4=B9io=EF=BC=8C=E4=BA=8E?= =?UTF-8?q?=E6=98=AF=E6=94=B9=E6=88=90=E4=BA=86=E5=86=85=E5=AD=98=E4=B8=AD?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/word_bank/_rule.py | 15 +++++++++------ plugins/word_bank/message_handle.py | 16 ++++++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/plugins/word_bank/_rule.py b/plugins/word_bank/_rule.py index df0b784e..f55971bd 100644 --- a/plugins/word_bank/_rule.py +++ b/plugins/word_bank/_rule.py @@ -1,9 +1,10 @@ -import random +import imagehash +from PIL import Image +from io import BytesIO +from httpx import TimeoutException from nonebot.adapters.onebot.v11 import MessageEvent -from configs.path_config import TEMP_PATH -from utils.image_utils import get_img_hash from utils.utils import get_message_text, get_message_img, get_message_at from ._model import WordBank from utils.http_utils import AsyncHttpx @@ -13,11 +14,13 @@ async def check(event: MessageEvent) -> bool: text = get_message_text(event.message) img = get_message_img(event.message) at = get_message_at(event.message) - rand = random.randint(1, 100) problem = text if not text and len(img) == 1: - if await AsyncHttpx.download_file(img[0], TEMP_PATH / f"{event.user_id}_{rand}_word_bank_check.jpg"): - problem = str(get_img_hash(TEMP_PATH / f"{event.user_id}_{rand}_word_bank_check.jpg")) + try: + r = await AsyncHttpx.get(img[0]) + problem = str(imagehash.average_hash(Image.open(BytesIO(r.content)))) + except TimeoutException: + pass if at: temp = '' for seg in event.message: diff --git a/plugins/word_bank/message_handle.py b/plugins/word_bank/message_handle.py index af5a7973..da28bf2d 100644 --- a/plugins/word_bank/message_handle.py +++ b/plugins/word_bank/message_handle.py @@ -1,10 +1,12 @@ -import random +import imagehash +from PIL import Image +from io import BytesIO +from httpx import TimeoutException from services import logger -from utils.image_utils import get_img_hash from ._rule import check from ._model import WordBank -from configs.path_config import DATA_PATH, TEMP_PATH +from configs.path_config import DATA_PATH from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent from utils.utils import get_message_img, get_message_text, get_message_at from nonebot import on_message @@ -25,9 +27,11 @@ async def _(event: MessageEvent): at = get_message_at(event.message) problem = None if not text and img and len(img) == 1: - rand = random.randint(1, 10000) - if await AsyncHttpx.download_file(img[0], TEMP_PATH / f"{event.user_id}_{rand}_word_bank.jpg"): - problem = str(get_img_hash(TEMP_PATH / f"{event.user_id}_{rand}_word_bank.jpg")) + try: + r = await AsyncHttpx.get(img[0]) + problem = str(imagehash.average_hash(Image.open(BytesIO(r.content)))) + except TimeoutException: + logger.error(f"下载 {img[0]} 下载超时..") elif at: temp = '' for seg in event.message: