mirror of
https://github.com/gryf/slack-backup.git
synced 2025-12-17 11:30:25 +01:00
Changing the way how arguments are passed
This commit is contained in:
@@ -3,40 +3,54 @@
|
|||||||
"""
|
"""
|
||||||
Create backup for certain date for specified channel in slack
|
Create backup for certain date for specified channel in slack
|
||||||
"""
|
"""
|
||||||
|
from six.moves import builtins
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
|
||||||
|
|
||||||
from slack_backup import client
|
from slack_backup import client
|
||||||
|
from slack_backup import logger
|
||||||
|
|
||||||
|
|
||||||
def channel_list(string):
|
|
||||||
return string.split(',')
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main function"""
|
"""Main function"""
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('-t', '--token', required=True, help='Slack token - '
|
parser.add_argument('-t', '--token', required=True, help='Slack token - '
|
||||||
'a string, which can be generated/obtained via '
|
'a string, which can be generated/obtained via '
|
||||||
'https://api.slack.com/docs/oauth-test-tokens page')
|
'https://api.slack.com/docs/oauth-test-tokens page.')
|
||||||
parser.add_argument('-d', '--database', default='',
|
parser.add_argument('-d', '--database', default='',
|
||||||
help='Path to the database file')
|
help='Path to the database file.')
|
||||||
parser.add_argument('-f', '--format', default='text',
|
parser.add_argument('-f', '--format', default='none',
|
||||||
choices=('text', 'json', 'html', 'none'),
|
choices=('text', 'none'),
|
||||||
help='Output format. Default is none; only database '
|
help='Output format. Default is none; only database '
|
||||||
'is updated by latest messages for all/selected '
|
'is updated by latest messages for all/selected '
|
||||||
'channels.')
|
'channels.')
|
||||||
parser.add_argument('-c', '--channels', default=[], nargs='+',
|
parser.add_argument('-c', '--channels', default=[], nargs='+',
|
||||||
help='List of channels to perform actions on. '
|
help='List of channels to perform actions on. '
|
||||||
'Default is all channels')
|
'Default is all channels.')
|
||||||
parser.add_argument('-t', '--team', default='', help='team name, which is'
|
parser.add_argument('-u', '--user', default='', help='Username for your '
|
||||||
|
'Slack account')
|
||||||
|
parser.add_argument('-p', '--password', default='', help='Password for your '
|
||||||
|
'Slack account.')
|
||||||
|
parser.add_argument('-e', '--team', default='', help='Team name, which is'
|
||||||
' part of slack url, for example: if url is '
|
' part of slack url, for example: if url is '
|
||||||
'"https://team.slack.com" than "team" is a name of '
|
'"https://team.slack.com" than "team" is a name of '
|
||||||
'the team.')
|
'the team.')
|
||||||
|
parser.add_argument('-a', '--assets', default='assets', help='Directory '
|
||||||
|
'where to put downloaded files and images, "assets" '
|
||||||
|
'by default.')
|
||||||
|
parser.add_argument('-v', '--verbose', help='Be verbose. Adding more "v"'
|
||||||
|
'will increase verbosity', action="count", default=0)
|
||||||
|
parser.add_argument('-q', '--quiet', help='Be quiet. Adding more "q"'
|
||||||
|
'will decrease verbosity', action="count", default=0)
|
||||||
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
builtins.log = logger.Logger('slack_backup')()
|
||||||
|
log.set_verbose(args.verbose, args.quiet)
|
||||||
|
|
||||||
slack = client.Client(args)
|
slack = client.Client(args)
|
||||||
slack.update()
|
slack.update()
|
||||||
# slack.generate_history(Reporter(args.format))
|
slack.generate_history()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -19,57 +19,59 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
def __init__(self, args):
|
def __init__(self, args):
|
||||||
self.slack = slackclient.SlackClient(args.token)
|
self.slack = slackclient.SlackClient(args.token)
|
||||||
self.engine = db.connect(args.dbfilename)
|
self.engine = db.connect(args.database)
|
||||||
self.session = db.Session()
|
self.session = db.Session()
|
||||||
self.selected_channels = args.channels
|
self.selected_channels = args.channels
|
||||||
self.user = args.user
|
self.user = args.user
|
||||||
self.password = args.password
|
self.password = args.password
|
||||||
if not self.user and not self.password:
|
if not self.user and not self.password:
|
||||||
logging.warning('No media will be downloaded, due to not '
|
log.warning('No media will be downloaded, due to not '
|
||||||
'providing credentials for a slack account')
|
'providing credentials for a slack account')
|
||||||
elif not self.user and self.password:
|
elif not self.user and self.password:
|
||||||
logging.warning('No media will be downloaded, due to not '
|
log.warning('No media will be downloaded, due to not '
|
||||||
'providing username for a slack account')
|
'providing username for a slack account')
|
||||||
elif self.user and not self.password:
|
elif self.user and not self.password:
|
||||||
self.password = getpass.getpass(prompt='Provide password for '
|
self.password = getpass.getpass(prompt='Provide password for '
|
||||||
'your slack account: ')
|
'your slack account: ')
|
||||||
self.q = self.session.query
|
self.q = self.session.query
|
||||||
self.dld = download.Download(args.user, args.password, args.team)
|
self.downloader = download.Download(args)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""
|
"""
|
||||||
Perform an update, store data to db
|
Perform an update, store data to db
|
||||||
"""
|
"""
|
||||||
self.dld.authorize()
|
self.downloader.authorize()
|
||||||
self.update_users()
|
self.update_users()
|
||||||
self.update_channels()
|
self.update_channels()
|
||||||
self.update_history()
|
self.update_history()
|
||||||
|
|
||||||
def update_channels(self):
|
def update_channels(self):
|
||||||
"""Fetch and update channel list with current state in db"""
|
"""Fetch and update channel list with current state in db"""
|
||||||
|
log.info("Fetching and update channels information in DB")
|
||||||
result = self._channels_list()
|
result = self._channels_list()
|
||||||
|
|
||||||
if not result:
|
if not result:
|
||||||
return
|
return
|
||||||
|
|
||||||
for channel_data in result:
|
for data in result:
|
||||||
channel = self.q(o.Channel).\
|
channel = self.q(o.Channel).\
|
||||||
filter(o.Channel.slackid == channel_data['id']).one_or_none()
|
filter(o.Channel.slackid == data['id']).one_or_none()
|
||||||
|
|
||||||
if not channel:
|
if not channel:
|
||||||
channel = o.Channel()
|
channel = o.Channel()
|
||||||
self.session.add(channel)
|
self.session.add(channel)
|
||||||
|
|
||||||
self._update_channel(channel, channel_data)
|
self._update_channel(channel, data)
|
||||||
|
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
|
|
||||||
def update_users(self):
|
def update_users(self):
|
||||||
"""Fetch and update user list with current state in db"""
|
"""Fetch and update user list with current state in db"""
|
||||||
|
log.info("Fetching and updating user information in DB")
|
||||||
result = self.slack.api_call("users.list", presence=0)
|
result = self.slack.api_call("users.list", presence=0)
|
||||||
|
|
||||||
if not result.get("ok"):
|
if not result.get("ok"):
|
||||||
logging.error(result['error'])
|
log.error(result['error'])
|
||||||
return
|
return
|
||||||
|
|
||||||
for user_data in result['members']:
|
for user_data in result['members']:
|
||||||
|
|||||||
@@ -351,8 +351,9 @@ class FakeArgs(object):
|
|||||||
user = 'fake_user'
|
user = 'fake_user'
|
||||||
password = 'fake_password'
|
password = 'fake_password'
|
||||||
team = 'fake_team'
|
team = 'fake_team'
|
||||||
dbfilename = None
|
database = None
|
||||||
channels = None
|
channels = None
|
||||||
|
assets = 'assets'
|
||||||
|
|
||||||
|
|
||||||
class TestApiCalls(TestCase):
|
class TestApiCalls(TestCase):
|
||||||
@@ -421,7 +422,7 @@ class TestMessage(TestCase):
|
|||||||
args.channels = ['general']
|
args.channels = ['general']
|
||||||
|
|
||||||
self.cl = client.Client(args)
|
self.cl = client.Client(args)
|
||||||
self.cl.dld.authorize = MagicMock()
|
self.cl.downloader.authorize = MagicMock()
|
||||||
self.cl.slack.api_call = MagicMock(return_value=USERS)
|
self.cl.slack.api_call = MagicMock(return_value=USERS)
|
||||||
self.cl.update_users()
|
self.cl.update_users()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user