PagerMaid_Plugins_Pyro/zpr/main.py
omg-xtao d301f5e370
All checks were successful
Github commit to telegram / build (push) Successful in 49s
zpr 修复
2024-07-29 12:44:05 +08:00

86 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import contextlib
import shutil
from pagermaid.listener import listener
from pagermaid.enums import Client, Message, AsyncClient
from pyrogram.types import InputMediaPhoto
from pyrogram.errors import RPCError
from pathlib import Path
pixiv_img_host = "i.pixiv.cat"
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"
}
data_path = Path("data/zpr")
async def get_result(message, request, r18=0):
# r18: 0为非 R181为 R182为混合在库中的分类不等同于作品本身的 R18 标识)
# num: 图片的数量
# size: 返回图片的尺寸质量
data_path.mkdir(parents=True, exist_ok=True)
size = "regular"
des = "出错了,没有纸片人看了。"
data = await request.get(
f"https://api.lolicon.app/setu/v2?num=1&r18={r18}&size={size}",
headers=headers,
timeout=10,
)
if data.status_code != 200:
return None, "连接二次元大门出错。。。"
await message.edit("已进入二次元 . . .")
try:
result = data.json()["data"]
except Exception:
return None, "解析JSON出错。"
setu_list = [] # 发送
await message.edit("努力获取中 。。。")
for i in range(1):
urls = result[i]["urls"][size].replace("i.pixiv.re", pixiv_img_host)
pid = result[i]["pid"]
title = result[i]["title"]
img_name = f"{result[i]['pid']}_{i}.jpg"
file_path = data_path / img_name
try:
img = await request.get(urls, headers=headers, timeout=10)
if img.status_code != 200:
continue
with open(file_path, mode="wb") as f:
f.write(img.content)
except Exception:
return None, None, "连接二次元出错。。。"
spoiler = r18 == 1
setu_list.append(InputMediaPhoto(media=str(file_path), caption=f"[Pixiv] {title} PID:{pid}", has_spoiler=spoiler))
return setu_list, des if setu_list else None
@listener(command="zpr", description="随机获取一组涩涩纸片人。", parameters="{r18}")
async def zpr(client: Client, message: Message, request: AsyncClient):
arguments = message.arguments.upper().strip()
message = await message.edit("正在前往二次元。。。")
try:
photoList, des = await get_result(
message, request, r18=1 if arguments == "R18" else 0
)
if not photoList:
shutil.rmtree("data/zpr")
return await message.edit(des)
with contextlib.suppress(Exception):
await message.edit("传送中。。。")
try:
await client.send_media_group(
message.chat.id,
photoList,
reply_to_message_id=message.reply_to_message_id,
message_thread_id=message.message_thread_id,
)
except RPCError as e:
return await message.edit(
"此群组不允许发送媒体。"
if e.ID == "CHAT_SEND_MEDIA_FORBIDDEN"
else f"发生错误:\n`{e}`"
)
except Exception as e:
return await message.edit(f"发生错误:\n`{e}`")
shutil.rmtree("data/zpr")
await message.safe_delete()