1
0
mirror of https://github.com/gryf/slack-backup.git synced 2025-12-17 11:30:25 +01:00

8 Commits
v0.4 ... v0.4.5

Author SHA1 Message Date
5f9f290ba4 Fix for message comment.
If comment is sent by the user, different structure of the data is sent.
First of all, the type of this message is "message", but it contain
dictionary under 'comment' key, which can be confusing, which contain
needed data (like user id). For this kind of messages, in case of lack
of 'user' in main dict, dict['comment']['user'] will be used for getting
user identifier, while dict['text'] remains as a message text.
2017-02-13 19:57:31 +01:00
Roman Dobosz
08a0a82435 Changed absolute to relative for filepaths stored in File objects 2016-12-03 18:43:49 +01:00
Roman Dobosz
a42506dff9 Fix for new fnames in case of already existing ones 2016-12-03 18:14:28 +01:00
Roman Dobosz
0d7607cf3c Added log for updating specific channel messages 2016-12-02 17:46:27 +01:00
9ddd470b54 Move commands functions to its own module 2016-11-28 19:05:26 +01:00
feb773956c Fix for extension of config file 2016-11-28 18:25:41 +01:00
3f95986981 Fix the tests.
For some reason, database key was treated differently in configparser object
in python 3.4.2. In Python 3.4.5 everything is fine. Fixed the defaults to
make sure all string options are treated equally.
2016-11-28 18:14:47 +01:00
6d5f3746a2 Superfast fix for non-existed config parameter in cmdline options 2016-11-28 17:20:14 +01:00
7 changed files with 154 additions and 138 deletions

View File

