diff --git a/stickerbridge/bot_commands.py b/stickerbridge/bot_commands.py index 9a6b0a8..cd69868 100644 --- a/stickerbridge/bot_commands.py +++ b/stickerbridge/bot_commands.py @@ -36,6 +36,7 @@ class Command: "List of commands:\n" "help - Show this help message.\n" "import [import_name] [-p | --primary] - Use this to import Telegram stickers from given link. import_name is pack_name if not provided. if -p flag is provided, pack will be uploaded as a Default Pack for this room." + "preview [pack_name] - Use this to create a preview for a Telegram stickers. If pack_name is not provided, then preview is generated for a primary pack." ) await send_text_to_room(self.client, self.room.room_id, text) @@ -83,36 +84,32 @@ class Command: await send_text_to_room(self.client, self.room.room_id, text) async def _generate_preview(self): - await send_text_to_room( + pack_name = "" + if not self.args: + isDefault = True + await send_text_to_room( self.client, self.room.room_id, - f"Not implemented YET",) - return + f"Previewing primary pack") + else: + pack_name = self.args[0] - # if not self.args: - # await send_text_to_room( - # self.client, - # self.room.room_id, - # f"You need to provide pack name. Example: !sb preview pack_name",) - # return - - # pack_name = self.args[0] - # previewer = MatrixPreview(self.client, self.room) - # async for status in previewer.generate_stickerset_preview_to_room(pack_name): - # switch = { - # MatrixPreview.STATUS_OK: "Done", - # MatrixPreview.STATUS_NO_PERMISSION: ( - # "I do not have permissions to update this room\n" - # "Please, give me mod 🙏" - # ), - # MatrixPreview.STATUS_PACK_NOT_EXISTS: ( - # f"Stickerpack '{pack_name}' does not exists.\n" - # "Please create it first." - # ), - # MatrixPreview.STATUS_UPDATING_ROOM_STATE: f"Updating room state...", - # } - # text = switch.get(status, "Warning: Unknown status") - # await send_text_to_room(self.client, self.room.room_id, text) + previewer = MatrixPreview(self.client, self.room) + async for status in previewer.generate_stickerset_preview_to_room(pack_name): + switch = { + MatrixPreview.STATUS_OK: "Done", + MatrixPreview.STATUS_NO_PERMISSION: ( + "I do not have permissions to update this room\n" + "Please, give me mod 🙏" + ), + MatrixPreview.STATUS_PACK_NOT_EXISTS: ( + f"Stickerpack '{pack_name}' does not exists.\n" + "Please create it first." + ), + MatrixPreview.STATUS_UPDATING_ROOM_STATE: f"Updating room state...", + } + text = switch.get(status, "Warning: Unknown status") + await send_text_to_room(self.client, self.room.room_id, text) async def _unknown_command(self): await send_text_to_room( diff --git a/stickerbridge/chat_functions.py b/stickerbridge/chat_functions.py index f53a5ff..160ecfc 100644 --- a/stickerbridge/chat_functions.py +++ b/stickerbridge/chat_functions.py @@ -20,6 +20,12 @@ async def send_text_to_room(client: AsyncClient, room_id: str, message: str): content, ) +async def send_sticker_to_room(client: AsyncClient, room_id: str, content: dict): + return await client.room_send( + room_id, + "m.sticker", + content, + ) async def has_permission(client: AsyncClient, room_id: str, permission_type: str): """Reimplementation of AsyncClient.has_permission because matrix-nio version always gives an error @@ -52,6 +58,8 @@ async def is_stickerpack_existing(client: AsyncClient, room_id: str, pack_name: async def upload_stickerpack(client: AsyncClient, room_id: str, stickerset: MatrixStickerset, name): return await client.room_put_state(room_id, 'im.ponies.room_emotes', stickerset.json(), state_key=name) +async def update_room_image(client: AsyncClient, room_id: str, preview: dict): + return await client.room_put_state(room_id, 'm.room.avatar', preview) async def upload_image(client: AsyncClient, image: str): mime_type = magic.from_file(image, mime=True) diff --git a/stickerbridge/matrix_preview.py b/stickerbridge/matrix_preview.py index dff9450..a92d3ef 100644 --- a/stickerbridge/matrix_preview.py +++ b/stickerbridge/matrix_preview.py @@ -1,12 +1,6 @@ -import tempfile -import os - from nio import MatrixRoom, AsyncClient -from chat_functions import has_permission, is_stickerpack_existing, send_text_to_room, upload_image, upload_stickerpack -from sticker_types import Sticker, MatrixStickerset -from telegram_exporter import TelegramExporter - +from chat_functions import has_permission, is_stickerpack_existing, send_sticker_to_room, update_room_image class MatrixPreview: @@ -30,40 +24,18 @@ class MatrixPreview: yield self.STATUS_NO_PERMISSION return - name = pack_name - if pack_name.lower() == "default": - name = "" - - if not await is_stickerpack_existing(self.client, self.room.room_id, name): + if not await is_stickerpack_existing(self.client, self.room.room_id, pack_name): yield self.STATUS_PACK_NOT_EXISTS return - stickerpack = await self.client.room_get_state_event(self.room.room_id, 'im.ponies.room_emotes', name) - - # {'pack': {'display_name': 'https://t.me/addstickers/kentai_radiquum'}, 'images': {'🤗': {'url': 'mxc://wah.su/OzamJbZNgcIIDeMXofMnmkBO', 'usage': ['sticker']}, '🤗-1': {...}}} - - print(stickerpack) - # yield self.STATUS_DOWNLOADING - # converted_stickerset = await self.exporter.get_stickerset(pack_name) - # yield self.STATUS_UPLOADING - # for sticker in converted_stickerset: - # with tempfile.NamedTemporaryFile('w+b', delete=False) as file: - # file.write(sticker.image_data) - # sticker_mxc = await upload_image(self.client, file.name) - - # file.close() - # os.unlink(file.name) - - # stickerset.add_sticker(sticker_mxc, sticker.alt_text) - - # if not stickerset.count(): - # yield self.STATUS_PACK_EMPTY - # return - yield self.STATUS_UPDATING_ROOM_STATE - + stickerpack = await self.client.room_get_state_event(self.room.room_id, 'im.ponies.room_emotes', pack_name) + first_item = dict(list(stickerpack.content["images"].items())[:1]) + _first_item = first_item.popitem() + await update_room_image(self.client, self.room.room_id, {"url": _first_item[1]['url']}) - # await upload_stickerpack(self.client, self.room.room_id, stickerset, name) + for stick in list(stickerpack.content["images"].items())[:5]: + await send_sticker_to_room(self.client, self.room.room_id, {"body": stick[0], "url": stick[1]['url'], "info": {"mimetype":"image/png"}}) yield self.STATUS_OK diff --git a/stickerbridge/telegram_exporter.py b/stickerbridge/telegram_exporter.py index a486db3..002c683 100644 --- a/stickerbridge/telegram_exporter.py +++ b/stickerbridge/telegram_exporter.py @@ -17,7 +17,7 @@ from sticker_types import Sticker def _convert_image(data: bytes) -> (bytes, int, int): image: Image.Image = Image.open(BytesIO(data)).convert("RGBA") new_file = BytesIO() - image.save(new_file, "webp") + image.save(new_file, "png") w, h = image.size if w > 256 or h > 256: if w > h: