From e71d08aee89d978f1cee6833fee13d3ef6517da3 Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 19 Jun 2020 01:32:13 +0200 Subject: [PATCH] [somebot] WS:ConvertWebp --- somebot/modules/WSConvertWebp.py | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 somebot/modules/WSConvertWebp.py diff --git a/somebot/modules/WSConvertWebp.py b/somebot/modules/WSConvertWebp.py new file mode 100644 index 0000000..1e8b4d4 --- /dev/null +++ b/somebot/modules/WSConvertWebp.py @@ -0,0 +1,40 @@ +# Mattermost Bot. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# published under MIT-License + +import inspect +import json +import tempfile +from PIL import Image + + +from AbstractWSHandler import * +class WSConvertWebp(AbstractWSHandler): + NAME = "convertwebp" + + def on_WS_EVENT(self, data): + # no longer needed, as its checked on registration + if data["event"] != "posted": + return False + + 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 len(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