forked from varia/bots
added download function for media files + prepared logbot to run on the server
This commit is contained in:
parent
80f9ef4232
commit
c612847048
Binary file not shown.
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
@ -2,20 +2,19 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf8">
|
<meta charset="utf8">
|
||||||
<title>*logbot*</title>
|
<title>{{ title }} - (Generator: LogBot)</title>
|
||||||
<link rel="stylesheet" type="text/css" href="stylesheet.css">
|
<link rel="stylesheet" type="text/css" href="stylesheet.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>{{ room }}</h1>
|
<h1>{{ title }}</h1>
|
||||||
|
|
||||||
{% for num, msg in db.items() %}
|
{% for num, msg in db.items() %}
|
||||||
|
|
||||||
<div class="post">
|
<div class="post">
|
||||||
<p class="key">({{ num }})</p>
|
<p class="key">[{{ num }}]</p>
|
||||||
<p class="message">{{ msg }}</p>
|
<p class="message">{{ msg }}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
|
{% endfor %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
106
LogBot/logbot.py
106
LogBot/logbot.py
@ -1,35 +1,78 @@
|
|||||||
from xbotlib import Bot
|
from xbotlib import Bot
|
||||||
import json
|
|
||||||
import jinja2
|
import jinja2
|
||||||
import re
|
import os, re, json, shutil
|
||||||
import os
|
import urllib.request
|
||||||
import shutil
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
output = './'
|
local = './'
|
||||||
|
server = '/var/www/logs/'
|
||||||
|
|
||||||
def add_to_db(self, message):
|
# output = local
|
||||||
keys = [int(x) for x in self.db[message.room].keys()]
|
output = server
|
||||||
|
|
||||||
|
def add_to_db(self, message, media_post=None):
|
||||||
|
keys = [x for x in self.db[message.room]['messages'].keys()]
|
||||||
if not keys:
|
if not keys:
|
||||||
new_key = '0'
|
new_key = "0"
|
||||||
else:
|
else:
|
||||||
new_key = str(keys[-1] + 1)
|
new_key = str(int(keys[-1]) + 1)
|
||||||
self.db[message.room][new_key] = message.content
|
if media_post:
|
||||||
|
self.db[message.room]['messages'][new_key] = media_post
|
||||||
|
else:
|
||||||
|
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][key]
|
del self.db[message.room]['messages'][key]
|
||||||
|
|
||||||
def write_log(self, message):
|
def write_log(self, message):
|
||||||
template_file = 'index.html.j2' # Hmm... how to read the self.template ?
|
template_file = 'index.html.j2' # Hmm... how to read the self.template ?
|
||||||
print('using the template: ', template_file)
|
print('using the template: ', template_file)
|
||||||
template = jinja2.Template(open(template_file).read())
|
template = jinja2.Template(open(template_file).read())
|
||||||
if not os.path.isdir(message.room):
|
|
||||||
os.mkdir(message.room)
|
|
||||||
shutil.copy('stylesheet.css', message.room)
|
|
||||||
with open(f'{ output }/{ message.room }/index.html','w') as out:
|
with open(f'{ output }/{ message.room }/index.html','w') as out:
|
||||||
html = template.render(room=message.room, db=self.db[message.room])
|
html = template.render(title=self.db[message.room]['title'], db=self.db[message.room]['messages'])
|
||||||
out.write(html)
|
out.write(html)
|
||||||
print('writing to: ', f'{ message.room }/index.html')
|
print('writing to: ', f'{ message.room }/index.html')
|
||||||
|
|
||||||
|
def download(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'
|
||||||
|
else:
|
||||||
|
media_type = None
|
||||||
|
|
||||||
|
# download file
|
||||||
|
data = urllib.request.urlopen(message.url).read()
|
||||||
|
if data:
|
||||||
|
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)
|
||||||
|
if not os.path.isdir(f'{ message.room }/{ media_type }'):
|
||||||
|
os.mkdir(f'{ message.room }/{ media_type }')
|
||||||
|
with open(f'{ message.room }/{ media_type }/{ filename }', 'wb') as media_file:
|
||||||
|
media_file.write(data)
|
||||||
|
media_file.close()
|
||||||
|
|
||||||
|
# define media_post
|
||||||
|
media_path = f'{ 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):
|
||||||
|
|
||||||
@ -45,7 +88,7 @@ logbot @add <message>: Add a message to the log.
|
|||||||
|
|
||||||
logbot @delete <num>: Delete posts from the log. For example: @logbot @delete 5
|
logbot @delete <num>: Delete posts from the log. For example: @logbot @delete 5
|
||||||
|
|
||||||
logbot @title <string>: Set the title of your log. [future-feature]
|
logbot @title <string>: Set the title of your log.
|
||||||
|
|
||||||
logbot @style <element> <css-rule>: Edit the css of your log. For example: logbot @style body background-color: pink; [future-feature]
|
logbot @style <element> <css-rule>: Edit the css of your log. For example: logbot @style body background-color: pink; [future-feature]
|
||||||
|
|
||||||
@ -56,8 +99,18 @@ logbot @uptime: To check how long @logbot has been around
|
|||||||
|
|
||||||
def group(self, message):
|
def group(self, message):
|
||||||
|
|
||||||
|
# create a folder + database item
|
||||||
|
# for each of the rooms in which
|
||||||
|
# logbot is hanging around :)
|
||||||
if not message.room in self.db.keys():
|
if not message.room in self.db.keys():
|
||||||
self.db[message.room] = {}
|
self.db[message.room] = {}
|
||||||
|
self.db[message.room]['messages'] = {}
|
||||||
|
self.db[message.room]['title'] = message.room
|
||||||
|
if not os.path.isdir(message.room):
|
||||||
|
os.mkdir(message.room)
|
||||||
|
shutil.copy('stylesheet.css', message.room)
|
||||||
|
else:
|
||||||
|
print('WARNING! This folder already exists:', message.room)
|
||||||
|
|
||||||
# to debug in the terminal
|
# to debug in the terminal
|
||||||
print('------------------')
|
print('------------------')
|
||||||
@ -65,9 +118,15 @@ logbot @uptime: To check how long @logbot has been around
|
|||||||
print('room: ', message.room)
|
print('room: ', message.room)
|
||||||
print('sender: ', message.sender)
|
print('sender: ', message.sender)
|
||||||
|
|
||||||
|
# image / PDF / audio / video
|
||||||
if message.url:
|
if message.url:
|
||||||
# messages = writedb(f'<img src="{ message.url }">')
|
media_post, media_type = download(message)
|
||||||
reply = 'Thanks for that image!'
|
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 }!'
|
||||||
|
else:
|
||||||
|
reply = 'Sorry, can\'t touch that :('
|
||||||
|
|
||||||
elif '@add' in message.text:
|
elif '@add' in message.text:
|
||||||
add_to_db(self, message)
|
add_to_db(self, message)
|
||||||
@ -77,9 +136,9 @@ logbot @uptime: To check how long @logbot has been around
|
|||||||
match = re.findall("@delete \d*", message.content)[0]
|
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]:
|
if key in self.db[message.room]['messages']:
|
||||||
print('To be deleted:', self.db[message.room][key])
|
print('To be deleted:', self.db[message.room]['messages'][key])
|
||||||
reply = f'This message is deleted: { self.db[message.room][key] }'
|
reply = f"This message is deleted: { self.db[message.room]['messages'][key] }"
|
||||||
del_from_db(self, message, key)
|
del_from_db(self, message, key)
|
||||||
else:
|
else:
|
||||||
reply = 'This message is already gone!'
|
reply = 'This message is already gone!'
|
||||||
@ -88,7 +147,10 @@ logbot @uptime: To check how long @logbot has been around
|
|||||||
print('HELP')
|
print('HELP')
|
||||||
|
|
||||||
elif '@title' in message.text:
|
elif '@title' in message.text:
|
||||||
reply = 'This is a future-feature ...'
|
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:
|
elif '@style' in message.text:
|
||||||
reply = 'This is a future-feature ...'
|
reply = 'This is a future-feature ...'
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
body{
|
body{
|
||||||
margin: 1em;
|
margin: 1em;
|
||||||
|
max-width: 800px;
|
||||||
}
|
}
|
||||||
.post{
|
.post{
|
||||||
margin: 1em 0;
|
margin: 1em 0;
|
||||||
@ -10,4 +11,11 @@ body{
|
|||||||
margin: 0 1em 1em;
|
margin: 0 1em 1em;
|
||||||
}
|
}
|
||||||
.post p.message{
|
.post p.message{
|
||||||
|
}
|
||||||
|
img,
|
||||||
|
iframe,
|
||||||
|
audio,
|
||||||
|
video{
|
||||||
|
max-width: calc(100% - 100px);
|
||||||
|
height: auto;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user