Convert stickers to png

This commit is contained in:
Tulir Asokan
2018-01-27 21:57:14 +02:00
parent 0280309098
commit 58d572162d
3 changed files with 24 additions and 6 deletions
+1 -1
View File
@@ -86,7 +86,7 @@ does not do this automatically.~~
* [x] Forwards * [x] Forwards
* [x] Images * [x] Images
* [x] Locations * [x] Locations
* [ ] Stickers (only works if client supports webp, need converter) * [x] Stickers
* [x] Audio messages * [x] Audio messages
* [x] Video messages * [x] Video messages
* [x] Documents * [x] Documents
+22 -5
View File
@@ -17,6 +17,8 @@ from telethon.tl.functions.messages import GetFullChatRequest, EditChatAdminRequ
from telethon.tl.functions.channels import GetParticipantsRequest from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.errors.rpc_error_list import ChatAdminRequiredError from telethon.errors.rpc_error_list import ChatAdminRequiredError
from telethon.tl.types import * from telethon.tl.types import *
from PIL import Image
from io import BytesIO
import mimetypes import mimetypes
import magic import magic
from .db import Portal as DBPortal, Message as DBMessage from .db import Portal as DBPortal, Message as DBMessage
@@ -300,18 +302,31 @@ class Portal:
name = media.caption name = media.caption
sender.intent.send_image(self.mxid, uploaded["content_uri"], info=info, text=name) sender.intent.send_image(self.mxid, uploaded["content_uri"], info=info, text=name)
@staticmethod
def convert_webp(file, to="png"):
image = Image.open(BytesIO(file)).convert("RGBA")
file = BytesIO()
image.save(file, to)
return file.getvalue()
def handle_telegram_document(self, source, sender, media): def handle_telegram_document(self, source, sender, media):
file = source.download_file(media.document) file = source.download_file(media.document)
mime_type = magic.from_buffer(file, mime=True) mime_type = magic.from_buffer(file, mime=True)
dont_change_mime = False
if mime_type == "image/webp":
file = self.convert_webp(file, to="png")
mime_type = "image/png"
dont_change_mime = True
uploaded = sender.intent.upload_file(file, mime_type) uploaded = sender.intent.upload_file(file, mime_type)
name = media.caption name = media.caption
if not name: for attr in media.document.attributes:
for attr in media.document.attributes: if not name and isinstance(attr, DocumentAttributeFilename):
if isinstance(attr, DocumentAttributeFilename): name = attr.file_name
name = attr.file_name if not dont_change_mime:
(mime_from_name, _) = mimetypes.guess_type(name) (mime_from_name, _) = mimetypes.guess_type(name)
mime_type = mime_from_name or mime_type mime_type = mime_from_name or mime_type
break elif isinstance(attr, DocumentAttributeSticker):
name = f"Sticker for {attr.alt}"
mime_type = media.document.mime_type or mime_type mime_type = media.document.mime_type or mime_type
info = { info = {
"size": media.document.size, "size": media.document.size,
@@ -322,6 +337,8 @@ class Portal:
type = "m.video" type = "m.video"
elif mime_type.startswith("audio/"): elif mime_type.startswith("audio/"):
type = "m.audio" type = "m.audio"
elif mime_type.startswith("image/"):
type = "m.image"
sender.intent.send_file(self.mxid, uploaded["content_uri"], info=info, text=name, sender.intent.send_file(self.mxid, uploaded["content_uri"], info=info, text=name,
type=type) type=type)
+1
View File
@@ -6,3 +6,4 @@ python-magic
SQLAlchemy SQLAlchemy
Telethon Telethon
Markdown Markdown
Pillow