diff --git a/setup.py b/setup.py index f71ab74..7769c8a 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ except ImportError: setup(name="slack-backup", packages=["slack_backup"], - version="0.7", + version="0.8", description="Make copy of slack converstaions", author="Roman Dobosz", author_email="gryf73@gmail.com", diff --git a/slack_backup/reporters.py b/slack_backup/reporters.py index 5ede715..8ffcb9b 100644 --- a/slack_backup/reporters.py +++ b/slack_backup/reporters.py @@ -34,6 +34,7 @@ class Reporter(object): literal_url_pat = re.compile(r'(?P(?Phttps?[^\s\|]+))') url_pat = re.compile(r'(?P<(?Phttp[^\|>]+)' r'(\|(?P[^>]+))?>)') + url2_pat = re.compile(r'<(?P<url>https?[^\s\|]+)>') slackid_pat = re.compile(r'(?P<replace><@' r'(?P<slackid>U[A-Z,0-9]+)(\|[^>]+)?[^>]*>)') @@ -44,7 +45,7 @@ class Reporter(object): self.types = {"channel_join": self._msg_join, "channel_leave": self._msg_leave, "channel_topic": self._msg_topic, - "file_share": self._msg_file, + # "file_share": self._msg_file, "me_message": self._msg_me} self.emoji = emoji.EMOJI.get(args.theme, {}) @@ -66,18 +67,25 @@ class Reporter(object): filter(o.Message.channel == channel).\ order_by(o.Message.ts).all(): messages.append(message) - self.write_msg(messages, log_path) + self.write_msg(messages, log_path, channel) def get_log_path(self, name): """Return relative log file name """ return os.path.join(self.out, name + self.ext) - def write_msg(self, messages, log): + def write_msg(self, messages, log, channel): """Write message to file""" with open(log, "a", encoding='utf8') as fobj: for message in messages: data = self._process_message(message) fobj.write(data['tpl'].format(**data)) + if message.files: + for _file in message.files: + data = self._msg_file(message, _file) + fobj.write(data['tpl'].format(**data)) + # else: + # data = self._process_message(message) + # fobj.write(data['tpl'].format(**data)) def _get_symbol(self, item): """Return appropriate item depending on the selected theme""" @@ -134,7 +142,7 @@ class Reporter(object): return {'msg': msg.user.name + ' ' + msg.text, 'nick': self._get_symbol('me')} - def _msg_file(self, msg): + def _msg_file(self, msg, _file): """return data for file""" return {'msg': msg.text, 'nick': self._get_symbol('file')} @@ -194,7 +202,7 @@ class TextReporter(Reporter): order_by(o.Message.ts).all(): messages.append(message) - self.write_msg(messages, log_path) + self.write_msg(messages, log_path, channel) def _set_max_len(self, channel): """calculate max_len for sepcified channel""" @@ -221,17 +229,20 @@ class TextReporter(Reporter): 'tpl': self.tpl}) return data - def _msg_file(self, msg): + def _msg_file(self, message, _file): """return data for file""" - if msg.file.filepath: - fpath = os.path.abspath(msg.file.filepath) + if _file.filepath: + fpath = os.path.abspath(_file.filepath) fpath = pathlib.PurePath(fpath).as_uri() else: fpath = 'does_not_exists' - return {'msg': self.url_pat.sub('(' + fpath + ') ' + msg.file.title, - msg.text), - 'nick': self._get_symbol('file')} + return {'msg': _file.title + ' ' + fpath, + 'nick': self._get_symbol('file'), + 'date': message.datetime().strftime("%Y-%m-%d %H:%M:%S"), + 'max_len': self._max_len, + 'separator': self._get_symbol('separator'), + 'tpl': self.tpl} def _msg(self, msg): """return data for all other message types""" @@ -293,7 +304,9 @@ class StaticHtmlReporter(Reporter): <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/> - <title>Bla + %(title)s + """ + msg_style = """ @@ -349,12 +366,16 @@ class StaticHtmlReporter(Reporter): 'msgs': self.index_list % self._get_index_list()} fobj.write(self.index_templ % content) - def write_msg(self, messages, log): + def write_msg(self, messages, log, channel): """Write message to file""" with open(log, "w", encoding='utf8') as fobj: - fobj.write(self.msg_head) + title = channel.name + if channel.topic: + title = channel.name + ' ' + channel.topic.value + fobj.write(self.msg_head % {'title': title}) + fobj.write(self.msg_style) - super(StaticHtmlReporter, self).write_msg(messages, log) + super(StaticHtmlReporter, self).write_msg(messages, log, channel) with open(log, "a", encoding='utf8') as fobj: fobj.write(self.msg_foot) @@ -377,29 +398,46 @@ class StaticHtmlReporter(Reporter): 'tpl': self.msg_line}) return data - def _msg_file(self, msg): + def _msg_file(self, msg, _file): """return data for file""" - if msg.file.filepath: - fpath = os.path.abspath(msg.file.filepath) + if _file.filepath: + fpath = os.path.abspath(_file.filepath) fpath = pathlib.PurePath(fpath).as_uri() else: fpath = 'does_not_exists' _, ext = os.path.splitext(fpath) if ext.lower() in ('.png', '.jpg', '.jpeg', '.gif'): - url = ('' +
-                   msg.file.title + '') + url = ('' +
+                   _file.title + '') else: - url = ('' + msg.file.title + '') + url = ('' + _file.title + '') - return {'msg': self.url_pat.sub(url, msg.text), + data = {'date': msg.datetime().strftime("%Y-%m-%d %H:%M:%S"), + 'msg': self._filter_slackid(url + _file.title), + 'tpl': self.msg_line, 'nick': self._get_symbol('file')} + for emoticon in self.emoji: + data['msg'] = data['msg'].replace(emoticon, self.emoji[emoticon]) + + return data + def _msg(self, msg): """return processor for all other message types""" + match = self.url2_pat.match(msg.text) + text = msg.text + if match: + text = '' + for part in self.url2_pat.split(msg.text): + if 'http' in part: + text += '' + part + '' + else: + text += part + data = {'date': msg.datetime().strftime("%Y-%m-%d %H:%M:%S"), - 'msg': msg.text, + 'msg': text, 'nick': msg.user.name} link = '{title}'