PagerMaid_Plugins_Pyro/zpr/main.py

103 lines
3.9 KiB
Python
Raw Normal View History

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
2023-03-12 13:40:36 +00:00
from pathlib import Path
2024-07-30 03:09:12 +00:00
# pixiv反代服务器
2024-07-29 04:44:05 +00:00
pixiv_img_host = "i.pixiv.cat"
headers = {
2024-07-30 03:09:12 +00:00
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.2651.74"
}
2023-03-12 13:40:36 +00:00
data_path = Path("data/zpr")
2024-07-30 03:09:12 +00:00
async def get_result(message, request, r18=0, tag="", num=1):
2023-03-12 13:40:36 +00:00
data_path.mkdir(parents=True, exist_ok=True)
des = "出错了,没有纸片人看了。"
2022-11-21 11:38:51 +00:00
data = await request.get(
2024-07-30 03:09:12 +00:00
f"https://api.lolicon.app/setu/v2?num={num}&r18={r18}&tag={tag}&size=regular&size=original&proxy={pixiv_img_host}&excludeAI=true",
2022-11-21 11:38:51 +00:00
headers=headers,
timeout=10,
)
2024-07-30 03:09:12 +00:00
spoiler = r18 == 1
if data.status_code != 200:
2023-03-12 13:40:36 +00:00
return None, "连接二次元大门出错。。。"
await message.edit("已进入二次元 . . .")
try:
2023-07-01 12:18:58 +00:00
result = data.json()["data"]
except Exception:
2023-03-12 13:40:36 +00:00
return None, "解析JSON出错。"
2022-11-21 11:38:51 +00:00
setu_list = [] # 发送
await message.edit("努力获取中 。。。")
2024-07-30 03:09:12 +00:00
for i in range(int(num)):
urls = result[i]["urls"]["regular"]
original = result[i]["urls"]["original"]
2024-07-29 04:44:05 +00:00
pid = result[i]["pid"]
title = result[i]["title"]
2024-07-30 03:09:12 +00:00
width = result[i]["width"]
height = result[i]["height"]
2022-11-21 11:38:51 +00:00
img_name = f"{result[i]['pid']}_{i}.jpg"
2023-03-12 13:42:22 +00:00
file_path = data_path / img_name
try:
img = await request.get(urls, headers=headers, timeout=10)
2023-03-12 13:40:36 +00:00
if img.status_code != 200:
continue
with open(file_path, mode="wb") as f:
f.write(img.content)
except Exception:
return None, None, "连接二次元出错。。。"
2024-07-30 03:09:12 +00:00
setu_list.append(InputMediaPhoto(media=str(file_path), caption=f"**{title}**\nPID:[{pid}](https://www.pixiv.net/artworks/{pid})\n查看原图:[点击查看]({original})\n原图尺寸:{width}x{height}", has_spoiler=spoiler))
2023-03-12 13:40:36 +00:00
return setu_list, des if setu_list else None
2024-07-30 03:09:12 +00:00
@listener(command="zpr", description="随机获取一组涩涩纸片人。", parameters="{tag} {r18} {num}")
async def zpr(client: Client, message: Message, request: AsyncClient):
message = await message.edit("正在前往二次元。。。")
2024-07-30 03:09:12 +00:00
p = message.parameter
n = 1
r = 0
t = ""
try:
2024-07-30 03:09:12 +00:00
if len(p) > 0:
if p[0].isdigit():
n = p[0]
elif p[0] == "r18":
r = 1
if len(p) > 1 and p[1].isdigit():
n = p[1]
else:
t = p[0]
if len(p) > 1:
if p[1].isdigit():
n = p[1]
elif p[1] == "r18":
r = 1
if len(p) > 2 and p[2].isdigit():
n = p[2]
2023-07-01 12:18:58 +00:00
photoList, des = await get_result(
2024-07-30 03:09:12 +00:00
message, request, r18=r, tag=t, num=n
2023-07-01 12:18:58 +00:00
)
if not photoList:
2023-03-12 13:40:36 +00:00
shutil.rmtree("data/zpr")
return await message.edit(des)
with contextlib.suppress(Exception):
await message.edit("传送中。。。")
try:
2023-07-01 12:18:58 +00:00
await client.send_media_group(
2024-02-04 07:56:06 +00:00
message.chat.id,
photoList,
reply_to_message_id=message.reply_to_message_id,
message_thread_id=message.message_thread_id,
2023-07-01 12:18:58 +00:00
)
except RPCError as e:
2022-11-21 11:38:51 +00:00
return await message.edit(
2023-07-01 12:18:58 +00:00
"此群组不允许发送媒体。"
if e.ID == "CHAT_SEND_MEDIA_FORBIDDEN"
else f"发生错误:\n`{e}`"
)
except Exception as e:
return await message.edit(f"发生错误:\n`{e}`")
2023-03-12 13:42:22 +00:00
shutil.rmtree("data/zpr")
await message.safe_delete()