forked from ganesh-4212/slack-backup-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.py
202 lines (152 loc) · 5.92 KB
/
backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import argparse
import datetime
import json
import requests
import app_constants as APP_CONSTANTS
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
parser = argparse.ArgumentParser(
description='Backup Slack channel, conversation, Users, and direct messages.')
parser.add_argument('-t', '--token', dest='token',required=True,
help='Slack api Access token')
parser.add_argument('-od', '--outDir', dest='outDir',required=False,default='./output',
help='Output directory to store JSON backup files.')
args = parser.parse_args()
token = args.token
auth_headers = {'Authorization': 'Bearer ' + token}
outDir = args.outDir
client = WebClient(token=token)
def getOutputPath(relativePath):
return outDir+relativePath
def parseTemplatedFileName(template, *args):
return template.format(*args)
def readPostManCollectionJson():
with open('slack-postman.json') as file:
jsonObj = json.load(file)
return jsonObj
def readRequestJsonFile():
with open('requests.json') as file:
jsonObj = json.load(file)
return jsonObj
def makedirPath(outputPath):
dirPath = os.path.dirname(outputPath)
if not os.path.exists(dirPath):
os.makedirs(dirPath)
def writeJSONFile(jsonObj, filePath):
outputPath = getOutputPath(filePath)
makedirPath(outputPath)
with open(outputPath, 'w+') as file:
json.dump(jsonObj, file, indent=True)
def getUsers():
response = client.users_list()
return response['members']
def lookupUser(users, userID):
for u in users:
if u['id'] == userID:
return u
def getChannels():
response = client.conversations_list(types='public_channel,private_channel')
return response['channels']
def getGroups():
response = client.conversations_list(types='mpim')
return response['channels']
def getOneToOneConversations():
response = client.conversations_list(types='im')
return response['channels']
def getConversationHistory(channelId):
params = { 'channel': channelId, 'count': 1000}
msgs = []
while True:
response = client.conversations_history(**params)
msgs.extend(response['messages'])
if not response['has_more']:
break
params['latest'] = msgs[-1]['ts']
for m in msgs:
if not ('reply_count' in m and m['reply_count'] > 0):
continue
m['replies'] = []
params = { 'channel': channelId, 'ts': m['ts'] }
while True:
response = client.conversations_replies(**params)
if not response['ok']:
break
m['replies'].extend(response['messages'])
if not response['has_more']:
break
params['cursor'] = response['response_metadata']['next_cursor']
return msgs
def getFileList():
params = { 'count': 100, 'show_files_hidden_by_limit': True, 'page': 1}
files = []
while True:
response = client.files_list(**params)
if not response['ok']:
break
files.extend(response['files'])
if response['paging']['pages'] <= params['page']:
break
params['page'] += 1
return files
def downloadFiles(users):
files = getFileList()
writeJSONFile(files, APP_CONSTANTS.FILE_LIST_FILE)
skipped_files=0
for file in files:
if file['mode'] == 'hidden_by_limit':
skipped_files+=1
continue
url = file['url_private_download']
r = requests.get(url, headers={'Authorization': 'Bearer ' + token}, stream=True)
r.raise_for_status()
file['date'] = datetime.datetime.fromtimestamp(file['timestamp']).strftime('%Y-%m-%d %H:%M:%S')
file['author'] = lookupUser(users, file['user'])['name']
filename = APP_CONSTANTS.FILES_FILENAME.format(**file)
print('Downloading to ' + filename)
outputPath = getOutputPath(filename)
makedirPath(outputPath)
with open(outputPath, 'wb') as f:
for chunk in r.iter_content(chunk_size=32*1024):
f.write(chunk)
print(f"{skipped_files} files hidden by limit")
def run():
channels = getChannels()
writeJSONFile(channels, APP_CONSTANTS.CHANNEL_LIST_FILE)
for channel in channels:
channelId = channel['id']
channelName = channel['name']
channelHistory = getConversationHistory(channelId)
if channel['is_private']:
template = APP_CONSTANTS.PRIVATE_CHANNEL_HISTORY_FILE
else:
template = APP_CONSTANTS.CHANNEL_HISTORY_FILE
channelHistoryFilename = parseTemplatedFileName(template, channelName)
writeJSONFile(channelHistory, channelHistoryFilename)
groups = getGroups()
writeJSONFile(groups, APP_CONSTANTS.GROUP_LIST_FILE)
for group in groups:
groupId = group['id']
groupName = group['name']
groupHistory = getConversationHistory(groupId)
groupHistoryFilename = parseTemplatedFileName(
APP_CONSTANTS.GROUP_HISTORY_FILE, groupName)
writeJSONFile(groupHistory, groupHistoryFilename)
users = getUsers()
writeJSONFile(users, APP_CONSTANTS.USER_LIST_FILE)
userIdToNameDict = {user['id']: user['name'] for user in users}
# Get one to one conversation list
oneToOneConversations = getOneToOneConversations()
writeJSONFile(oneToOneConversations,
APP_CONSTANTS.ONE_TO_ONE_CONVERSATION_LIST_FILE)
for conversation in oneToOneConversations:
conversationId = conversation['id']
userId = conversation['user']
userName = userIdToNameDict[userId]
conversationHistory = getConversationHistory(conversationId)
conversationHistoryFilename = parseTemplatedFileName(
APP_CONSTANTS.ONE_TO_ONE_CONVERSATION_HISTORY_FILE, userName, userId)
writeJSONFile(conversationHistory, conversationHistoryFilename)
downloadFiles(users)
if __name__ == '__main__':
run()