forked from varia/bots
Auto-formatter has something to say here
This commit is contained in:
parent
63365779e3
commit
b359d92021
@ -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:`.
|
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?
|
## How to use LogBot?
|
||||||
|
|
||||||
@ -24,10 +24,10 @@ logbot @uptime: To check how long @logbot has been around
|
|||||||
|
|
||||||
## Situated tails
|
## Situated tails
|
||||||
|
|
||||||
* Archive bot, Relearn 2017, <https://gitlab.com/relearn/relearn2017/-/tree/master/xmpp-bots/archive-bot>
|
- 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>
|
- 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>
|
- Logbot v1, Varia XMPP extension 2017-2020, <https://git.vvvvvvaria.org/varia/bots/src/branch/master/logbot>
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
* [xbotlib](https://git.vvvvvvaria.org/decentral1se/xbotlib)
|
- [xbotlib](https://git.vvvvvvaria.org/decentral1se/xbotlib)
|
||||||
|
299
LogBot/logbot.py
299
LogBot/logbot.py
@ -1,81 +1,134 @@
|
|||||||
from xbotlib import Bot
|
import json
|
||||||
import jinja2
|
import os
|
||||||
import os, re, json, shutil
|
import re
|
||||||
from os import environ
|
import shutil
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
from os import environ
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
import jinja2
|
||||||
|
from xbotlib import Bot
|
||||||
|
|
||||||
|
|
||||||
def add_to_db(self, message, media_post=None):
|
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)
|
keys.sort(key=int)
|
||||||
if not keys:
|
if not keys:
|
||||||
new_key = "0"
|
new_key = "0"
|
||||||
else:
|
else:
|
||||||
new_key = str(int(keys[-1]) + 1)
|
new_key = str(int(keys[-1]) + 1)
|
||||||
if media_post:
|
if media_post:
|
||||||
self.db[message.room]['messages'][new_key] = media_post
|
self.db[message.room]["messages"][new_key] = media_post
|
||||||
else:
|
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):
|
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):
|
def write_log(self, message):
|
||||||
template = jinja2.Template(open('template.html').read())
|
template = jinja2.Template(open("template.html").read())
|
||||||
roomname = re.sub(r'@.*', '', message.room)
|
roomname = re.sub(r"@.*", "", message.room)
|
||||||
log_path = os.path.join(self.output, roomname, 'index.html')
|
log_path = os.path.join(self.output, roomname, "index.html")
|
||||||
with open(log_path,'w') as out:
|
with open(log_path, "w") as out:
|
||||||
html = template.render(title=self.db[message.room]['title'], db=self.db[message.room]['messages'])
|
html = template.render(
|
||||||
out.write(html)
|
title=self.db[message.room]["title"], db=self.db[message.room]["messages"]
|
||||||
print('writing to: ', log_path)
|
)
|
||||||
|
out.write(html)
|
||||||
|
print("writing to: ", log_path)
|
||||||
|
|
||||||
|
|
||||||
def download(self, message):
|
def download(self, message):
|
||||||
# define media_type
|
# define media_type
|
||||||
if message.url.lower().endswith(('.jpg','jpeg','png','.gif','.bmp','.svg','eps')):
|
if message.url.lower().endswith(
|
||||||
media_type = 'images'
|
(".jpg", "jpeg", "png", ".gif", ".bmp", ".svg", "eps")
|
||||||
elif message.url.lower().endswith('.pdf'):
|
):
|
||||||
media_type = 'pdf'
|
media_type = "images"
|
||||||
elif message.url.lower().endswith(('.mp3','.ogg','.oga','.mogg','.wav','.m4a','.webm')):
|
elif message.url.lower().endswith(".pdf"):
|
||||||
media_type = 'audio'
|
media_type = "pdf"
|
||||||
elif message.url.lower().endswith(('.mp4','.webm','.flv','.vob','.avi','.mov','.qt','.mpg','.mpeg','.mp4','.m2v','.mpe','.3gp')):
|
elif message.url.lower().endswith(
|
||||||
media_type = 'video'
|
(".mp3", ".ogg", ".oga", ".mogg", ".wav", ".m4a", ".webm")
|
||||||
else:
|
):
|
||||||
media_type = None
|
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
|
# download file
|
||||||
data = urllib.request.urlopen(message.url).read()
|
data = urllib.request.urlopen(message.url).read()
|
||||||
if data:
|
if data:
|
||||||
print('downloading: ', message.url)
|
print("downloading: ", message.url)
|
||||||
parsed_url = urlparse(message.url)
|
parsed_url = urlparse(message.url)
|
||||||
filename = os.path.basename(parsed_url.path).replace(' ','_').replace('%20','_') # safe url's
|
filename = (
|
||||||
print('as the file: ', filename)
|
os.path.basename(parsed_url.path).replace(" ", "_").replace("%20", "_")
|
||||||
roomname = re.sub(r'@.*', '', message.room)
|
) # safe url's
|
||||||
path = os.path.join(self.output, roomname, media_type)
|
print("as the file: ", filename)
|
||||||
if not os.path.isdir(path):
|
roomname = re.sub(r"@.*", "", message.room)
|
||||||
os.mkdir(path)
|
path = os.path.join(self.output, roomname, media_type)
|
||||||
file_path = os.path.join(path, filename)
|
if not os.path.isdir(path):
|
||||||
with open(file_path, 'wb') as media_file:
|
os.mkdir(path)
|
||||||
media_file.write(data)
|
file_path = os.path.join(path, filename)
|
||||||
media_file.close()
|
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")
|
||||||
|
):
|
||||||
|
media_post = f'<img src="{ media_path }">'
|
||||||
|
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")
|
||||||
|
):
|
||||||
|
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",
|
||||||
|
)
|
||||||
|
):
|
||||||
|
media_post = f'<video controls src="{ media_path }"></video>'
|
||||||
|
else:
|
||||||
|
media_post = None
|
||||||
|
|
||||||
|
return media_post, media_type
|
||||||
|
|
||||||
# define media_post
|
|
||||||
media_path = os.path.join(media_type, filename)
|
|
||||||
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'):
|
|
||||||
media_post = f'<iframe src="{ media_path }" width="800" height="1000"></iframe>'
|
|
||||||
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')):
|
|
||||||
media_post = f'<video controls src="{ media_path }"></video>'
|
|
||||||
else:
|
|
||||||
media_post = None
|
|
||||||
|
|
||||||
return media_post, media_type
|
|
||||||
|
|
||||||
class logbot(Bot):
|
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:)
|
(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
|
logbot @uptime: To check how long @logbot has been around
|
||||||
|
|
||||||
@bots: To see who is around :)
|
@bots: To see who is around :)
|
||||||
'''
|
"""
|
||||||
def setup(self):
|
|
||||||
print('INFO ', f'Output folder is set to: { self.output }')
|
|
||||||
|
|
||||||
for room in self.rooms:
|
|
||||||
|
|
||||||
roomname = str(re.match(r'.*@', room).group()).replace('@','')
|
def setup(self):
|
||||||
self.room_path = os.path.join(self.output, roomname)
|
print("INFO ", f"Output folder is set to: { self.output }")
|
||||||
print('INFO ', f'Room path is set to: { self.room_path }')
|
|
||||||
|
|
||||||
# Check if the room is in the database already
|
for room in self.rooms:
|
||||||
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 }')
|
|
||||||
|
|
||||||
# Check if the room has an output folder already
|
roomname = str(re.match(r".*@", room).group()).replace("@", "")
|
||||||
if not os.path.exists(self.room_path):
|
self.room_path = os.path.join(self.output, roomname)
|
||||||
os.mkdir(self.room_path)
|
print("INFO ", f"Room path is set to: { 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 }')
|
|
||||||
|
|
||||||
def group(self, message):
|
|
||||||
|
|
||||||
# to debug in the terminal
|
# Check if the room is in the database already
|
||||||
print('------------------')
|
if not room in self.db.keys():
|
||||||
print('message: ', message.text)
|
self.db[room] = {}
|
||||||
print('room: ', message.room)
|
self.db[room]["messages"] = {}
|
||||||
print('sender: ', message.sender)
|
self.db[room]["title"] = room
|
||||||
|
print("INFO ", f"Added to the database: { room }")
|
||||||
|
|
||||||
# image / PDF / audio / video
|
# Check if the room has an output folder already
|
||||||
if message.url:
|
if not os.path.exists(self.room_path):
|
||||||
media_post, media_type = download(self, message)
|
os.mkdir(self.room_path)
|
||||||
if media_post:
|
shutil.copy("stylesheet.css", self.room_path)
|
||||||
add_to_db(self, message, media_post=media_post)
|
print("INFO ", f"Created a folder for: { room }")
|
||||||
media_type = media_type.replace('images', 'image') # linguistic hack!
|
print("INFO ", f"Copied stylesheet.css to: { room }")
|
||||||
reply = f'Thanks for that { media_type }!'
|
|
||||||
else:
|
|
||||||
reply = 'Sorry, can\'t process that :('
|
|
||||||
|
|
||||||
elif '@add' in message.text:
|
def group(self, message):
|
||||||
add_to_db(self, message)
|
|
||||||
reply = 'Added, thanks!'
|
|
||||||
|
|
||||||
elif '@delete' in message.text:
|
# to debug in the terminal
|
||||||
match = re.findall("@delete \d*", message.content)[0]
|
print("------------------")
|
||||||
key = str(match.replace('@delete ',''))
|
print("message: ", message.text)
|
||||||
|
print("room: ", message.room)
|
||||||
|
print("sender: ", message.sender)
|
||||||
|
|
||||||
if key in self.db[message.room]['messages']:
|
# image / PDF / audio / video
|
||||||
print('To be deleted:', self.db[message.room]['messages'][key])
|
if message.url:
|
||||||
reply = f"This message is deleted: { self.db[message.room]['messages'][key] }"
|
media_post, media_type = download(self, message)
|
||||||
del_from_db(self, message, key)
|
if media_post:
|
||||||
else:
|
add_to_db(self, message, media_post=media_post)
|
||||||
reply = 'This message is already gone!'
|
media_type = media_type.replace("images", "image") # linguistic hack!
|
||||||
|
reply = f"Thanks for that { media_type }!"
|
||||||
|
else:
|
||||||
|
reply = "Sorry, can't process that :("
|
||||||
|
|
||||||
elif '@title' in message.text:
|
elif "@add" in message.text:
|
||||||
match = re.findall("@title .*", message.content)[0]
|
add_to_db(self, message)
|
||||||
title = match.replace('@title','')
|
reply = "Added, thanks!"
|
||||||
self.db[message.room]['title'] = title
|
|
||||||
reply = f'The title of the log is changed to: { title }'
|
|
||||||
|
|
||||||
elif '@style' in message.text:
|
elif "@delete" in message.text:
|
||||||
reply = 'This is a future-feature ...'
|
match = re.findall("@delete \d*", message.content)[0]
|
||||||
|
key = str(match.replace("@delete ", ""))
|
||||||
|
|
||||||
else:
|
if key in self.db[message.room]["messages"]:
|
||||||
reply = 'Hmm ... not sure what you want to do?'
|
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!"
|
||||||
|
|
||||||
|
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 }"
|
||||||
|
|
||||||
|
elif "@style" in message.text:
|
||||||
|
reply = "This is a future-feature ..."
|
||||||
|
|
||||||
|
else:
|
||||||
|
reply = "Hmm ... not sure what you want to do?"
|
||||||
|
|
||||||
|
write_log(self, message)
|
||||||
|
return self.reply(reply, room=message.room)
|
||||||
|
|
||||||
write_log(self, message)
|
|
||||||
return self.reply(reply, room=message.room)
|
|
||||||
|
|
||||||
logbot()
|
logbot()
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
body{
|
body {
|
||||||
margin: 1em;
|
margin: 1em;
|
||||||
max-width: 800px;
|
max-width: 800px;
|
||||||
}
|
}
|
||||||
.post{
|
.post {
|
||||||
margin: 1em 0;
|
margin: 1em 0;
|
||||||
clear: both;
|
clear: both;
|
||||||
}
|
}
|
||||||
.post p.key{
|
.post p.key {
|
||||||
float: left;
|
float: left;
|
||||||
margin: 0 1em 1em;
|
margin: 0 1em 1em;
|
||||||
}
|
}
|
||||||
.post p.message{
|
.post p.message {
|
||||||
}
|
}
|
||||||
img,
|
img,
|
||||||
iframe,
|
iframe,
|
||||||
audio,
|
audio,
|
||||||
video{
|
video {
|
||||||
max-width: calc(100% - 100px);
|
max-width: calc(100% - 100px);
|
||||||
height: auto;
|
height: auto;
|
||||||
}
|
}
|
||||||
|
@ -17,4 +17,4 @@
|
|||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user