@@ -1,122 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Create backup for certain date for specified channel in slack
Execute commands for slack-backup
"""
import argparse
import logging
from slack_backup import client
from slack_backup import config
def setup_logger(args):
"""Setup logger format and level"""
level = logging.WARNING
if args.quiet:
level = logging.ERROR
if args.quiet > 1:
level = logging.CRITICAL
if args.verbose:
level = logging.INFO
if args.verbose > 1:
level = logging.DEBUG
logging.basicConfig(level=level,
format="%(asctime)s %(levelname)s: %(message)s")
def generate_raport(args):
"""Generate logs"""
slack = client.Client(args)
slack.generate_history()
def fetch_data(args):
"""Fetch and store data"""
slack = client.Client(args)
slack.update()
def main():
"""Main function"""
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest='parser')
subparser.required = True
fetch = subparser.add_parser('fetch', help='Update local db with Slack'
' data')
fetch.add_argument('-t', '--token', default=None, help='Slack token - '
'a string, which can be generated/obtained via '
'https://api.slack.com/docs/oauth-test-tokens page.')
fetch.add_argument('-u', '--user', default=None, help='Username for your '
'Slack account')
fetch.add_argument('-p', '--password', default=None, help='Password for '
'your Slack account.')
fetch.add_argument('-e', '--team', default=None, help='Team name, which '
'is part of slack url, for example: if url is '
'"https://team.slack.com" than "team" is a name of '
'the team.')
fetch.add_argument('-v', '--verbose', help='Be verbose. Adding more "v" '
'will increase verbosity', action="count",
default=None)
fetch.add_argument('-q', '--quiet', help='Be quiet. Adding more "q" will'
' decrease verbosity', action="count", default=None)
fetch.add_argument('-c', '--channels', default=None, nargs='+',
help='List of channels to perform actions on. '
'Default is all channels.')
fetch.add_argument('-d', '--database', default=None,
help='Path to the database file.')
fetch.add_argument('-i', '--config', default=None,
help='Use specific config file.')
fetch.set_defaults(func=fetch_data)
generate = subparser.add_parser('generate', help='Generate logs out of '
'data in provided database')
generate.add_argument('-o', '--output', default=None, help="Output "
"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'),
help='Output format. Default is none; only database '
'is updated by latest messages for all/selected '
'channels.')
generate.add_argument('-t', '--theme', default=None,
choices=('plain', 'unicode'),
help='Choose theme for text output. It doesn\'t '
'affect other output formats.')
generate.add_argument('-v', '--verbose', help='Be verbose. Adding more '
'"v" will increase verbosity', action="count",
default=None)
generate.add_argument('-q', '--quiet', help='Be quiet. Adding more "q" '
'will decrease verbosity', action="count",
default=None)
generate.add_argument('-c', '--channels', default=[], nargs='+',
help='List of channels to perform actions on. '
'Default is all channels.')
generate.add_argument('-d', '--database', default=None,
help='Path to the database file.')
generate.add_argument('-i', '--config', default=None,
help='Use specific config file.')
generate.set_defaults(func=generate_raport)
args = parser.parse_args()
cfg = config.Config()
msg = cfg.update(args)
setup_logger(args)
logging.info(msg)
args.func(args)
from slack_backup import command
if __name__ == "__main__":
main()
command.main()

View File

@@ -10,7 +10,7 @@ except ImportError:
setup(name="slack-backup",
packages=["slack_backup"],
version="0.4",
version="0.4.5",
description="Make copy of slack converstaions",
author="Roman Dobosz",
author_email="gryf73@gmail.com",

View File

@@ -112,6 +112,7 @@ class Client(object):
channels = all_channels
for channel in channels:
logging.info("Getting messages for channel `%s'", channel.name)
latest = self.q(o.Message).\
filter(o.Message.channel == channel).\
order_by(o.Message.ts.desc()).first()
@@ -146,8 +147,12 @@ class Client(object):
Create message with corresponding possible metadata, like reactions,
files etc.
"""
try:
user = self.q(o.User).\
filter(o.User.slackid == data['user']).one()
except KeyError:
user = self.q(o.User).\
filter(o.User.slackid == data['comment']['user']).one()
if data['type'] == 'message' and not data['text'].strip():
logging.info("Skipping message from `%s' since it's empty",
@@ -255,7 +260,7 @@ class Client(object):
if not database:
return 'assets'
path = os.path.dirname(os.path.abspath(database))
path = os.path.dirname(database)
return os.path.join(path, 'assets')
def _channels_list(self):

118
slack_backup/command.py Normal file
View File

@@ -0,0 +1,118 @@
"""
Create backup for certain date for specified channel in slack
"""
import argparse
import logging
from slack_backup import client
from slack_backup import config
def setup_logger(args):
"""Setup logger format and level"""
level = logging.WARNING
if args.quiet:
level = logging.ERROR
if args.quiet > 1:
level = logging.CRITICAL
if args.verbose:
level = logging.INFO
if args.verbose > 1:
level = logging.DEBUG
logging.basicConfig(level=level,
format="%(asctime)s %(levelname)s: %(message)s")
def generate_raport(args):
"""Generate logs"""
slack = client.Client(args)
slack.generate_history()
def fetch_data(args):
"""Fetch and store data"""
slack = client.Client(args)
slack.update()
def main():
"""Main function"""
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest='parser')
subparser.required = True
fetch = subparser.add_parser('fetch', help='Update local db with Slack'
' data')
fetch.add_argument('-t', '--token', default=None, help='Slack token - '
'a string, which can be generated/obtained via '
'https://api.slack.com/docs/oauth-test-tokens page.')
fetch.add_argument('-u', '--user', default=None, help='Username for your '
'Slack account')
fetch.add_argument('-p', '--password', default=None, help='Password for '
'your Slack account.')
fetch.add_argument('-e', '--team', default=None, help='Team name, which '
'is part of slack url, for example: if url is '
'"https://team.slack.com" than "team" is a name of '
'the team.')
fetch.add_argument('-v', '--verbose', help='Be verbose. Adding more "v" '
'will increase verbosity', action="count",
default=None)
fetch.add_argument('-q', '--quiet', help='Be quiet. Adding more "q" will'
' decrease verbosity', action="count", default=None)
fetch.add_argument('-c', '--channels', default=None, nargs='+',
help='List of channels to perform actions on. '
'Default is all channels.')
fetch.add_argument('-d', '--database', default=None,
help='Path to the database file.')
fetch.add_argument('-i', '--config', default=None,
help='Use specific config file.')
fetch.set_defaults(func=fetch_data)
generate = subparser.add_parser('generate', help='Generate logs out of '
'data in provided database')
generate.add_argument('-o', '--output', default=None, help="Output "
"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'),
help='Output format. Default is none; only database '
'is updated by latest messages for all/selected '
'channels.')
generate.add_argument('-t', '--theme', default=None,
choices=('plain', 'unicode'),
help='Choose theme for text output. It doesn\'t '
'affect other output formats.')
generate.add_argument('-v', '--verbose', help='Be verbose. Adding more '
'"v" will increase verbosity', action="count",
default=None)
generate.add_argument('-q', '--quiet', help='Be quiet. Adding more "q" '
'will decrease verbosity', action="count",
default=None)
generate.add_argument('-c', '--channels', default=[], nargs='+',
help='List of channels to perform actions on. '
'Default is all channels.')
generate.add_argument('-d', '--database', default=None,
help='Path to the database file.')
generate.add_argument('-i', '--config', default=None,
help='Use specific config file.')
generate.set_defaults(func=generate_raport)
args = parser.parse_args()
cfg = config.Config()
msg = cfg.update(args)
setup_logger(args)
logging.info(msg)
args.func(args)

