forked from varia/bots
upgrading logbot to xbotlib 0.16
This commit is contained in:
parent
184b3fa099
commit
6918715868
@ -1,17 +1,13 @@
|
|||||||
from xbotlib import Bot
|
from xbotlib import Bot
|
||||||
import jinja2
|
import jinja2
|
||||||
import os, re, json, shutil
|
import os, re, json, shutil
|
||||||
|
from os import environ
|
||||||
import urllib.request
|
import urllib.request
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
local = './'
|
|
||||||
server = '/var/www/logs'
|
|
||||||
|
|
||||||
# output = local
|
|
||||||
output = server
|
|
||||||
|
|
||||||
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)
|
||||||
if not keys:
|
if not keys:
|
||||||
new_key = "0"
|
new_key = "0"
|
||||||
else:
|
else:
|
||||||
@ -25,13 +21,12 @@ 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_file = 'index.html.j2' # Hmm... how to read the self.template ?
|
template = jinja2.Template(open('template.html').read())
|
||||||
print('using the template: ', template_file)
|
log_path = os.path.join(self.output, message.room, 'index.html')
|
||||||
template = jinja2.Template(open(template_file).read())
|
with open(log_path,'w') as out:
|
||||||
with open(f'{ output }/{ message.room }/index.html','w') as out:
|
|
||||||
html = template.render(title=self.db[message.room]['title'], db=self.db[message.room]['messages'])
|
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: ', log_path)
|
||||||
|
|
||||||
def download(message):
|
def download(message):
|
||||||
# define media_type
|
# define media_type
|
||||||
@ -53,14 +48,16 @@ def download(message):
|
|||||||
parsed_url = urlparse(message.url)
|
parsed_url = urlparse(message.url)
|
||||||
filename = os.path.basename(parsed_url.path).replace(' ','_').replace('%20','_') # safe url's
|
filename = os.path.basename(parsed_url.path).replace(' ','_').replace('%20','_') # safe url's
|
||||||
print('as the file: ', filename)
|
print('as the file: ', filename)
|
||||||
if not os.path.isdir(f'{ output }/{ message.room }/{ media_type }'):
|
path = os.path.join(output, message.room, media_type)
|
||||||
os.mkdir(f'{ output }/{ message.room }/{ media_type }')
|
if not os.path.isdir(path):
|
||||||
with open(f'{ output }/{ message.room }/{ media_type }/{ filename }', 'wb') as media_file:
|
os.mkdir(path)
|
||||||
|
file_path = os.path.join(path, filename)
|
||||||
|
with open(file_path, 'wb') as media_file:
|
||||||
media_file.write(data)
|
media_file.write(data)
|
||||||
media_file.close()
|
media_file.close()
|
||||||
|
|
||||||
# define media_post
|
# define media_post
|
||||||
media_path = f'{ media_type }/{ filename }'
|
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 }">'
|
media_post = f'<img src="{ media_path }">'
|
||||||
elif message.url.lower().endswith('.pdf'):
|
elif message.url.lower().endswith('.pdf'):
|
||||||
@ -96,24 +93,30 @@ 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):
|
||||||
|
for room in self.rooms:
|
||||||
|
|
||||||
|
roomname = str(re.match(r'.*@', room).group()).replace('@','')
|
||||||
|
self.output = os.path.join(self.output, roomname)
|
||||||
|
|
||||||
|
# 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 }')
|
||||||
|
|
||||||
|
# Check if the room has an output folder already
|
||||||
|
if not os.path.exists(self.output):
|
||||||
|
os.mkdir(self.output)
|
||||||
|
shutil.copy('stylesheet.css', self.output)
|
||||||
|
print('INFO ', f'Created a folder for: { room }')
|
||||||
|
print('INFO ', f'Copied stylesheet.css to: { room }')
|
||||||
|
|
||||||
|
print('INFO ', f'Output folder is set to: { self.output }')
|
||||||
|
|
||||||
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():
|
|
||||||
self.db[message.room] = {}
|
|
||||||
self.db[message.room]['messages'] = {}
|
|
||||||
self.db[message.room]['title'] = message.room
|
|
||||||
output_path = os.path.join(server, message.room)
|
|
||||||
if not os.path.isdir(output_path):
|
|
||||||
os.mkdir(output_path)
|
|
||||||
print('This folder is created:', output_path)
|
|
||||||
shutil.copy('stylesheet.css', output_path)
|
|
||||||
else:
|
|
||||||
print('WARNING! This folder already exists:', output_path)
|
|
||||||
|
|
||||||
# to debug in the terminal
|
# to debug in the terminal
|
||||||
print('------------------')
|
print('------------------')
|
||||||
print('message: ', message.text)
|
print('message: ', message.text)
|
||||||
@ -145,9 +148,6 @@ logbot @uptime: To check how long @logbot has been around
|
|||||||
else:
|
else:
|
||||||
reply = 'This message is already gone!'
|
reply = 'This message is already gone!'
|
||||||
|
|
||||||
elif '@help' in message.text:
|
|
||||||
print('HELP')
|
|
||||||
|
|
||||||
elif '@title' in message.text:
|
elif '@title' in message.text:
|
||||||
match = re.findall("@title .*", message.content)[0]
|
match = re.findall("@title .*", message.content)[0]
|
||||||
title = match.replace('@title','')
|
title = match.replace('@title','')
|
||||||
|
Loading…
Reference in New Issue
Block a user