2022-10-17 02:37:24 +00:00
|
|
|
|
import contextlib
|
|
|
|
|
import shutil
|
|
|
|
|
from os import makedirs
|
|
|
|
|
from os.path import exists, dirname, abspath
|
2022-09-28 10:58:40 +00:00
|
|
|
|
from pagermaid.listener import listener
|
|
|
|
|
from pagermaid.enums import Client, Message, AsyncClient
|
|
|
|
|
from pyrogram.types import InputMediaPhoto
|
2022-11-18 09:51:25 +00:00
|
|
|
|
from pyrogram.errors import RPCError
|
2023-03-12 13:40:36 +00:00
|
|
|
|
from pathlib import Path
|
2022-11-18 09:51:25 +00:00
|
|
|
|
|
2023-03-12 13:19:08 +00:00
|
|
|
|
pixiv_img_host = "pixiv.yuki.sh"
|
2022-11-18 09:51:25 +00:00
|
|
|
|
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'
|
|
|
|
|
}
|
2023-03-12 13:40:36 +00:00
|
|
|
|
data_path = Path("data/zpr")
|
2022-09-28 10:58:40 +00:00
|
|
|
|
|
2022-11-21 11:38:51 +00:00
|
|
|
|
|
2023-03-12 13:19:08 +00:00
|
|
|
|
async def get_result(message, request, r18=2):
|
2022-09-28 10:58:40 +00:00
|
|
|
|
# r18: 0为非 R18,1为 R18,2为混合(在库中的分类,不等同于作品本身的 R18 标识)
|
|
|
|
|
# num: 图片的数量
|
|
|
|
|
# size: 返回图片的尺寸质量
|
2023-03-12 13:40:36 +00:00
|
|
|
|
data_path.mkdir(parents=True, exist_ok=True)
|
2022-09-28 10:58:40 +00:00
|
|
|
|
size = "regular"
|
|
|
|
|
des = "出错了,没有纸片人看了。"
|
2022-11-21 11:38:51 +00:00
|
|
|
|
data = await request.get(
|
|
|
|
|
f"https://api.lolicon.app/setu/v2?num=5&r18={r18}&size={size}",
|
|
|
|
|
headers=headers,
|
|
|
|
|
timeout=10,
|
|
|
|
|
)
|
2022-09-28 10:58:40 +00:00
|
|
|
|
if data.status_code != 200:
|
2023-03-12 13:40:36 +00:00
|
|
|
|
return None, "连接二次元大门出错。。。"
|
2022-09-28 10:58:40 +00:00
|
|
|
|
await message.edit("已进入二次元 . . .")
|
|
|
|
|
try:
|
|
|
|
|
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 = [] # 发送
|
2022-09-28 10:58:40 +00:00
|
|
|
|
await message.edit("努力获取中 。。。")
|
|
|
|
|
for i in range(5):
|
2023-03-12 13:19:08 +00:00
|
|
|
|
urls = result[i]['urls'][size].replace('i.pixiv.re', pixiv_img_host)
|
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
|
2022-09-28 10:58:40 +00:00
|
|
|
|
try:
|
2022-10-15 09:56:12 +00:00
|
|
|
|
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:
|
2022-09-28 10:58:40 +00:00
|
|
|
|
f.write(img.content)
|
|
|
|
|
except Exception:
|
|
|
|
|
return None, None, "连接二次元出错。。。"
|
2023-03-12 13:40:36 +00:00
|
|
|
|
setu_list.append(InputMediaPhoto(media=str(file_path)))
|
|
|
|
|
return setu_list, des if setu_list else None
|
2022-09-28 10:58:40 +00:00
|
|
|
|
|
2022-11-18 09:51:25 +00:00
|
|
|
|
|
2022-09-28 10:58:40 +00:00
|
|
|
|
@listener(command="zpr",
|
|
|
|
|
description="随机获取一组涩涩纸片人。",
|
|
|
|
|
parameters="{r18}")
|
|
|
|
|
async def zpr(client: Client, message: Message, request: AsyncClient):
|
|
|
|
|
arguments = message.arguments.upper().strip()
|
2022-11-18 09:51:25 +00:00
|
|
|
|
message_thread_id = message.reply_to_top_message_id or message.reply_to_message_id
|
2022-09-28 10:58:40 +00:00
|
|
|
|
message = await message.edit("正在前往二次元。。。")
|
|
|
|
|
try:
|
2023-03-12 13:40:36 +00:00
|
|
|
|
photoList, des = await get_result(message, request, r18=2 if arguments == "R18" else 0)
|
2022-10-17 02:37:24 +00:00
|
|
|
|
if not photoList:
|
2023-03-12 13:40:36 +00:00
|
|
|
|
shutil.rmtree("data/zpr")
|
2022-10-17 02:37:24 +00:00
|
|
|
|
return await message.edit(des)
|
|
|
|
|
with contextlib.suppress(Exception):
|
2022-11-18 09:51:25 +00:00
|
|
|
|
await message.edit("传送中。。。")
|
|
|
|
|
try:
|
|
|
|
|
await client.send_media_group(message.chat.id, photoList, reply_to_message_id=message_thread_id)
|
|
|
|
|
except RPCError as e:
|
2022-11-21 11:38:51 +00:00
|
|
|
|
return await message.edit(
|
|
|
|
|
"此群组不允许发送媒体。" if e.ID == "CHAT_SEND_MEDIA_FORBIDDEN" else f"发生错误:\n`{e}`")
|
2022-10-15 09:56:12 +00:00
|
|
|
|
except Exception as e:
|
2022-11-18 09:51:25 +00:00
|
|
|
|
return await message.edit(f"发生错误:\n`{e}`")
|
2023-03-12 13:42:22 +00:00
|
|
|
|
shutil.rmtree("data/zpr")
|
2022-09-28 10:58:40 +00:00
|
|
|
|
await message.safe_delete()
|