Browse Source

Auto-formatter has something to say here

master
Luke Murphy 4 years ago
parent
commit
b359d92021
No known key found for this signature in database GPG Key ID: 5E2EF5A63E3718CC
  1. 10
      LogBot/README.md
  2. 181
      LogBot/logbot.py
  3. 10
      LogBot/stylesheet.css

10
LogBot/README.md

@ -4,7 +4,7 @@ A small XMPP bot written in Python that logs XMPP conversations into a HTML page
The bot is used in group chats, where it includes all images that are send to the group and all messages that include `@logbot` or `logbot` or `logbot:`.
*work-in-progress*
_work-in-progress_
## How to use LogBot?
@ -24,10 +24,10 @@ logbot @uptime: To check how long @logbot has been around
## Situated tails
* Archive bot, Relearn 2017, <https://gitlab.com/relearn/relearn2017/-/tree/master/xmpp-bots/archive-bot>
* Streambot, Varia website extension 2017-2018, <https://git.vvvvvvaria.org/varia/xmpp.streambot>
* Logbot v1, Varia XMPP extension 2017-2020, <https://git.vvvvvvaria.org/varia/bots/src/branch/master/logbot>
- Archive bot, Relearn 2017, <https://gitlab.com/relearn/relearn2017/-/tree/master/xmpp-bots/archive-bot>
- Streambot, Varia website extension 2017-2018, <https://git.vvvvvvaria.org/varia/xmpp.streambot>
- Logbot v1, Varia XMPP extension 2017-2020, <https://git.vvvvvvaria.org/varia/bots/src/branch/master/logbot>
## Requirements
* [xbotlib](https://git.vvvvvvaria.org/decentral1se/xbotlib)
- [xbotlib](https://git.vvvvvvaria.org/decentral1se/xbotlib)

181
LogBot/logbot.py

@ -1,81 +1,134 @@
from xbotlib import Bot
import jinja2
import os, re, json, shutil
from os import environ
import json
import os
import re
import shutil
import urllib.request
from os import environ
from urllib.parse import urlparse
import jinja2
from xbotlib import Bot
def add_to_db(self, message, media_post=None):
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)
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
self.db[message.room]["messages"][new_key] = media_post
else:
self.db[message.room]['messages'][new_key] = message.content.replace('@add','')
self.db[message.room]["messages"][new_key] = message.content.replace("@add", "")
def del_from_db(self, message, key):
del self.db[message.room]['messages'][key]
del self.db[message.room]["messages"][key]
def write_log(self, message):
template = jinja2.Template(open('template.html').read())
roomname = re.sub(r'@.*', '', message.room)
log_path = os.path.join(self.output, roomname, 'index.html')
with open(log_path,'w') as out:
html = template.render(title=self.db[message.room]['title'], db=self.db[message.room]['messages'])
template = jinja2.Template(open("template.html").read())
roomname = re.sub(r"@.*", "", message.room)
log_path = os.path.join(self.output, roomname, "index.html")
with open(log_path, "w") as out:
html = template.render(
title=self.db[message.room]["title"], db=self.db[message.room]["messages"]
)
out.write(html)
print('writing to: ', log_path)
print("writing to: ", log_path)
def download(self, message):
# define media_type
if message.url.lower().endswith(('.jpg','jpeg','png','.gif','.bmp','.svg','eps')):
media_type = 'images'
elif message.url.lower().endswith('.pdf'):
media_type = 'pdf'
elif message.url.lower().endswith(('.mp3','.ogg','.oga','.mogg','.wav','.m4a','.webm')):
media_type = 'audio'
elif message.url.lower().endswith(('.mp4','.webm','.flv','.vob','.avi','.mov','.qt','.mpg','.mpeg','.mp4','.m2v','.mpe','.3gp')):
media_type = 'video'
if message.url.lower().endswith(
(".jpg", "jpeg", "png", ".gif", ".bmp", ".svg", "eps")
):
media_type = "images"
elif message.url.lower().endswith(".pdf"):
media_type = "pdf"
elif message.url.lower().endswith(
(".mp3", ".ogg", ".oga", ".mogg", ".wav", ".m4a", ".webm")
):
media_type = "audio"
elif message.url.lower().endswith(
(
".mp4",
".webm",
".flv",
".vob",
".avi",
".mov",
".qt",
".mpg",
".mpeg",
".mp4",
".m2v",
".mpe",
".3gp",
)
):
media_type = "video"
else:
media_type = None
# download file
data = urllib.request.urlopen(message.url).read()
if data:
print('downloading: ', message.url)
print("downloading: ", message.url)
parsed_url = urlparse(message.url)
filename = os.path.basename(parsed_url.path).replace(' ','_').replace('%20','_') # safe url's
print('as the file: ', filename)
roomname = re.sub(r'@.*', '', message.room)
filename = (
os.path.basename(parsed_url.path).replace(" ", "_").replace("%20", "_")
) # safe url's
print("as the file: ", filename)
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:
with open(file_path, "wb") as media_file:
media_file.write(data)
media_file.close()
# define media_post
media_path = os.path.join(media_type, filename)
if message.url.lower().endswith(('.jpg','jpeg','png','.gif','.bmp','.svg','eps')):
if message.url.lower().endswith(
(".jpg", "jpeg", "png", ".gif", ".bmp", ".svg", "eps")
):
media_post = f'<img src="{ media_path }">'
elif message.url.lower().endswith('.pdf'):
elif message.url.lower().endswith(".pdf"):
media_post = f'<iframe src="{ media_path }" width="800" height="1000"></iframe>'
elif message.url.lower().endswith(('.mp3','.ogg','.oga','.mogg','.wav','.m4a','.webm')):
elif message.url.lower().endswith(
(".mp3", ".ogg", ".oga", ".mogg", ".wav", ".m4a", ".webm")
):
media_post = f'<audio controls src="{ media_path }"></audio>'
elif message.url.lower().endswith(('.mp4','.webm','.flv','.vob','.avi','.mov','.qt','.mpg','.mpeg','.mp4','.m2v','.mpe','.3gp')):
elif message.url.lower().endswith(
(
".mp4",
".webm",
".flv",
".vob",
".avi",
".mov",
".qt",
".mpg",
".mpeg",
".mp4",
".m2v",
".mpe",
".3gp",
)
):
media_post = f'<video controls src="{ media_path }"></video>'
else:
media_post = None
return media_post, media_type
class logbot(Bot):
help = '''Oh dear, logbot is here!
help = """Oh dear, logbot is here!
(You can speak to the bot using @logbot or logbot:)
@ -94,76 +147,78 @@ logbot @style <element> <css-rule>: Edit the css of your log. For example: logbo
logbot @uptime: To check how long @logbot has been around
@bots: To see who is around :)
'''
"""
def setup(self):
print('INFO ', f'Output folder is set to: { self.output }')
print("INFO ", f"Output folder is set to: { self.output }")
for room in self.rooms:
roomname = str(re.match(r'.*@', room).group()).replace('@','')
roomname = str(re.match(r".*@", room).group()).replace("@", "")
self.room_path = os.path.join(self.output, roomname)
print('INFO ', f'Room path is set to: { self.room_path }')
print("INFO ", f"Room path is set to: { self.room_path }")
# Check if the room is in the database already
if not room in self.db.keys():
self.db[room] = {}
self.db[room]['messages'] = {}
self.db[room]['title'] = room
print('INFO ', f'Added to the database: { room }')
self.db[room]["messages"] = {}
self.db[room]["title"] = room
print("INFO ", f"Added to the database: { room }")
# Check if the room has an output folder already
if not os.path.exists(self.room_path):
os.mkdir(self.room_path)
shutil.copy('stylesheet.css', self.room_path)
print('INFO ', f'Created a folder for: { room }')
print('INFO ', f'Copied stylesheet.css to: { room }')
shutil.copy("stylesheet.css", self.room_path)
print("INFO ", f"Created a folder for: { room }")
print("INFO ", f"Copied stylesheet.css to: { room }")
def group(self, message):
# to debug in the terminal
print('------------------')
print('message: ', message.text)
print('room: ', message.room)
print('sender: ', message.sender)
print("------------------")
print("message: ", message.text)
print("room: ", message.room)
print("sender: ", message.sender)
# image / PDF / audio / video
if message.url:
media_post, media_type = download(self, message)
if media_post:
add_to_db(self, message, media_post=media_post)
media_type = media_type.replace('images', 'image') # linguistic hack!
reply = f'Thanks for that { media_type }!'
media_type = media_type.replace("images", "image") # linguistic hack!
reply = f"Thanks for that { media_type }!"
else:
reply = 'Sorry, can\'t process that :('
reply = "Sorry, can't process that :("
elif '@add' in message.text:
elif "@add" in message.text:
add_to_db(self, message)
reply = 'Added, thanks!'
reply = "Added, thanks!"
elif '@delete' in message.text:
elif "@delete" in message.text:
match = re.findall("@delete \d*", message.content)[0]
key = str(match.replace('@delete ',''))
key = str(match.replace("@delete ", ""))
if key in self.db[message.room]['messages']:
print('To be deleted:', self.db[message.room]['messages'][key])
if key in self.db[message.room]["messages"]:
print("To be deleted:", self.db[message.room]["messages"][key])
reply = f"This message is deleted: { self.db[message.room]['messages'][key] }"
del_from_db(self, message, key)
else:
reply = 'This message is already gone!'
reply = "This message is already gone!"
elif '@title' in message.text:
elif "@title" in message.text:
match = re.findall("@title .*", message.content)[0]
title = match.replace('@title','')
self.db[message.room]['title'] = title
reply = f'The title of the log is changed to: { title }'
title = match.replace("@title", "")
self.db[message.room]["title"] = title
reply = f"The title of the log is changed to: { title }"
elif '@style' in message.text:
reply = 'This is a future-feature ...'
elif "@style" in message.text:
reply = "This is a future-feature ..."
else:
reply = 'Hmm ... not sure what you want to do?'
reply = "Hmm ... not sure what you want to do?"
write_log(self, message)
return self.reply(reply, room=message.room)
logbot()

10
LogBot/stylesheet.css

@ -1,21 +1,21 @@
body{
body {
margin: 1em;
max-width: 800px;
}
.post{
.post {
margin: 1em 0;
clear: both;
}
.post p.key{
.post p.key {
float: left;
margin: 0 1em 1em;
}
.post p.message{
.post p.message {
}
img,
iframe,
audio,
video{
video {
max-width: calc(100% - 100px);
height: auto;
}
Loading…
Cancel
Save