From 39b16e68a530f682e65791224add155f66896919 Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 11 Mar 2018 12:47:11 +0100 Subject: [PATCH] Added simple html reporter --- slack_backup/command.py | 2 +- slack_backup/reporters.py | 163 +++++++++++++++++++++++++++++++++++++- 2 files changed, 163 insertions(+), 2 deletions(-) diff --git a/slack_backup/command.py b/slack_backup/command.py index 7127078..455de21 100644 --- a/slack_backup/command.py +++ b/slack_backup/command.py @@ -105,7 +105,7 @@ def main(): "directory for store logs. All logs are organised " "per channel. By default it's `logs' directory") generate.add_argument('-f', '--format', default=None, - choices=('text', 'none'), + choices=('text', 'html', 'none'), help='Output format. Default is none; only database ' 'is updated by latest messages for all/selected ' 'channels.') diff --git a/slack_backup/reporters.py b/slack_backup/reporters.py index fa7779d..2149abf 100644 --- a/slack_backup/reporters.py +++ b/slack_backup/reporters.py @@ -260,9 +260,170 @@ class TextReporter(Reporter): self._get_symbol('separator') + ' ') +class StaticHtmlReporter(Reporter): + """Text-like, but with browsable, clickable links""" + ext = '.html' + index_templ = """ + + + + %(title)s + + +
+ %(msgs)s +
+ + + """ + index_list = """ + + """ + msg_head = """ + + + + Bla + + + +
+ + """ + msg_foot = """ +
+
+ + + """ + msg_line = """ + + {date} + {nick} + {msg} + + """ + + def __init__(self, args, query): + super(StaticHtmlReporter, self).__init__(args, query) + utils.makedirs(self.out) + self._max_len = 0 + + def generate(self): + """Generate raport""" + super(StaticHtmlReporter, self).generate() + + with open(os.path.join(self.out, "index.html"), "w") as fobj: + content = {'title': 'index', + 'msgs': self.index_list % self._get_index_list()} + fobj.write(self.index_templ % content) + + def write_msg(self, messages, log): + """Write message to file""" + with open(log, "w") as fobj: + fobj.write(self.msg_head) + + super(StaticHtmlReporter, self).write_msg(messages, log) + + with open(log, "a") as fobj: + fobj.write(self.msg_foot) + + def _get_index_list(self): + _list = [] + for channel in sorted([c.name for c in self.channels]): + _list.append('
  • %s
  • ' % (channel + '.html', + channel)) + return '\n'.join(_list) + + def _process_message(self, msg): + """ + Check what kind of message we are dealing with and do appropriate + formatting + """ + data = super(StaticHtmlReporter, self)._process_message(msg) + data['msg'] = self._filter_slackid(data['msg']) + data.update({'date': msg.datetime().strftime("%Y-%m-%d %H:%M:%S"), + 'tpl': self.msg_line}) + return data + + def _msg_file(self, msg): + """return data for file""" + fpath = os.path.abspath(msg.file.filepath) + _, ext = os.path.splitext(fpath) + if ext.lower() in ('.png', '.jpg', '.jpeg', '.gif'): + url = ('' + msg.file.title +
+                   '') + else: + url = ('' + msg.file.title + '') + + return {'msg': self.url_pat.sub(url, msg.text), + 'nick': self._get_symbol('file')} + + def _msg(self, msg): + """return processor for all other message types""" + + data = {'date': msg.datetime().strftime("%Y-%m-%d %H:%M:%S"), + 'msg': msg.text, + 'nick': msg.user.name} + + attachement_msg = [] + + if msg.attachments: + for att in msg.attachments: + if att.title: + att_text = att.title + else: + att_text = att.fallback + + if att.text: + att_text += '
    ' + att.text + + if 'http' in att.fallback: + if not att.fallback.startswith('http'): + link = ('' + att_text + '') + else: + link = ('' + + att_text + '') + + if att_text == att.title: + att_text = link + else: + att_text += '
    ' + link + + attachement_msg.append(att_text) + + data['msg'] += '
    '.join(attachement_msg) + return data + + def get_reporter(args, query): """Return object of right reporter class""" - reporters = {'text': TextReporter} + reporters = {'text': TextReporter, + 'html': StaticHtmlReporter} klass = reporters.get(args.format, NoneReporter) if klass.__name__ == 'Reporter':