From b33d4f173d2bb09719c3fb0d00d1a4ad7bcf4c9e Mon Sep 17 00:00:00 2001 From: ccl Date: Thu, 8 Jun 2023 01:34:14 +0200 Subject: [PATCH] added pdf as links, captions and image thumbnail functionality --- LogBot/logbot.py | 58 ++++++++++++++++++++++++++++++++++++++------ LogBot/template.html | 5 ++-- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/LogBot/logbot.py b/LogBot/logbot.py index 721ba38..96a4c0d 100644 --- a/LogBot/logbot.py +++ b/LogBot/logbot.py @@ -7,6 +7,9 @@ from urllib.parse import urlparse from mimetypes import guess_type import jinja2 from xbotlib import Bot +from PIL import Image +from io import BytesIO +import base64 # Main Logbot class @@ -37,6 +40,8 @@ class Logbot(Bot): logbot @uptime: To check how long @logbot has been around + logbot @caption "message": Adding a caption to a particular file. + @bots: To see who is around :) """ # noqa @@ -71,7 +76,29 @@ class Logbot(Bot): # get the size of the file media_size = os.path.getsize(os.path.join(self.output, folder_name, media_path)) - return media_type, media_mime, media_path, media_size + if media_type == "image": + try: + size = (450, 450) + im = Image.open(file_path) + im.thumbnail(size) + + if (im.mode == 'RGBA'): + bg = Image.new('RGBA', im.size, (255,255,255)) + composite = Image.alpha_composite(bg, im) + im=composite.convert('RGB') + + output = BytesIO() + im.save(output, format='JPEG') + im_data = output.getvalue() + data_url = base64.b64encode(im_data).decode() + data_url = "data:image/png;base64, " + data_url + except Exception as e: + print('Thumbnailer:', e) + data_url = media_path + else: + data_url='' + + return media_type, media_mime, media_path, media_size, data_url, filename def _href_wrap(self, post): """Wrap links in tags.""" @@ -86,7 +113,7 @@ class Logbot(Bot): if "@" in folder_name: # hacky folder_name = self._parse_room_name(folder_name) log_path = os.path.join(self.output, folder_name, "index.html") - template = jinja2.Template(open("template.html").read()) # it would be useful to use self.template here + template = jinja2.Template(open("template.html").read()) # it would be useful to use self.template here with open(log_path, "w") as out: html = template.render( title=self.db[message.room]["title"], @@ -107,7 +134,7 @@ class Logbot(Bot): template = jinja2.Template(open("template.rss").read()) # self.feedtemplate would be useful to have in the conf with open(feed_path, "w") as out: feed = template.render( - log_path=os.path.join(self.baseurl, folder_name, "index.html"), + log_path=os.path.join(self.baseurl, folder_name, "index.html"), feed_path=os.path.join(self.baseurl, folder_name, "feed.rss.xml"), title=self.db[message.room]["title"], db=self.db[message.room], @@ -117,7 +144,7 @@ class Logbot(Bot): out.write(feed) self.log.info(f"writing to: { feed_path }") - def _add_to_db(self, message, media_type=None, media_path=None, media_size=None): + def _add_to_db(self, message, media_type=None, media_path=None, media_size=None, data_url=None, filename=None): """Save new entry to database.""" keys = [x for x in self.db[message.room]["messages"].keys()] keys.sort(key=int) @@ -134,6 +161,9 @@ class Logbot(Bot): self.db[message.room]["messages"][new_key]['media']['type'] = media_type self.db[message.room]["messages"][new_key]['media']['path'] = media_path self.db[message.room]["messages"][new_key]['media']['size'] = media_size + self.db[message.room]["messages"][new_key]['media']['filename'] = filename + self.db[message.room]["messages"][new_key]['media']['data_url'] = data_url + self.db[message.room]["messages"][new_key]['media']['caption'] = '' else: post = message.content.replace("@add ", "") post = self._href_wrap(post) @@ -141,7 +171,6 @@ class Logbot(Bot): self.db[message.room]["messages"][new_key]['post'] = post self.db[message.room]["messages"][new_key]['date'] = date self.db._dumps() - return new_key def _parse_room_name(self, room): @@ -203,10 +232,10 @@ class Logbot(Bot): # Response to files: image / PDF / audio / video if message.url: - media_type, media_mime, media_path, media_size = self._download(message) + media_type, media_mime, media_path, media_size, data_url, filename = self._download(message) # TODO: Insert a list of accepted file types here. if media_path: - num = self._add_to_db(message, media_type=media_mime, media_path=media_path, media_size=media_size) + num = self._add_to_db(message, media_type=media_mime, media_path=media_path, media_size=media_size, data_url=data_url, filename=filename) media_type = media_type.replace("images", "image") # linguistic hack! if 'pdf' in message.url: media_type = 'PDF' # linguistic hack! @@ -240,6 +269,20 @@ class Logbot(Bot): else: reply = "This one is already gone!" + # Response to @caption + elif "@caption" in message.text: + match = re.findall(r"@caption \d*", message.content)[0] + key = str(match.replace("@caption ", "")) + captiontext = re.findall(r'"(.*?)"', message.text) + self.db[message.room]["messages"][key]['media']['caption'] = captiontext + reply = captiontext + + if key in self.db[message.room]["messages"]: + self.log.info( + f"To be captioned: { self.db[message.room]['messages'][key] }" + ) + reply = f"Added a caption for file number {key}." + # Response to @title elif "@title" in message.text: match = re.findall("@title .*", message.content)[0] @@ -320,5 +363,4 @@ class Logbot(Bot): # Reply to the groupchat self.reply(reply, room=message.room) - Logbot() diff --git a/LogBot/template.html b/LogBot/template.html index 356c325..74d983b 100644 --- a/LogBot/template.html +++ b/LogBot/template.html @@ -17,14 +17,15 @@ {% if 'media' in db[num] %}

{% if 'image' in db[num]['media']['type'] %} - + {% elif 'application' in db[num]['media']['type'] %} - + {{ db[num]['media']['filename']}} {% elif 'audio' in db[num]['media']['type'] %} {% elif 'video' in db[num]['media']['type'] %} {% endif %} +

{{ db[num]['media']['caption'] | join("") }}

{% else %}

{{ db[num]['post'] }}