zpr 修复图片反代站,完善错误信息提示 (#94)

Co-authored-by: Sourcery AI <>
Co-authored-by: omg-xtao <100690902+omg-xtao@users.noreply.github.com>
This commit is contained in:
瓜瓜吖 2022-10-15 17:56:12 +08:00 committed by GitHub
parent f7f310b6c9
commit 6b3d50d75f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,9 +8,12 @@ async def get_result(message, request, r18=0):
# r18: 0为非 R181为 R182为混合在库中的分类不等同于作品本身的 R18 标识) # r18: 0为非 R181为 R182为混合在库中的分类不等同于作品本身的 R18 标识)
# num: 图片的数量 # num: 图片的数量
# size: 返回图片的尺寸质量 # size: 返回图片的尺寸质量
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'
}
size = "regular" size = "regular"
des = "出错了,没有纸片人看了。" des = "出错了,没有纸片人看了。"
data = await request.get((f"https://api.lolicon.app/setu/v2?num=5&r18={r18}&size={size}")) data = await request.get((f"https://api.lolicon.app/setu/v2?num=5&r18={r18}&size={size}"), headers=headers, timeout=10)
if data.status_code != 200: if data.status_code != 200:
return None, None, "连接二次元大门出错。。。" return None, None, "连接二次元大门出错。。。"
await message.edit("已进入二次元 . . .") await message.edit("已进入二次元 . . .")
@ -22,10 +25,10 @@ async def get_result(message, request, r18=0):
delList = [] # 删除 delList = [] # 删除
await message.edit("努力获取中 。。。") await message.edit("努力获取中 。。。")
for i in range(5): for i in range(5):
urls = result[i]['urls'][size] urls = result[i]['urls'][size].replace('i.pixiv.re', 'img.misaka.gay')
imgname = (f"{result[i]['pid']}_{i}.png") imgname = (f"{result[i]['pid']}_{i}.jpg")
try: try:
img = await request.get(urls) img = await request.get(urls, headers=headers, timeout=10)
with open(imgname, mode="wb") as f: with open(imgname, mode="wb") as f:
f.write(img.content) f.write(img.content)
except Exception: except Exception:
@ -44,14 +47,18 @@ async def zpr(client: Client, message: Message, request: AsyncClient):
try: try:
photoList, delList, des = await get_result(message, request, r18=1 if arguments == "R18" else 0) photoList, delList, des = await get_result(message, request, r18=1 if arguments == "R18" else 0)
except Exception as e: except Exception as e:
return await message.edit(f"发生错误:{e}") return await message.edit(f"{des}\n\n错误信息\n`{e}`")
if not photoList: if not photoList:
return await message.edit(des) return await message.edit(des)
try: try:
await message.edit("传送中。。。") await message.edit("传送中。。。")
await client.send_media_group(message.chat.id, photoList) 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): for i in range(5):
safe_remove(delList[i]) if delList[i]:
except Exception: safe_remove(delList[i])
await client.send_message(message.chat.id, des) except:
pass
await message.safe_delete() await message.safe_delete()