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
|
from mimetypes import guess_type
|
||||||
import jinja2
|
import jinja2
|
||||||
from xbotlib import Bot
|
from xbotlib import Bot
|
||||||
|
from PIL import Image
|
||||||
|
from io import BytesIO
|
||||||
|
import base64
|
||||||
|
|
||||||
# Main Logbot class
|
# Main Logbot class
|
||||||
|
|
||||||
@ -37,6 +40,8 @@ class Logbot(Bot):
|
|||||||
|
|
||||||
logbot @uptime: To check how long @logbot has been around
|
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 :)
|
@bots: To see who is around :)
|
||||||
""" # noqa
|
""" # noqa
|
||||||
|
|
||||||
@ -71,7 +76,29 @@ class Logbot(Bot):
|
|||||||
# get the size of the file
|
# get the size of the file
|
||||||
media_size = os.path.getsize(os.path.join(self.output, folder_name, media_path))
|
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):
|
def _href_wrap(self, post):
|
||||||
"""Wrap links in <a> tags."""
|
"""Wrap links in <a> tags."""
|
||||||
@ -117,7 +144,7 @@ class Logbot(Bot):
|
|||||||
out.write(feed)
|
out.write(feed)
|
||||||
self.log.info(f"writing to: { feed_path }")
|
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."""
|
"""Save new entry to database."""
|
||||||
keys = [x for x in self.db[message.room]["messages"].keys()]
|
keys = [x for x in self.db[message.room]["messages"].keys()]
|
||||||
keys.sort(key=int)
|
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']['type'] = media_type
|
||||||
self.db[message.room]["messages"][new_key]['media']['path'] = media_path
|
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']['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:
|
else:
|
||||||
post = message.content.replace("@add ", "")
|
post = message.content.replace("@add ", "")
|
||||||
post = self._href_wrap(post)
|
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]['post'] = post
|
||||||
self.db[message.room]["messages"][new_key]['date'] = date
|
self.db[message.room]["messages"][new_key]['date'] = date
|
||||||
self.db._dumps()
|
self.db._dumps()
|
||||||
|
|
||||||
return new_key
|
return new_key
|
||||||
|
|
||||||
def _parse_room_name(self, room):
|
def _parse_room_name(self, room):
|
||||||
@ -203,10 +232,10 @@ class Logbot(Bot):
|
|||||||
|
|
||||||
# Response to files: image / PDF / audio / video
|
# Response to files: image / PDF / audio / video
|
||||||
if message.url:
|
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.
|
# TODO: Insert a list of accepted file types here.
|
||||||
if media_path:
|
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!
|
media_type = media_type.replace("images", "image") # linguistic hack!
|
||||||
if 'pdf' in message.url:
|
if 'pdf' in message.url:
|
||||||
media_type = 'PDF' # linguistic hack!
|
media_type = 'PDF' # linguistic hack!
|
||||||
@ -240,6 +269,20 @@ class Logbot(Bot):
|
|||||||
else:
|
else:
|
||||||
reply = "This one is already gone!"
|
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
|
# Response to @title
|
||||||
elif "@title" in message.text:
|
elif "@title" in message.text:
|
||||||
match = re.findall("@title .*", message.content)[0]
|
match = re.findall("@title .*", message.content)[0]
|
||||||
@ -320,5 +363,4 @@ class Logbot(Bot):
|
|||||||
# Reply to the groupchat
|
# Reply to the groupchat
|
||||||
self.reply(reply, room=message.room)
|
self.reply(reply, room=message.room)
|
||||||
|
|
||||||
|
|
||||||
Logbot()
|
Logbot()
|
||||||
|
@ -17,14 +17,15 @@
|
|||||||
{% if 'media' in db[num] %}
|
{% if 'media' in db[num] %}
|
||||||
<p class="message">
|
<p class="message">
|
||||||
{% if 'image' in db[num]['media']['type'] %}
|
{% 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'] %}
|
{% 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'] %}
|
{% elif 'audio' in db[num]['media']['type'] %}
|
||||||
<audio src="{{ db[num]['media']['path'] }}" preload="none" controls></audio>
|
<audio src="{{ db[num]['media']['path'] }}" preload="none" controls></audio>
|
||||||
{% elif 'video' in db[num]['media']['type'] %}
|
{% elif 'video' in db[num]['media']['type'] %}
|
||||||
<video src="{{ db[num]['media']['path'] }}" preload="none" controls></video>
|
<video src="{{ db[num]['media']['path'] }}" preload="none" controls></video>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
<figcaption>{{ db[num]['media']['caption'] | join("") }}</figcaption>
|
||||||
</p>
|
</p>
|
||||||
{% else %}
|
{% else %}
|
||||||
<p class="message">{{ db[num]['post'] }}</p>
|
<p class="message">{{ db[num]['post'] }}</p>
|
||||||
|
Loading…
Reference in New Issue
Block a user