From 2525ac4481ef7dc4928fd23c7c786f706c47c9d2 Mon Sep 17 00:00:00 2001 From: Vesugierii <66554786+Vesugierii@users.noreply.github.com> Date: Mon, 19 Jul 2021 12:05:55 +0800 Subject: [PATCH] =?UTF-8?q?getstickers=20=E5=A2=9E=E5=8A=A0=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=85=A8=E9=83=A8=E8=B4=B4=E7=BA=B8=E5=8C=85=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20(#174)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加下载所有贴纸包功能 Co-authored-by: Xtao_dada --- getstickers.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++---- list.json | 4 +-- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/getstickers.py b/getstickers.py index 6aa969b..37219e2 100644 --- a/getstickers.py +++ b/getstickers.py @@ -3,6 +3,7 @@ from collections import defaultdict import os import zipfile from PIL import Image +from telethon.tl.functions.messages import GetAllStickersRequest from telethon.tl.functions.messages import GetStickerSetRequest from telethon.errors import MessageNotModifiedError from telethon.errors.rpcerrorlist import StickersetInvalidError @@ -28,15 +29,80 @@ except ImportError: @listener(is_plugin=True, outgoing=True, command=alias_command("getstickers"), - description="获取整个贴纸包的贴纸,任意值开启 tgs 转 gif;转 gif 需要手动安装 pypi 依赖 lottie[gif] 。", + description="获取整个贴纸包的贴纸,false 关闭 tgs 转 gif;all开启下载所有贴纸包;转 gif 需要手动安装 pypi 依赖 lottie[gif] 。", parameters="<任意值>") async def getstickers(context): tgs_gif = True - if len(context.parameter) == 0: - tgs_gif = False if not os.path.isdir('data/sticker/'): os.makedirs('data/sticker/') - if context.reply_to_msg_id: + if len(context.parameter) == 1 or len(context.parameter) == 2: + if context.parameter[0] == "false" or context.parameter[1] == "false": + tgs_gif = False + if context.parameter[0] == "all" or context.paramete[1]== "all": + sticker_sets = await context.client(GetAllStickersRequest(0)) + for stickerset in sticker_sets.sets: + file_ext_ns_ion = "webp" + wdnmd = InputStickerSetID(id=stickerset.id, access_hash=stickerset.access_hash) + sticker_set = await context.client(GetStickerSetRequest(wdnmd)) + pack_file = os.path.join('data/sticker/', sticker_set.set.short_name, "pack.txt") + if os.path.isfile(pack_file): + os.remove(pack_file) + # Sticker emojis + emojis = defaultdict(str) + for pack in sticker_set.packs: + for document_id in pack.documents: + emojis[document_id] += pack.emoticon + async def download(sticker, emojis, path, file): + await context.client.download_media(sticker, file=os.path.join(path, file)) + with open(pack_file, "a") as f: + f.write(f"{{'image_file': '{file}','emojis':{emojis[sticker.id]}}},") + if file_ext_ns_ion == 'tgs' and lottie_import and tgs_gif: + animated = import_tgs(os.path.join(path, file)) + export_gif(animated, os.path.join(path, file)[:-3] + 'gif') + elif file_ext_ns_ion == 'webp': + convert_png(os.path.join(path, file)) + + pending_tasks = [ + asyncio.ensure_future( + download(document, emojis, 'data/sticker/' + sticker_set.set.short_name, + f"{i:03d}.{file_ext_ns_ion}") + ) for i, document in enumerate(sticker_set.documents) + ] + xx = await context.client.send_message(context.chat_id, + f"正在下载 {sticker_set.set.short_name} 中的 {sticker_set.set.count} 张贴纸。。。") + num_tasks = len(pending_tasks) + while 1: + done, pending_tasks = await asyncio.wait(pending_tasks, timeout=2.5, + return_when=asyncio.FIRST_COMPLETED) + if file_ext_ns_ion == 'tgs' and lottie_import and tgs_gif: + try: + await xx.edit( + f"正在下载/转换中,进度: {num_tasks - len(pending_tasks)}/{sticker_set.set.count}") + except MessageNotModifiedError: + pass + if not pending_tasks: + break + await xx.edit("下载完毕,打包上传中。") + directory_name = sticker_set.set.short_name + os.chdir("data/sticker/") # 修改当前工作目录 + zipf = zipfile.ZipFile(directory_name + ".zip", "w", zipfile.ZIP_DEFLATED) + zipdir(directory_name, zipf) + zipf.close() + await context.client.send_file( + context.chat_id, + directory_name + ".zip", + caption=sticker_set.set.short_name, + force_document=True, + allow_cache=False + ) + try: + os.remove(directory_name + ".zip") + os.remove(directory_name) + except: + pass + os.chdir(working_dir) + await xx.delete() + else: reply_message = await context.get_reply_message() if not reply_message.sticker: await context.edit("请回复一张贴纸。") @@ -117,8 +183,6 @@ async def getstickers(context): pass os.chdir(working_dir) await context.delete() - else: - await context.edit("请回复一张贴纸。") def find_instance(items, class_or_tuple): diff --git a/list.json b/list.json index fcc32f6..5e53b11 100644 --- a/list.json +++ b/list.json @@ -532,9 +532,9 @@ }, { "name": "getstickers", - "version": "1.04", + "version": "1.1", "section": "chat", - "maintainer": "xtaodada", + "maintainer": "xtaodada,Vesugierii", "size": "5.4 kb", "supported": true, "des-short": "贴纸包批量导出",