2021-02-27 10:33:45 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
2021-01-30 23:15:36 +01:00
|
|
|
import urllib.request
|
|
|
|
from urllib.parse import urlparse
|
2021-03-07 11:36:03 +01:00
|
|
|
from datetime import datetime
|
2021-02-27 10:33:45 +01:00
|
|
|
import jinja2
|
|
|
|
from xbotlib import Bot
|
|
|
|
|
2021-02-27 10:46:40 +01:00
|
|
|
class Logbot(Bot):
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2021-02-27 10:33:45 +01:00
|
|
|
help = """Oh dear, logbot is here!
|
2021-02-27 11:38:28 +01:00
|
|
|
(You can speak to the bot using @logbot or logbot:)
|
|
|
|
|
|
|
|
<image>: Your image is added to the log.
|
|
|
|
|
|
|
|
logbot @help: Print this message
|
|
|
|
|
|
|
|
logbot @add <message>: Add a message to the log.
|
|
|
|
|
|
|
|
logbot @delete <num>: Delete posts from the log. For example: @logbot @delete 5
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
logbot @title <string>: Set the title of your log.
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2021-04-23 16:52:55 +02:00
|
|
|
logbot @style <stylesheet>: Switch to another stylesheet. For example: logbot @style log. Available stylesheets include: timeline, float, opentab.
|
2021-03-12 12:10:01 +01:00
|
|
|
|
2021-03-12 21:19:40 +01:00
|
|
|
logbot @font <font>: Switch to another font. For example: logbot @font font. Available fonts include: polsku, notcouriersans; or select None to switch back to default serif.
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
logbot @uptime: To check how long @logbot has been around
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
@bots: To see who is around :)
|
|
|
|
""" # noqa
|
2021-01-30 17:36:59 +01:00
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
IMAGE_TYPES = (".jpg", "jpeg", "png", ".gif", ".bmp", ".svg", "eps")
|
|
|
|
AUDIO_TYPES = (".mp3", ".ogg", ".oga", ".mogg", ".wav", ".m4a", ".webm")
|
|
|
|
FILE_TYPES = ".pdf"
|
|
|
|
VIDEO_TYPES = (
|
|
|
|
".mp4",
|
|
|
|
".webm",
|
|
|
|
".flv",
|
|
|
|
".vob",
|
|
|
|
".avi",
|
|
|
|
".mov",
|
|
|
|
".qt",
|
|
|
|
".mpg",
|
|
|
|
".mpeg",
|
|
|
|
".mp4",
|
|
|
|
".m2v",
|
|
|
|
".mpe",
|
|
|
|
".3gp",
|
|
|
|
)
|
2021-01-30 17:36:59 +01:00
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
def _download(self, message):
|
2021-03-07 10:50:48 +01:00
|
|
|
"""Download media files."""
|
2021-02-27 11:38:28 +01:00
|
|
|
# define media_type
|
|
|
|
if message.url.lower().endswith(self.IMAGE_TYPES):
|
|
|
|
media_type = "images"
|
|
|
|
elif message.url.lower().endswith(self.FILE_TYPES):
|
|
|
|
media_type = "pdf"
|
|
|
|
elif message.url.lower().endswith(self.AUDIO_TYPES):
|
|
|
|
media_type = "audio"
|
|
|
|
elif message.url.lower().endswith(self.VIDEO_TYPES):
|
|
|
|
media_type = "video"
|
|
|
|
else:
|
|
|
|
media_type = None
|
2021-03-07 10:51:43 +01:00
|
|
|
self.log.info(f"Unable to determine media type of { message.url.lower() }")
|
2021-02-27 11:38:28 +01:00
|
|
|
|
|
|
|
# download file
|
|
|
|
data = urllib.request.urlopen(message.url).read()
|
|
|
|
if data:
|
2021-03-07 10:51:43 +01:00
|
|
|
self.log.info(f"downloading: { message.url }")
|
2021-02-27 11:38:28 +01:00
|
|
|
parsed_url = urlparse(message.url)
|
|
|
|
filename = (
|
|
|
|
os.path.basename(parsed_url.path).replace(" ", "_").replace("%20", "_")
|
|
|
|
) # safe url's
|
2021-03-07 10:51:43 +01:00
|
|
|
self.log.info(f"as the file: { filename }")
|
2021-02-27 11:38:28 +01:00
|
|
|
roomname = re.sub(r"@.*", "", message.room)
|
|
|
|
path = os.path.join(self.output, roomname, media_type)
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
os.mkdir(path)
|
|
|
|
file_path = os.path.join(path, filename)
|
|
|
|
with open(file_path, "wb") as media_file:
|
|
|
|
media_file.write(data)
|
|
|
|
|
|
|
|
# define media_post
|
|
|
|
media_path = os.path.join(media_type, filename)
|
|
|
|
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 = (
|
2021-03-12 21:20:14 +01:00
|
|
|
f'<iframe src="{ media_path }"></iframe>'
|
2021-02-27 11:38:28 +01:00
|
|
|
)
|
|
|
|
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):
|
|
|
|
media_post = f'<video controls src="{ media_path }"></video>'
|
|
|
|
else:
|
|
|
|
media_post = None
|
|
|
|
|
|
|
|
return media_post, media_type
|
|
|
|
|
|
|
|
def _write_log(self, message):
|
|
|
|
"""Write new log to the file system."""
|
2021-02-27 20:20:19 +01:00
|
|
|
template = jinja2.Template(open("template.html").read())
|
2021-02-27 11:38:28 +01:00
|
|
|
room_name = self._parse_room_name(message.room)
|
2021-03-07 11:49:55 +01:00
|
|
|
log_path = os.path.join(self.output, room_name, "index.html")
|
2021-02-27 11:38:28 +01:00
|
|
|
with open(log_path, "w") as out:
|
2021-02-27 20:20:19 +01:00
|
|
|
html = template.render(
|
2021-02-27 11:38:28 +01:00
|
|
|
title=self.db[message.room]["title"],
|
2021-03-07 11:36:03 +01:00
|
|
|
db=self.db[message.room]["messages"]
|
2021-02-27 11:38:28 +01:00
|
|
|
)
|
|
|
|
out.write(html)
|
2021-03-07 11:36:03 +01:00
|
|
|
self.log.info(f"writing to: { log_path }")
|
|
|
|
|
|
|
|
def _generate_feed(self, message):
|
|
|
|
template = jinja2.Template(open("feed.rss").read())
|
|
|
|
room_name = self._parse_room_name(message.room)
|
|
|
|
feed_path = os.path.join(self.output, room_name, "feed.rss")
|
|
|
|
date = datetime.now()
|
|
|
|
with open(feed_path, "w") as out:
|
|
|
|
feed = template.render(
|
2021-03-12 12:10:01 +01:00
|
|
|
log_path=os.path.join("https://vvvvvvaria.org/logs/", room_name, "index.html"), # hard-coding the URL for now
|
2021-03-07 11:36:03 +01:00
|
|
|
title=self.db[message.room]["title"],
|
|
|
|
db=self.db[message.room],
|
|
|
|
date=date.strftime("%A, %d. %B %Y %I:%M%p")
|
|
|
|
)
|
|
|
|
out.write(feed)
|
|
|
|
self.log.info(f"writing to: { feed_path }")
|
2021-02-27 11:38:28 +01:00
|
|
|
|
|
|
|
def _add_to_db(self, message, media_post=None):
|
|
|
|
"""Save new entry to database."""
|
|
|
|
keys = [x for x in self.db[message.room]["messages"].keys()]
|
|
|
|
keys.sort(key=int)
|
|
|
|
if not keys:
|
|
|
|
new_key = "0"
|
|
|
|
else:
|
|
|
|
new_key = str(int(keys[-1]) + 1)
|
|
|
|
if media_post:
|
|
|
|
self.db[message.room]["messages"][new_key] = media_post
|
|
|
|
else:
|
2021-03-07 11:36:03 +01:00
|
|
|
post = message.content.replace("@add ", "")
|
|
|
|
self.db[message.room]["messages"][new_key] = post
|
2021-03-07 10:52:33 +01:00
|
|
|
self.db._dumps()
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-02-27 11:30:44 +01:00
|
|
|
def _parse_room_name(self, room):
|
|
|
|
"""Parse room name from entire address string."""
|
|
|
|
return str(re.match(r".*@", room).group()).replace("@", "")
|
|
|
|
|
|
|
|
def _setup_room(self, room):
|
|
|
|
"""Create directories and database entries for a new room."""
|
|
|
|
room_name = self._parse_room_name(room)
|
|
|
|
room_path = os.path.join(self.output, room_name)
|
|
|
|
self.log.info(f"Processing setup logic for: {room_path}")
|
|
|
|
|
2021-02-27 20:15:17 +01:00
|
|
|
if room not in self.db:
|
2021-02-27 11:30:44 +01:00
|
|
|
self.db[room] = {}
|
2021-02-27 20:11:24 +01:00
|
|
|
if "messages" not in self.db[room]:
|
2021-02-27 11:30:44 +01:00
|
|
|
self.db[room]["messages"] = {}
|
2021-02-27 20:11:24 +01:00
|
|
|
if "title" not in self.db[room]:
|
2021-02-27 11:30:44 +01:00
|
|
|
self.db[room]["title"] = room
|
2021-03-12 12:10:01 +01:00
|
|
|
if "stylesheet" not in self.db[room]:
|
|
|
|
self.db[room]["stylesheet"] = room
|
|
|
|
if "font" not in self.db[room]:
|
|
|
|
self.db[room]["font"] = room
|
2021-02-27 20:15:28 +01:00
|
|
|
self.db._dumps()
|
2021-02-27 20:11:24 +01:00
|
|
|
self.log.info(f"Added to the database: { room }")
|
2021-02-27 11:30:44 +01:00
|
|
|
|
|
|
|
if not os.path.exists(room_path):
|
|
|
|
os.mkdir(room_path)
|
2021-03-12 12:10:01 +01:00
|
|
|
stylesheet_path = os.path.join("stylesheets","linear.css") # default stylesheet
|
|
|
|
stylesheet_dest_path = os.path.join(room_path,"stylesheet.css")
|
|
|
|
shutil.copy(stylesheet_path, stylesheet_dest_path)
|
2021-02-27 11:30:44 +01:00
|
|
|
self.log.info(f"Created a folder for: { room }")
|
2021-03-12 12:10:01 +01:00
|
|
|
self.log.info(f"Added stylesheet.css to: { room }")
|
2021-02-27 11:30:44 +01:00
|
|
|
|
2021-02-27 10:33:45 +01:00
|
|
|
def setup(self):
|
2021-03-07 10:50:48 +01:00
|
|
|
"""Setup a log for all the rooms LogBot is subscribed to."""
|
2021-02-27 10:45:19 +01:00
|
|
|
self.log.info(f"Output folder is set to: { self.output }")
|
2021-02-27 10:33:45 +01:00
|
|
|
for room in self.rooms:
|
2021-02-27 11:30:44 +01:00
|
|
|
self._setup_room(room)
|
2021-02-27 20:07:14 +01:00
|
|
|
for room in self._data["invited"]:
|
|
|
|
self._setup_room(room)
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-02-27 11:30:44 +01:00
|
|
|
def group_invite(self, message):
|
|
|
|
"""Extend xbotlib invite response logic and do required room setup."""
|
|
|
|
super().group_invite(message)
|
2021-02-27 11:46:53 +01:00
|
|
|
self._setup_room(str(message["from"]))
|
2021-02-27 10:33:45 +01:00
|
|
|
|
|
|
|
def group(self, message):
|
2021-03-07 10:50:48 +01:00
|
|
|
"""All the things LogBot does when it receives a group message."""
|
2021-02-27 10:45:19 +01:00
|
|
|
self.log.info("------------------")
|
2021-03-07 10:51:43 +01:00
|
|
|
self.log.info(f"message: { message.text }")
|
|
|
|
self.log.info(f"room: { message.room }")
|
|
|
|
self.log.info(f"sender: { message.sender }")
|
2021-02-27 11:56:35 +01:00
|
|
|
self.log.info("------------------")
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to files: image / PDF / audio / video
|
2021-02-27 10:33:45 +01:00
|
|
|
if message.url:
|
2021-02-27 11:50:05 +01:00
|
|
|
media_post, media_type = self._download(message)
|
2021-02-27 10:33:45 +01:00
|
|
|
if media_post:
|
2021-02-27 11:38:28 +01:00
|
|
|
self._add_to_db(message, media_post=media_post)
|
2021-02-27 10:33:45 +01:00
|
|
|
media_type = media_type.replace("images", "image") # linguistic hack!
|
|
|
|
reply = f"Thanks for that { media_type }!"
|
|
|
|
else:
|
2021-02-27 11:08:00 +01:00
|
|
|
reply = "Sorry, can't process that :( (unknown media type?)"
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to @add
|
2021-02-27 10:33:45 +01:00
|
|
|
elif "@add" in message.text:
|
2021-02-27 11:38:28 +01:00
|
|
|
self._add_to_db(message)
|
2021-02-27 10:33:45 +01:00
|
|
|
reply = "Added, thanks!"
|
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to @delete
|
2021-02-27 10:33:45 +01:00
|
|
|
elif "@delete" in message.text:
|
2021-02-27 10:47:16 +01:00
|
|
|
match = re.findall(r"@delete \d*", message.content)[0]
|
2021-02-27 10:33:45 +01:00
|
|
|
key = str(match.replace("@delete ", ""))
|
|
|
|
|
|
|
|
if key in self.db[message.room]["messages"]:
|
2021-02-27 11:57:19 +01:00
|
|
|
self.log.info(
|
2021-03-07 10:51:43 +01:00
|
|
|
f"To be deleted: { self.db[message.room]['messages'][key] }"
|
2021-02-27 11:57:19 +01:00
|
|
|
)
|
2021-02-27 10:33:45 +01:00
|
|
|
reply = f"This message is deleted: { self.db[message.room]['messages'][key] }"
|
2021-02-27 11:38:28 +01:00
|
|
|
del self.db[message.room]["messages"][key]
|
2021-02-27 10:33:45 +01:00
|
|
|
else:
|
|
|
|
reply = "This message is already gone!"
|
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to @title
|
2021-02-27 10:33:45 +01:00
|
|
|
elif "@title" in message.text:
|
|
|
|
match = re.findall("@title .*", message.content)[0]
|
2021-03-12 12:10:01 +01:00
|
|
|
title = match.replace("@title ", "")
|
2021-02-27 10:33:45 +01:00
|
|
|
self.db[message.room]["title"] = title
|
|
|
|
reply = f"The title of the log is changed to: { title }"
|
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to @style
|
2021-02-27 10:33:45 +01:00
|
|
|
elif "@style" in message.text:
|
2021-03-12 12:10:01 +01:00
|
|
|
match = re.findall("@style .*", message.content)[0]
|
|
|
|
stylesheet = match.replace("@style ", "")
|
|
|
|
stylesheet = stylesheet.lower()
|
|
|
|
self.db[message.room]["stylesheet"] = stylesheet
|
|
|
|
room_name = self._parse_room_name(message.room)
|
|
|
|
room_path = os.path.join(self.output, room_name)
|
|
|
|
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 }")
|
|
|
|
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."
|
|
|
|
|
|
|
|
# Response to @font
|
|
|
|
elif "@font" in message.text:
|
|
|
|
match = re.findall("@font .*", message.content)[0]
|
|
|
|
font = match.replace("@font ", "")
|
|
|
|
font = font.lower()
|
|
|
|
self.db[message.room]["font"] = font
|
|
|
|
room_name = self._parse_room_name(message.room)
|
|
|
|
room_path = os.path.join(self.output, room_name)
|
|
|
|
font_path = os.path.join("fonts", f"{ font }.ttf")
|
|
|
|
font_dest_path = os.path.join(room_path, "font.ttf")
|
|
|
|
if font == 'none':
|
|
|
|
os.remove(font_dest_path)
|
|
|
|
reply = "I removed the font and switched back to default serif."
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
shutil.copy(font_path, font_dest_path)
|
|
|
|
self.log.info(f"font in room { room_name } switched to: { font }")
|
|
|
|
reply = f"I'm switching the font of this log to: { font }."
|
|
|
|
except:
|
|
|
|
reply = f"The font '{ font }' is unknown to me. Check @help to see the available fonts."
|
2021-02-27 10:33:45 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
reply = "Hmm ... not sure what you want to do?"
|
|
|
|
|
2021-03-07 10:56:47 +01:00
|
|
|
# Regenerate the log webpage
|
2021-02-27 11:50:54 +01:00
|
|
|
self._write_log(message)
|
2021-03-07 10:56:47 +01:00
|
|
|
|
2021-03-07 11:36:03 +01:00
|
|
|
# Regenerate the RSS feed
|
|
|
|
self._generate_feed(message)
|
|
|
|
|
2021-03-07 10:56:47 +01:00
|
|
|
# Reply to the groupchat
|
2021-02-27 10:46:10 +01:00
|
|
|
self.reply(reply, room=message.room)
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2021-02-27 10:46:40 +01:00
|
|
|
Logbot()
|