Browse Source

Let my auto-formatter do its thing

master
decentral1se 5 months ago
parent
commit
3fea24c46b
No known key found for this signature in database GPG Key ID: 92DAD76BD9567B8A
  1. 47
      LogBot/logbot.py

47
LogBot/logbot.py

@ -2,11 +2,13 @@ import os
import re
import shutil
import urllib.request
from urllib.parse import urlparse
from datetime import datetime
from urllib.parse import urlparse
import jinja2
from xbotlib import Bot
class Logbot(Bot):
help = """Oh dear, logbot is here!
@ -89,9 +91,7 @@ class Logbot(Bot):
if message.url.lower().endswith(self.IMAGE_TYPES):
media_post = f'<img src="{ media_path }">'
elif message.url.lower().endswith(self.FILE_TYPES):
media_post = (
f'<iframe src="{ media_path }"></iframe>'
)
media_post = f'<iframe src="{ media_path }"></iframe>'
elif message.url.lower().endswith(self.AUDIO_TYPES):
media_post = f'<audio controls src="{ media_path }"></audio>'
elif message.url.lower().endswith(self.VIDEO_TYPES):
@ -105,30 +105,32 @@ class Logbot(Bot):
"""Write new log to the file system."""
template = jinja2.Template(open("template.html").read())
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)
log_path = os.path.join(self.output, folder_name, "index.html")
with open(log_path, "w") as out:
html = template.render(
title=self.db[message.room]["title"],
db=self.db[message.room]["messages"]
db=self.db[message.room]["messages"],
)
out.write(html)
self.log.info(f"writing to: { log_path }")
def _generate_feed(self, message):
template = jinja2.Template(open("feed.rss").read())
folder_name = self.db[message.room]["folder"]
if "@" in folder_name: # hacky
folder_name = self.db[message.room]["folder"]
if "@" in folder_name: # hacky
folder_name = self._parse_room_name(folder_name)
feed_path = os.path.join(self.output, folder_name, "feed.rss")
date = datetime.now()
with open(feed_path, "w") as out:
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"],
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)
self.log.info(f"writing to: { feed_path }")
@ -167,15 +169,17 @@ class Logbot(Bot):
if "folder" not in self.db[room]:
self.db[room]["folder"] = self._parse_room_name(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]:
self.db[room]["font"] = 'none' # default font
self.db[room]["font"] = "none" # default font
self.db._dumps()
self.log.info(f"Added to the database: { room }")
if not os.path.exists(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")
shutil.copy(stylesheet_path, stylesheet_dest_path)
self.log.info(f"Created a folder for: { room }")
@ -244,16 +248,16 @@ class Logbot(Bot):
match = re.findall("@folder .*", message.content)[0]
new_folder_name = match.replace("@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_path = os.path.join(self.output, current_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)
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}"
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
elif "@style" in message.text:
@ -263,13 +267,15 @@ class Logbot(Bot):
self.db[message.room]["stylesheet"] = stylesheet
room_name = self._parse_room_name(message.room)
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)
stylesheet_path = os.path.join("stylesheets", f"{ stylesheet }.css")
stylesheet_dest_path = os.path.join(room_path, "stylesheet.css")
try:
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 }."
except:
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
room_name = self._parse_room_name(message.room)
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)
font_path = os.path.join("fonts", f"{ font }.ttf")
font_dest_path = os.path.join(room_path, "font.ttf")
if font == 'none':
if font == "none":
os.remove(font_dest_path)
reply = "I removed the font and switched back to default serif."
else:
@ -309,4 +315,5 @@ class Logbot(Bot):
# Reply to the groupchat
self.reply(reply, room=message.room)
Logbot()

Loading…
Cancel
Save