From 3fd126f5cae97e3cb214bdcff8a0f81ffac3b2bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=93=9C=E7=93=9C=E5=90=96?= <31442507+guaguaya66@users.noreply.github.com> Date: Mon, 17 Oct 2022 10:37:24 +0800 Subject: [PATCH] =?UTF-8?q?zpr=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9BBUG?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E5=88=A0=E9=99=A4=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E6=96=B9=E6=B3=95=E5=92=8C=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=B5=81=E7=A8=8B=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修了一些bug 修复消息被删除后提示出错导致无法发送图片的情况 代码变更 优化错误处理流程 通过直接删除zpr图片文件夹达到清理图片文件的效果 `rmtree` --- zpr/main.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/zpr/main.py b/zpr/main.py index e9bb475..60c07e3 100644 --- a/zpr/main.py +++ b/zpr/main.py @@ -1,13 +1,19 @@ +import contextlib +import shutil +from os import makedirs +from os.path import exists, dirname, abspath from pagermaid.listener import listener from pagermaid.single_utils import safe_remove from pagermaid.enums import Client, Message, AsyncClient from pyrogram.types import InputMediaPhoto - async def get_result(message, request, r18=0): # r18: 0为非 R18,1为 R18,2为混合(在库中的分类,不等同于作品本身的 R18 标识) # num: 图片的数量 # size: 返回图片的尺寸质量 + zpr_path = f"{dirname(abspath(__file__))}/zpr/" + if not exists(zpr_path): + makedirs(zpr_path) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.42' } @@ -22,21 +28,18 @@ async def get_result(message, request, r18=0): except Exception: return None, None, "解析JSON出错。" setuList = [] # 发送 - delList = [] # 删除 await message.edit("努力获取中 。。。") for i in range(5): urls = result[i]['urls'][size].replace('i.pixiv.re', 'img.misaka.gay') imgname = (f"{result[i]['pid']}_{i}.jpg") try: img = await request.get(urls, headers=headers, timeout=10) - with open(imgname, mode="wb") as f: + with open(f"{zpr_path}{imgname}", mode="wb") as f: f.write(img.content) except Exception: return None, None, "连接二次元出错。。。" - setuList.append(InputMediaPhoto(imgname)) - delList.append(imgname) - return setuList, delList, des if setuList else None - + setuList.append(InputMediaPhoto(f"{zpr_path}{imgname}")) + return setuList, zpr_path, des if setuList else None @listener(command="zpr", description="随机获取一组涩涩纸片人。", @@ -45,20 +48,14 @@ async def zpr(client: Client, message: Message, request: AsyncClient): arguments = message.arguments.upper().strip() message = await message.edit("正在前往二次元。。。") try: - photoList, delList, des = await get_result(message, request, r18=1 if arguments == "R18" else 0) - except Exception as e: - return await message.edit(f"{des}\n\n错误信息:\n`{e}`") - if not photoList: - return await message.edit(des) - try: - await message.edit("传送中。。。") + photoList, zpr_path, des = await get_result(message, request, r18=1 if arguments == "R18" else 0) + if not photoList: + shutil.rmtree(zpr_path) + return await message.edit(des) + with contextlib.suppress(Exception): + message = await message.edit("传送中。。。") await client.send_media_group(message.chat.id, photoList) except Exception as e: await client.send_message(message.chat.id, f"{des}\n\n错误信息:\n`{e}`") - try: - for i in range(5): - if delList[i]: - safe_remove(delList[i]) - except: - pass + shutil.rmtree(zpr_path) await message.safe_delete()