forked from varia/bots
added pdf as links, captions and image thumbnail functionality
This commit is contained in:
parent
03e2dcd79e
commit
b33d4f173d
@ -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."""
|
||||
@ -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()
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user