Browse Source

writing HTML in templates, not in db + padding baseurl to templates to make images in the RSS work (hopefully)

master
manetta 3 years ago
parent
commit
47aa5473a5
  1. 37
      LogBot/logbot.py

37
LogBot/logbot.py

@ -12,6 +12,8 @@ from xbotlib import Bot
class Logbot(Bot):
baseurl = "https://vvvvvvaria.org/logs/" # hardcoding the url now, self.baseurl would be helpful to have in the conf
help = """Oh dear, logbot is here!
(You can speak to the bot using @logbot or logbot:)
@ -61,23 +63,13 @@ class Logbot(Bot):
with open(file_path, "wb") as media_file:
media_file.write(data)
# define media_post
# define relative path to media file as "media_path"
media_path = os.path.join(media_type, filename)
if media_type == "image":
media_post = f'<img src="{ media_path }" loading="lazy">'
elif media_type == "application":
media_post = f'<iframe src="{ media_path }" loading="lazy"></iframe>'
elif media_type == "audio":
media_post = f'<audio controls src="{ media_path }"></audio>'
elif media_type == "video":
media_post = f'<video controls src="{ media_path }"></video>'
else:
media_post = None
# get the size of the file
media_size = os.path.getsize(os.path.join(self.output, folder_name, media_path))
return media_post, media_type, media_mime, media_path, media_size
return media_type, media_mime, media_path, media_size
def _href_wrap(self, post):
"""Wrap links in <a> tags."""
@ -112,16 +104,17 @@ 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("https://vvvvvvaria.org/logs/", folder_name, "index.html"), # hardcoding the url now, self.baseurl would be helpful to have in the conf
feed_path=os.path.join("https://vvvvvvaria.org/logs/", folder_name, "feed.rss.xml"), # hardcoding the url again
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],
date=date.strftime("%a, %d %b %Y %H:%M:%S +0100") # timezone is hardcoded now
date=date.strftime("%a, %d %b %Y %H:%M:%S +0100"), # timezone is hardcoded now
baseurl=self.baseurl
)
out.write(feed)
self.log.info(f"writing to: { feed_path }")
def _add_to_db(self, message, media_post=None, media_type=None, media_url=None, media_size=None):
def _add_to_db(self, message, media_type=None, media_path=None, media_size=None):
"""Save new entry to database."""
keys = [x for x in self.db[message.room]["messages"].keys()]
keys.sort(key=int)
@ -130,13 +123,13 @@ class Logbot(Bot):
new_key = "0"
else:
new_key = str(int(keys[-1]) + 1)
if media_post:
if media_path:
self.db[message.room]["messages"][new_key] = {}
self.db[message.room]["messages"][new_key]['post'] = media_post
self.db[message.room]["messages"][new_key]['post'] = ''
self.db[message.room]["messages"][new_key]['date'] = date
self.db[message.room]["messages"][new_key]['media'] = {}
self.db[message.room]["messages"][new_key]['media']['type'] = media_type
self.db[message.room]["messages"][new_key]['media']['url'] = os.path.join("https://vvvvvvaria.org/logs/", media_url)
self.db[message.room]["messages"][new_key]['media']['path'] = media_path
self.db[message.room]["messages"][new_key]['media']['size'] = media_size
else:
post = message.content.replace("@add ", "")
@ -207,10 +200,10 @@ class Logbot(Bot):
# Response to files: image / PDF / audio / video
if message.url:
media_post, media_type, media_mime, media_path, media_size = self._download(message)
media_type, media_mime, media_path, media_size = self._download(message)
# TODO: Insert a list of accepted file types here.
if media_post:
num = self._add_to_db(message, media_post=media_post, media_type=media_mime, media_url=media_path, media_size=media_size)
num = self._add_to_db(message, media_type=media_mime, media_path=media_path, media_size=media_size)
media_type = media_type.replace("images", "image") # linguistic hack!
if 'pdf' in message.url:
media_type = 'PDF' # linguistic hack!
@ -260,7 +253,7 @@ class Logbot(Bot):
try:
os.rename(current_folder_name_path, new_folder_name_path)
self.db[message.room]["folder"] = new_folder_name
reply = f"The foldername of the log is changed to: { new_folder_name }. The URL of the log changed into: https://vvvvvvaria.org/logs/{ new_folder_name}"
reply = f"The foldername of the log is changed to: { new_folder_name }. The URL of the log changed into: { self.baseurl }{ new_folder_name}"
except:
reply = f"Sorry i couldn't shange that foldername into: '{ new_folder_name }'. Try again with: 'logbot @folder newname'."

Loading…
Cancel
Save