Let my auto-formatter do its thing

This commit is contained in:
decentral1se 2021-06-01 13:18:02 +02:00
parent 52a26d1646
commit 3fea24c46b
No known key found for this signature in database
GPG Key ID: 92DAD76BD9567B8A

View File

@ -2,11 +2,13 @@ import os
import re import re
import shutil import shutil
import urllib.request import urllib.request
from urllib.parse import urlparse
from datetime import datetime from datetime import datetime
from urllib.parse import urlparse
import jinja2 import jinja2
from xbotlib import Bot from xbotlib import Bot
class Logbot(Bot): class Logbot(Bot):
help = """Oh dear, logbot is here! help = """Oh dear, logbot is here!
@ -89,9 +91,7 @@ class Logbot(Bot):
if message.url.lower().endswith(self.IMAGE_TYPES): if message.url.lower().endswith(self.IMAGE_TYPES):
media_post = f'<img src="{ media_path }">' media_post = f'<img src="{ media_path }">'
elif message.url.lower().endswith(self.FILE_TYPES): elif message.url.lower().endswith(self.FILE_TYPES):
media_post = ( media_post = f'<iframe src="{ media_path }"></iframe>'
f'<iframe src="{ media_path }"></iframe>'
)
elif message.url.lower().endswith(self.AUDIO_TYPES): elif message.url.lower().endswith(self.AUDIO_TYPES):
media_post = f'<audio controls src="{ media_path }"></audio>' media_post = f'<audio controls src="{ media_path }"></audio>'
elif message.url.lower().endswith(self.VIDEO_TYPES): elif message.url.lower().endswith(self.VIDEO_TYPES):
@ -105,30 +105,32 @@ class Logbot(Bot):
"""Write new log to the file system.""" """Write new log to the file system."""
template = jinja2.Template(open("template.html").read()) template = jinja2.Template(open("template.html").read())
folder_name = self.db[message.room]["folder"] folder_name = self.db[message.room]["folder"]
if "@" in folder_name: # hacky if "@" in folder_name: # hacky
folder_name = self._parse_room_name(folder_name) folder_name = self._parse_room_name(folder_name)
log_path = os.path.join(self.output, folder_name, "index.html") log_path = os.path.join(self.output, folder_name, "index.html")
with open(log_path, "w") as out: with open(log_path, "w") as out:
html = template.render( html = template.render(
title=self.db[message.room]["title"], title=self.db[message.room]["title"],
db=self.db[message.room]["messages"] db=self.db[message.room]["messages"],
) )
out.write(html) out.write(html)
self.log.info(f"writing to: { log_path }") self.log.info(f"writing to: { log_path }")
def _generate_feed(self, message): def _generate_feed(self, message):
template = jinja2.Template(open("feed.rss").read()) template = jinja2.Template(open("feed.rss").read())
folder_name = self.db[message.room]["folder"] folder_name = self.db[message.room]["folder"]
if "@" in folder_name: # hacky if "@" in folder_name: # hacky
folder_name = self._parse_room_name(folder_name) folder_name = self._parse_room_name(folder_name)
feed_path = os.path.join(self.output, folder_name, "feed.rss") feed_path = os.path.join(self.output, folder_name, "feed.rss")
date = datetime.now() date = datetime.now()
with open(feed_path, "w") as out: with open(feed_path, "w") as out:
feed = template.render( feed = template.render(
log_path=os.path.join("https://vvvvvvaria.org/logs/", folder_name, "index.html"), # hard-coding the URL for now log_path=os.path.join(
"https://vvvvvvaria.org/logs/", folder_name, "index.html"
), # hard-coding the URL for now
title=self.db[message.room]["title"], title=self.db[message.room]["title"],
db=self.db[message.room], db=self.db[message.room],
date=date.strftime("%A, %d. %B %Y %I:%M%p") date=date.strftime("%A, %d. %B %Y %I:%M%p"),
) )
out.write(feed) out.write(feed)
self.log.info(f"writing to: { feed_path }") self.log.info(f"writing to: { feed_path }")
@ -167,15 +169,17 @@ class Logbot(Bot):
if "folder" not in self.db[room]: if "folder" not in self.db[room]:
self.db[room]["folder"] = self._parse_room_name(room) self.db[room]["folder"] = self._parse_room_name(room)
if "stylesheet" not in self.db[room]: if "stylesheet" not in self.db[room]:
self.db[room]["stylesheet"] = "timeline" # default stylesheet self.db[room]["stylesheet"] = "timeline" # default stylesheet
if "font" not in self.db[room]: if "font" not in self.db[room]:
self.db[room]["font"] = 'none' # default font self.db[room]["font"] = "none" # default font
self.db._dumps() self.db._dumps()
self.log.info(f"Added to the database: { room }") self.log.info(f"Added to the database: { room }")
if not os.path.exists(room_path): if not os.path.exists(room_path):
os.mkdir(room_path) os.mkdir(room_path)
stylesheet_path = os.path.join("stylesheets", self.db[room]["stylesheet"] + ".css") stylesheet_path = os.path.join(
"stylesheets", self.db[room]["stylesheet"] + ".css"
)
stylesheet_dest_path = os.path.join(room_path, "stylesheet.css") stylesheet_dest_path = os.path.join(room_path, "stylesheet.css")
shutil.copy(stylesheet_path, stylesheet_dest_path) shutil.copy(stylesheet_path, stylesheet_dest_path)
self.log.info(f"Created a folder for: { room }") self.log.info(f"Created a folder for: { room }")
@ -244,16 +248,16 @@ class Logbot(Bot):
match = re.findall("@folder .*", message.content)[0] match = re.findall("@folder .*", message.content)[0]
new_folder_name = match.replace("@folder ", "") new_folder_name = match.replace("@folder ", "")
current_folder_name = self.db[message.room]["folder"] current_folder_name = self.db[message.room]["folder"]
if "@" in current_folder_name: # hacky if "@" in current_folder_name: # hacky
current_folder_name = self._parse_room_name(current_folder_name) current_folder_name = self._parse_room_name(current_folder_name)
current_folder_name_path = os.path.join(self.output, current_folder_name) current_folder_name_path = os.path.join(self.output, current_folder_name)
new_folder_name_path = os.path.join(self.output, new_folder_name) new_folder_name_path = os.path.join(self.output, new_folder_name)
try: try:
os.rename(current_folder_name_path, new_folder_name_path) os.rename(current_folder_name_path, new_folder_name_path)
self.db[message.room]["folder"] = new_folder_name 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: https://vvvvvvaria.org/logs/{ new_folder_name}"
except: except:
reply = f"Sorry i couldn\'t shange that foldername into: '{ new_folder_name }'. Try again with: 'logbot @folder newname'." reply = f"Sorry i couldn't shange that foldername into: '{ new_folder_name }'. Try again with: 'logbot @folder newname'."
# Response to @style # Response to @style
elif "@style" in message.text: elif "@style" in message.text:
@ -263,13 +267,15 @@ class Logbot(Bot):
self.db[message.room]["stylesheet"] = stylesheet self.db[message.room]["stylesheet"] = stylesheet
room_name = self._parse_room_name(message.room) room_name = self._parse_room_name(message.room)
room_path = os.path.join(self.output, self.db[message.room]["folder"]) room_path = os.path.join(self.output, self.db[message.room]["folder"])
if "@" in room_path: # hacky if "@" in room_path: # hacky
room_path = self._parse_room_name(room_path) room_path = self._parse_room_name(room_path)
stylesheet_path = os.path.join("stylesheets", f"{ stylesheet }.css") stylesheet_path = os.path.join("stylesheets", f"{ stylesheet }.css")
stylesheet_dest_path = os.path.join(room_path, "stylesheet.css") stylesheet_dest_path = os.path.join(room_path, "stylesheet.css")
try: try:
shutil.copy(stylesheet_path, stylesheet_dest_path) shutil.copy(stylesheet_path, stylesheet_dest_path)
self.log.info(f"Stylesheet in room { room_name } switched to: { stylesheet }") self.log.info(
f"Stylesheet in room { room_name } switched to: { stylesheet }"
)
reply = f"I'm switching the stylesheet of this log to: { stylesheet }." reply = f"I'm switching the stylesheet of this log to: { stylesheet }."
except: except:
reply = f"The stylesheet '{ stylesheet }' is unknown to me. Check @help to see the available stylesheets." reply = f"The stylesheet '{ stylesheet }' is unknown to me. Check @help to see the available stylesheets."
@ -282,11 +288,11 @@ class Logbot(Bot):
self.db[message.room]["font"] = font self.db[message.room]["font"] = font
room_name = self._parse_room_name(message.room) room_name = self._parse_room_name(message.room)
room_path = os.path.join(self.output, self.db[message.room]["folder"]) room_path = os.path.join(self.output, self.db[message.room]["folder"])
if "@" in room_path: # hacky if "@" in room_path: # hacky
room_path = self._parse_room_name(room_path) room_path = self._parse_room_name(room_path)
font_path = os.path.join("fonts", f"{ font }.ttf") font_path = os.path.join("fonts", f"{ font }.ttf")
font_dest_path = os.path.join(room_path, "font.ttf") font_dest_path = os.path.join(room_path, "font.ttf")
if font == 'none': if font == "none":
os.remove(font_dest_path) os.remove(font_dest_path)
reply = "I removed the font and switched back to default serif." reply = "I removed the font and switched back to default serif."
else: else:
@ -309,4 +315,5 @@ 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()