2020-02-19 15:31:39 +00:00
|
|
|
""" PagerMaid module to handle sticker collection. """
|
|
|
|
|
2020-08-16 09:08:16 +00:00
|
|
|
import certifi
|
|
|
|
import ssl
|
2020-04-06 07:04:10 +00:00
|
|
|
from asyncio import sleep
|
2020-02-24 01:29:40 +00:00
|
|
|
from os import remove
|
2020-02-19 15:31:39 +00:00
|
|
|
from urllib import request
|
|
|
|
from io import BytesIO
|
|
|
|
from telethon.tl.types import DocumentAttributeFilename, MessageMediaPhoto
|
2020-07-16 17:13:00 +00:00
|
|
|
from telethon.errors.common import AlreadyInConversationError
|
2020-02-19 15:31:39 +00:00
|
|
|
from PIL import Image
|
|
|
|
from math import floor
|
|
|
|
from pagermaid import bot
|
|
|
|
from pagermaid.listener import listener
|
2021-04-12 16:25:32 +00:00
|
|
|
from pagermaid.utils import lang
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2020-08-09 15:17:55 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command="sticker",
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('sticker_des'),
|
2020-04-04 15:02:09 +00:00
|
|
|
parameters="<emoji>")
|
2020-02-19 15:31:39 +00:00
|
|
|
async def sticker(context):
|
|
|
|
""" Fetches images/stickers and add them to your pack. """
|
|
|
|
user = await bot.get_me()
|
|
|
|
if not user.username:
|
|
|
|
user.username = user.first_name
|
|
|
|
message = await context.get_reply_message()
|
|
|
|
custom_emoji = False
|
|
|
|
animated = False
|
|
|
|
emoji = ""
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_processing'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
if message and message.media:
|
|
|
|
if isinstance(message.media, MessageMediaPhoto):
|
|
|
|
photo = BytesIO()
|
|
|
|
photo = await bot.download_media(message.photo, photo)
|
|
|
|
elif "image" in message.media.document.mime_type.split('/'):
|
|
|
|
photo = BytesIO()
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_downloading'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
await bot.download_file(message.media.document, photo)
|
|
|
|
if (DocumentAttributeFilename(file_name='sticker.webp') in
|
|
|
|
message.media.document.attributes):
|
|
|
|
emoji = message.media.document.attributes[1].alt
|
|
|
|
custom_emoji = True
|
|
|
|
elif (DocumentAttributeFilename(file_name='AnimatedSticker.tgs') in
|
|
|
|
message.media.document.attributes):
|
2020-02-22 13:14:25 +00:00
|
|
|
photo = BytesIO()
|
|
|
|
await bot.download_file(message.media.document, "AnimatedSticker.tgs")
|
2020-05-05 03:04:26 +00:00
|
|
|
for index in range(len(message.media.document.attributes)):
|
|
|
|
try:
|
|
|
|
emoji = message.media.document.attributes[index].alt
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
custom_emoji = True
|
|
|
|
animated = True
|
|
|
|
photo = 1
|
|
|
|
else:
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_type_not_support'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
else:
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_reply_not_sticker'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if photo:
|
|
|
|
split_strings = context.text.split()
|
|
|
|
if not custom_emoji:
|
|
|
|
emoji = "👀"
|
|
|
|
pack = 1
|
2020-07-25 03:06:54 +00:00
|
|
|
sticker_already = False
|
2020-02-19 15:31:39 +00:00
|
|
|
if len(split_strings) == 3:
|
|
|
|
pack = split_strings[2]
|
|
|
|
emoji = split_strings[1]
|
|
|
|
elif len(split_strings) == 2:
|
|
|
|
if split_strings[1].isnumeric():
|
|
|
|
pack = int(split_strings[1])
|
|
|
|
else:
|
|
|
|
emoji = split_strings[1]
|
|
|
|
|
|
|
|
pack_name = f"{user.username}_{pack}"
|
2021-04-12 16:25:32 +00:00
|
|
|
pack_title = f"@{user.username} {lang('sticker_pack_title')} ({pack})"
|
2020-02-19 15:31:39 +00:00
|
|
|
command = '/newpack'
|
|
|
|
file = BytesIO()
|
|
|
|
|
|
|
|
if not animated:
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_resizing'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
image = await resize_image(photo)
|
|
|
|
file.name = "sticker.png"
|
|
|
|
image.save(file, "PNG")
|
|
|
|
else:
|
|
|
|
pack_name += "_animated"
|
|
|
|
pack_title += " (animated)"
|
|
|
|
command = '/newanimated'
|
|
|
|
|
|
|
|
response = request.urlopen(
|
2020-08-16 09:08:16 +00:00
|
|
|
request.Request(f'http://t.me/addstickers/{pack_name}'), context=ssl.create_default_context(cafile=certifi.where()))
|
2020-08-14 14:49:18 +00:00
|
|
|
if not response.status == 200:
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_telegram_server_error'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-08-14 14:49:18 +00:00
|
|
|
return
|
2020-02-19 15:31:39 +00:00
|
|
|
http_response = response.read().decode("utf8").split('\n')
|
|
|
|
|
|
|
|
if " A <strong>Telegram</strong> user has created the <strong>Sticker Set</strong>." not in \
|
|
|
|
http_response:
|
2020-08-02 11:12:28 +00:00
|
|
|
for _ in range(20): # 最多重试20次
|
2020-07-16 17:13:00 +00:00
|
|
|
try:
|
|
|
|
async with bot.conversation('Stickers') as conversation:
|
|
|
|
await conversation.send_message('/addsticker')
|
|
|
|
await conversation.get_response()
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
await conversation.send_message(pack_name)
|
|
|
|
chat_response = await conversation.get_response()
|
|
|
|
while chat_response.text == "Whoa! That's probably enough stickers for one pack, give it a break. \
|
2020-07-25 03:06:54 +00:00
|
|
|
A pack can't have more than 120 stickers at the moment.":
|
2020-07-16 17:13:00 +00:00
|
|
|
pack += 1
|
|
|
|
pack_name = f"{user.username}_{pack}"
|
2021-04-12 16:25:32 +00:00
|
|
|
pack_title = f"@{user.username} {lang('sticker_pack_title')} ({pack})"
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_change_pack_to') + str(pack) + lang('sticker_last_is_full'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-07-16 17:13:00 +00:00
|
|
|
await conversation.send_message(pack_name)
|
|
|
|
chat_response = await conversation.get_response()
|
|
|
|
if chat_response.text == "Invalid pack selected.":
|
|
|
|
await add_sticker(conversation, command, pack_title, pack_name, animated, message,
|
|
|
|
context, file, emoji)
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
|
|
|
await context.edit(
|
2021-04-12 16:25:32 +00:00
|
|
|
f"{lang('sticker_has_been_added')} [{lang('sticker_this')}](t.me/addstickers/{pack_name}) {lang('sticker_pack')}",
|
2021-02-08 05:25:53 +00:00
|
|
|
parse_mode='md')
|
|
|
|
except:
|
|
|
|
pass
|
2020-07-16 17:13:00 +00:00
|
|
|
return
|
|
|
|
await upload_sticker(animated, message, context, file, conversation)
|
|
|
|
await conversation.get_response()
|
|
|
|
await conversation.send_message(emoji)
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
await conversation.get_response()
|
|
|
|
await conversation.send_message('/done')
|
|
|
|
await conversation.get_response()
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
break
|
|
|
|
except AlreadyInConversationError:
|
2020-07-25 03:06:54 +00:00
|
|
|
if not sticker_already:
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_another_running'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-07-25 03:06:54 +00:00
|
|
|
sticker_already = True
|
|
|
|
else:
|
|
|
|
pass
|
2020-08-02 11:12:28 +00:00
|
|
|
await sleep(.5)
|
2020-07-16 17:13:00 +00:00
|
|
|
except Exception:
|
|
|
|
raise
|
2020-02-19 15:31:39 +00:00
|
|
|
else:
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sticker_no_pack_exist_creating'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
async with bot.conversation('Stickers') as conversation:
|
|
|
|
await add_sticker(conversation, command, pack_title, pack_name, animated, message,
|
|
|
|
context, file, emoji)
|
|
|
|
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
|
|
|
await context.edit(
|
2021-04-12 16:25:32 +00:00
|
|
|
f"{lang('sticker_has_been_added')} [{lang('sticker_this')}](t.me/addstickers/{pack_name}) {lang('sticker_pack')}",
|
2020-04-06 07:04:10 +00:00
|
|
|
parse_mode='md')
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-07-25 03:06:54 +00:00
|
|
|
await sleep(5)
|
2020-04-06 07:04:10 +00:00
|
|
|
try:
|
2021-02-08 05:25:53 +00:00
|
|
|
await context.delete()
|
2020-04-06 07:04:10 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def add_sticker(conversation, command, pack_title, pack_name, animated, message, context, file, emoji):
|
|
|
|
await conversation.send_message(command)
|
|
|
|
await conversation.get_response()
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
await conversation.send_message(pack_title)
|
|
|
|
await conversation.get_response()
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
await upload_sticker(animated, message, context, file, conversation)
|
|
|
|
await conversation.get_response()
|
|
|
|
await conversation.send_message(emoji)
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
await conversation.get_response()
|
|
|
|
await conversation.send_message("/publish")
|
|
|
|
if animated:
|
|
|
|
await conversation.get_response()
|
|
|
|
await conversation.send_message(f"<{pack_title}>")
|
|
|
|
await conversation.get_response()
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
await conversation.send_message("/skip")
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
await conversation.get_response()
|
|
|
|
await conversation.send_message(pack_name)
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
await conversation.get_response()
|
|
|
|
await bot.send_read_acknowledge(conversation.chat_id)
|
|
|
|
|
|
|
|
|
|
|
|
async def upload_sticker(animated, message, context, file, conversation):
|
|
|
|
if animated:
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('us_animated_uploading'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-22 13:14:25 +00:00
|
|
|
await conversation.send_file("AnimatedSticker.tgs", force_document=True)
|
2020-02-24 01:29:40 +00:00
|
|
|
remove("AnimatedSticker.tgs")
|
2020-02-19 15:31:39 +00:00
|
|
|
else:
|
|
|
|
file.seek(0)
|
2021-02-08 05:25:53 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('us_static_uploading'))
|
2021-02-08 05:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
await conversation.send_file(file, force_document=True)
|
|
|
|
|
|
|
|
|
|
|
|
async def resize_image(photo):
|
|
|
|
image = Image.open(photo)
|
|
|
|
maxsize = (512, 512)
|
|
|
|
if (image.width and image.height) < 512:
|
|
|
|
size1 = image.width
|
|
|
|
size2 = image.height
|
|
|
|
if image.width > image.height:
|
|
|
|
scale = 512 / size1
|
|
|
|
size1new = 512
|
|
|
|
size2new = size2 * scale
|
|
|
|
else:
|
|
|
|
scale = 512 / size2
|
|
|
|
size1new = size1 * scale
|
|
|
|
size2new = 512
|
|
|
|
size1new = floor(size1new)
|
|
|
|
size2new = floor(size2new)
|
|
|
|
size_new = (size1new, size2new)
|
|
|
|
image = image.resize(size_new)
|
|
|
|
else:
|
|
|
|
image.thumbnail(maxsize)
|
|
|
|
|
|
|
|
return image
|