View File

@@ -53,8 +53,12 @@ class Config(object):
def load_config(self, args):
locations = [args.config,
'./slack-backup.conf',
path = ''
if hasattr(args, 'config') and args.config:
path = args.config
locations = [path,
'./slack-backup.ini',
os.path.expandvars('$XDG_CONFIG_HOME/slack-backup.ini'),
os.path.expandvars('$HOME/.config/slack-backup.ini')]
@@ -79,19 +83,25 @@ class Config(object):
val = self.cp.get(section, option, fallback='[]')
val = json.loads(val)
else:
val = self.cp.get(section, option, fallback='')
val = self.cp.get(section, option, fallback=None)
self._options[option] = val
def update_args(self, args):
if 'parser' not in args:
# it doesn't make sense to update args, since no action was
# choosen
return
# special case, re-set information for verbose/quiet options
if args.verbose is not None and self._options['quiet'] is not None:
self._options['quiet'] = 0
if 'verbose' in args and args.verbose is not None:
self._options['verbose'] = args.verbose
if args.quiet is not None and self._options['verbose'] is not None:
self._options['verbose'] = 0
if self._options['quiet'] is not None:
self._options['quiet'] = 0
if 'quiet' in args and args.quiet is not None:
self._options['quiet'] = args.quiet
if self._options['verbose'] is not None:
self._options['verbose'] = 0
for sec_id in (args.parser, 'common'):
for option in self.sections[sec_id]:

View File

@@ -55,8 +55,8 @@ class Download(object):
'file': self._files}
if filetype == 'file' and not self._authorized:
logging.info("There was no (valid) credentials passed, therefore "
"file `%s' cannot be downloaded", url)
logging.warning("There was no (valid) credentials passed, "
"therefore file `%s' cannot be downloaded", url)
return
splitted = url.split('/')
@@ -79,8 +79,10 @@ class Download(object):
count = 1
while filetype != 'avatar' and os.path.exists(path):
if count == 1:
base, ext = os.path.splitext(path)
path = base + "%0.3d" % count + ext
path = base + ".%0.3d" % count + ext
count += 1
return path

View File

@@ -51,13 +51,6 @@ class TestConfig(unittest.TestCase):
args.config = None
args.parser = 'fetch'
args.verbose = 2
args.quiet = None
args.channels = None
args.database = None
args.user = None
args.password = None
args.team = None
args.token = None
conf = config.Config()
conf.update(args)
@@ -66,7 +59,7 @@ class TestConfig(unittest.TestCase):
'verbose': 2,
'quiet': 0,
'channels': [],
'database': '',
'database': None,
'user': None,
'password': None,
'team': None,