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
|
||||
import jinja2
|
||||
import os, re, json, shutil
|
||||
from os import environ
|
||||
import urllib.request
|
||||
from urllib.parse import urlparse
|
||||
|
||||
local = './'
|
||||
server = '/var/www/logs'
|
||||
|
||||
# output = local
|
||||
output = server
|
||||
|
||||
def add_to_db(self, message, media_post=None):
|
||||
keys = [x for x in self.db[message.room]['messages'].keys()]
|
||||
keys.sort(key=int)
|
||||
if not keys:
|
||||
new_key = "0"
|
||||
else:
|
||||
@ -25,13 +21,12 @@ def del_from_db(self, message, key):
|
||||
del self.db[message.room]['messages'][key]
|
||||
|
||||
def write_log(self, message):
|
||||
template_file = 'index.html.j2' # Hmm... how to read the self.template ?
|
||||
print('using the template: ', template_file)
|
||||
template = jinja2.Template(open(template_file).read())
|
||||
with open(f'{ output }/{ message.room }/index.html','w') as out:
|
||||
template = jinja2.Template(open('template.html').read())
|
||||
log_path = os.path.join(self.output, message.room, '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: ', f'{ message.room }/index.html')
|
||||
print('writing to: ', log_path)
|
||||
|
||||
def download(message):
|
||||
# define media_type
|
||||
@ -53,14 +48,16 @@ def download(message):
|
||||
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'{ output }/{ message.room }/{ media_type }'):
|
||||
os.mkdir(f'{ output }/{ message.room }/{ media_type }')
|
||||
with open(f'{ output }/{ message.room }/{ media_type }/{ filename }', 'wb') as media_file:
|
||||
path = os.path.join(output, message.room, 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)
|
||||
media_file.close()
|
||||
|
||||
# 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')):
|
||||
media_post = f'<img src="{ media_path }">'
|
||||
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 :)
|
||||
'''
|
||||
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):
|
||||
|
||||
# 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
|
||||
print('------------------')
|
||||
print('message: ', message.text)
|
||||
@ -145,9 +148,6 @@ logbot @uptime: To check how long @logbot has been around
|
||||
else:
|
||||
reply = 'This message is already gone!'
|
||||
|
||||
elif '@help' in message.text:
|
||||
print('HELP')
|
||||
|
||||
elif '@title' in message.text:
|
||||
match = re.findall("@title .*", message.content)[0]
|
||||
title = match.replace('@title','')
|
||||
|
Loading…
Reference in New Issue
Block a user