PagerMaid_Plugins_Pyro/getstickers/main.py

132 lines
4.4 KiB
Python
Raw Normal View History

import asyncio
import shutil
from collections import defaultdict
import os
import zipfile
2022-08-15 10:37:35 +00:00
from pyrogram.enums import MessageEntityType
from pyrogram.raw.functions.messages import GetStickerSet
from pyrogram.raw.types import InputStickerSetShortName
from pyrogram.raw.types.messages import StickerSet
2022-08-15 10:37:35 +00:00
from pyrogram.types import Document, Sticker
from pagermaid import working_dir
from pagermaid.enums import Message, Client
from pagermaid.listener import listener
from pagermaid.single_utils import safe_remove
2022-08-15 10:37:35 +00:00
async def download_stickers(bot: Client, message: Message, sticker: Sticker):
try:
sticker_set: StickerSet = await bot.invoke(
2023-07-01 12:18:58 +00:00
GetStickerSet(
stickerset=InputStickerSetShortName(short_name=sticker.set_name), hash=0
)
)
2022-08-15 10:37:35 +00:00
except Exception: # noqa
2023-07-01 12:18:58 +00:00
return await message.edit("回复的贴纸不存在于任何贴纸包中。")
2022-08-15 10:37:35 +00:00
2023-07-01 12:18:58 +00:00
pack_file = os.path.join("data/sticker/", sticker_set.set.short_name, "pack.txt")
if os.path.isfile(pack_file):
os.remove(pack_file)
emojis = defaultdict(str)
for pack in sticker_set.packs:
for document_id in pack.documents:
emojis[document_id] += pack.emoticon
file_ext_ns_ion = "webp"
if sticker.is_video:
file_ext_ns_ion = "mp4"
elif sticker.is_animated:
file_ext_ns_ion = "tgs"
async def download(sticker_, emojis_, path, file):
sticker_file = Document._parse(bot, sticker_, "sticker.webp") # noqa
2023-07-01 12:18:58 +00:00
await bot.download_media(
sticker_file.file_id, file_name=os.path.join(path, file)
)
with open(pack_file, "a") as f:
f.write(f"{{'image_file': '{file}','emojis':{emojis_[sticker_.id]}}},")
2023-07-01 12:18:58 +00:00
pending_tasks = [
asyncio.ensure_future(
download(
document,
emojis,
f"data/sticker/{sticker_set.set.short_name}",
f"{i:03d}.{file_ext_ns_ion}",
)
)
for i, document in enumerate(sticker_set.documents)
]
2022-08-15 10:37:35 +00:00
message: Message = await message.edit(
2023-07-01 12:18:58 +00:00
f"正在下载 {sticker_set.set.short_name} 中的 {sticker_set.set.count} 张贴纸。。。"
)
while 1:
2023-07-01 12:18:58 +00:00
done, pending_tasks = await asyncio.wait(
pending_tasks, timeout=2.5, return_when=asyncio.FIRST_COMPLETED
)
if not pending_tasks:
break
2022-08-15 10:37:35 +00:00
await upload_sticker(bot, message, sticker_set)
2022-08-15 10:37:35 +00:00
async def upload_sticker(bot: Client, message: Message, sticker_set: StickerSet):
await message.edit("下载完毕,打包上传中。")
directory_name = sticker_set.set.short_name
os.chdir("data/sticker/")
zipf = zipfile.ZipFile(f"{directory_name}.zip", "w", zipfile.ZIP_DEFLATED)
zipdir(directory_name, zipf)
zipf.close()
await bot.send_document(
message.chat.id,
f"{directory_name}.zip",
caption=sticker_set.set.short_name,
2023-07-01 12:18:58 +00:00
reply_to_message_id=message.reply_to_message_id
or message.reply_to_top_message_id,
)
safe_remove(f"{directory_name}.zip")
shutil.rmtree(directory_name)
os.chdir(working_dir)
await message.safe_delete()
2022-08-15 10:37:35 +00:00
async def get_custom_emojis(bot: Client, message: Message):
if message.entities:
for entity in message.entities:
if entity.type == MessageEntityType.CUSTOM_EMOJI:
try:
2023-07-01 12:18:58 +00:00
sticker = await bot.get_custom_emoji_stickers(
[entity.custom_emoji_id]
)
2022-08-15 10:37:35 +00:00
except Exception:
return None
return sticker[0] if sticker else None
2023-07-01 12:18:58 +00:00
@listener(command="getstickers", description="获取整个贴纸包的贴纸")
2022-08-15 10:37:35 +00:00
async def get_stickers(bot: Client, message: Message):
2023-07-01 12:18:58 +00:00
if not os.path.isdir("data/sticker/"):
os.makedirs("data/sticker/")
2022-08-15 10:37:35 +00:00
if message.reply_to_message:
2023-07-01 12:18:58 +00:00
sticker = message.reply_to_message.sticker or await get_custom_emojis(
bot, message.reply_to_message
)
2022-08-15 10:37:35 +00:00
else:
sticker = message.sticker or await get_custom_emojis(bot, message)
if not sticker:
return await message.edit("请回复一张贴纸。")
if not sticker.set_name:
return await message.edit("回复的贴纸不属于任何贴纸包。")
await download_stickers(bot, message, sticker)
def zipdir(path, zip_):
for root, dirs, files in os.walk(path):
for file in files:
zip_.write(os.path.join(root, file))
os.remove(os.path.join(root, file))