Browse Source

upgrading logbot to xbotlib 0.16

master
manetta 4 years ago
parent
commit
6918715868
  1. 66
      LogBot/logbot.py
  2. 0
      LogBot/template.html

66
LogBot/logbot.py

@ -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,23 +93,29 @@ 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:
def group(self, message): roomname = str(re.match(r'.*@', room).group()).replace('@','')
self.output = os.path.join(self.output, roomname)
# create a folder + database item # Check if the room is in the database already
# for each of the rooms in which if not room in self.db.keys():
# logbot is hanging around :) self.db[room] = {}
if not message.room in self.db.keys(): self.db[room]['messages'] = {}
self.db[message.room] = {} self.db[room]['title'] = room
self.db[message.room]['messages'] = {} print('INFO ', f'Added to the database: { room }')
self.db[message.room]['title'] = message.room
output_path = os.path.join(server, message.room) # Check if the room has an output folder already
if not os.path.isdir(output_path): if not os.path.exists(self.output):
os.mkdir(output_path) os.mkdir(self.output)
print('This folder is created:', output_path) shutil.copy('stylesheet.css', self.output)
shutil.copy('stylesheet.css', output_path) print('INFO ', f'Created a folder for: { room }')
else: print('INFO ', f'Copied stylesheet.css to: { room }')
print('WARNING! This folder already exists:', output_path)
print('INFO ', f'Output folder is set to: { self.output }')
def group(self, message):
# to debug in the terminal # to debug in the terminal
print('------------------') print('------------------')
@ -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','')

0
LogBot/index.html.j2 → LogBot/template.html

Loading…
Cancel
Save