added pdf as links, captions and image thumbnail functionality

This commit is contained in:
ccl 2023-06-08 01:34:14 +02:00
parent 03e2dcd79e
commit b33d4f173d
2 changed files with 53 additions and 10 deletions

View File

@ -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 <num> "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 <a> tags."""
@ -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()

View File

@ -17,14 +17,15 @@
{% if 'media' in db[num] %}
<p class="message">
{% if 'image' in db[num]['media']['type'] %}
<img src="{{ db[num]['media']['path'] }}" loading="lazy">
<a href="{{ db[num]['media']['path'] }}"><img class='thumbnail' src="{{ db[num]['media']['data_url'] }}"></a>
{% elif 'application' in db[num]['media']['type'] %}
<iframe src="{{ db[num]['media']['path'] }}" loading="lazy"></iframe>
<a href="{{ db[num]['media']['path'] }}" loading="lazy">{{ db[num]['media']['filename']}}</a>
{% elif 'audio' in db[num]['media']['type'] %}
<audio src="{{ db[num]['media']['path'] }}" preload="none" controls></audio>
{% elif 'video' in db[num]['media']['type'] %}
<video src="{{ db[num]['media']['path'] }}" preload="none" controls></video>
{% endif %}
<figcaption>{{ db[num]['media']['caption'] | join("") }}</figcaption>
</p>
{% else %}
<p class="message">{{ db[num]['post'] }}</p>