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

Added executable script and client class

This commit is contained in:
2016-11-18 19:11:12 +01:00
parent 1d90ee6d9f
commit 1e1d5cb38c
3 changed files with 88 additions and 0 deletions

27
scripts/slack-backup.py Executable file
View File

@@ -0,0 +1,27 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Create backup for certain date for specified channel in slack
"""
import argparse
import pprint
from slack_backup import client
def main():
"""Main function"""
parser = argparse.ArgumentParser()
parser.add_argument("token", help="Slack token - a string, which can be"
" generated/obtained via "
"https://api.slack.com/docs/oauth-test-tokens page")
# parser.add_argument("channel", help="Slack channel name")
args = parser.parse_args()
slack = client.Client(args.token)
pprint.pprint(slack.get_hisotry(selected_channels=['elysium']))
if __name__ == "__main__":
main()

0
slack_backup/__init__.py Normal file
View File

61
slack_backup/client.py Normal file
View File

@@ -0,0 +1,61 @@
#!/usr/bin/env python3
"""
Create backup for certain date for specified channel in slack
"""
import argparse
import logging
import slackclient
from slack_backup import objects
class Client(object):
def __init__(self, token):
self.slack = slackclient.SlackClient(token)
def get_hisotry(self, selected_channels=None, from_date=0):
channels = self._get_channel_list()
if selected_channels:
selected_channels = [c for c in channels
if c.name in selected_channels]
else:
selected_channels = channels
users = self._get_user_list()
for channel in selected_channels:
history = []
latest = 'now'
while True:
messages, latest = self._get_channel_history(channel, latest)
# TODO: merge messages witihn a channel
if not messages:
break
def _get_channel_history(self, channel, latest='now'):
result = self.slack.api_call("channels.history", channel=channel._id,
count=1000, latest=latest)
if not result.get("ok"):
logging.error(result['error'])
return None, None
def _get_channel_list(self):
result = self.slack.api_call("channels.list")
if not result.get("ok"):
logging.error(result['error'])
return None
return [objects.Channel(chan) for chan in result['channels']]
def _get_user_list(self):
result = self.slack.api_call("users.list", presence=0)
if not result.get("ok"):
logging.error(result['error'])
return None
return [objects.User(user) for user in result['members']]