From 252a91e93cc0cad221b510d07b3085b3059601e2 Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot] WS:ConvertWebp --- modules/WSConvertWebp.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 modules/WSConvertWebp.py diff --git a/modules/WSConvertWebp.py b/modules/WSConvertWebp.py new file mode 100644 index 0000000..f64065d --- /dev/null +++ b/modules/WSConvertWebp.py @@ -0,0 +1,35 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License + +import json +import tempfile +from PIL import Image + + +from AbstractWSHandler import * +class WSConvertWebp(AbstractWSHandler): + NAME = "convertwebp" + + def on_WS_EVENT(self, data): + post = json.loads(data["data"]["post"]) + + if not "metadata" in post or not "files" in post["metadata"]: + return False + + with tempfile.TemporaryDirectory() as tmpdirname: + tempfilenames = [] + for mmfile in post["metadata"]["files"]: + if mmfile["mime_type"] == "image/webp": + res = self.bot.api.get_file(mmfile["id"]) + open(tmpdirname+"/"+mmfile["id"]+".webp", 'wb').write(res.content) + image = Image.open(tmpdirname+"/"+mmfile["id"]+".webp").convert("RGB") + image.save(tmpdirname+"/"+mmfile["id"]+".png", "png") + tempfilenames.append(tmpdirname+"/"+mmfile["id"]+".png") + + if tempfilenames: + root_id = post["id"] + if post["root_id"] != "": + root_id = post["root_id"] + self.bot.api.create_post(post['channel_id'], "Let me convert that for you", props={"from_webhook":"true"}, filepaths=tempfilenames, root_id=root_id) + return True -- 2.43.0