forked from gonzalognzl/slack-export
-
Notifications
You must be signed in to change notification settings - Fork 13
/
slack_export.py
426 lines (353 loc) · 14.1 KB
/
slack_export.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from slacker import Slacker, Conversations
import json
import argparse
import os
import io
import shutil
import copy
import requests
import sys
from datetime import datetime
from pick import pick
from time import sleep
# fetches the complete message history for a channel/group/im
#
# pageableObject could be:
# slack.channel
# slack.groups
# slack.im
#
# channelId is the id of the channel/group/im you want to download history for.
def getHistory(pageableObject, channelId, pageSize = 100):
messages = []
lastTimestamp = None
while(True):
try:
if isinstance(pageableObject, Conversations):
response = pageableObject.history(
channel=channelId,
latest=lastTimestamp,
oldest=0,
limit=pageSize
).body
else:
response = pageableObject.history(
channel = channelId,
latest = lastTimestamp,
oldest = 0,
count = pageSize
).body
except requests.exceptions.HTTPError as e:
if e.response.status_code == 429:
retryInSeconds = int(e.response.headers['Retry-After'])
print(u"Rate limit hit. Retrying in {0} second{1}.".format(retryInSeconds, "s" if retryInSeconds > 1 else ""))
sleep(retryInSeconds)
if isinstance(pageableObject, Conversations):
response = pageableObject.history(
channel=channelId,
latest=lastTimestamp,
oldest=0,
limit=pageSize
).body
else:
response = pageableObject.history(
channel=channelId,
latest=lastTimestamp,
oldest=0,
count=pageSize
).body
messages.extend(response['messages'])
if (response['has_more'] == True):
sys.stdout.write(".")
sys.stdout.flush()
lastTimestamp = messages[-1]['ts'] # -1 means last element in a list
sleep(1) # Respect the Slack API rate limit
else:
break
if lastTimestamp != None:
print("")
messages.sort(key = lambda message: message['ts'])
return messages
def mkdir(directory):
if not os.path.isdir(directory):
os.makedirs(directory)
# create datetime object from slack timestamp ('ts') string
def parseTimeStamp( timeStamp ):
if '.' in timeStamp:
t_list = timeStamp.split('.')
if len( t_list ) != 2:
raise ValueError( 'Invalid time stamp' )
else:
return datetime.utcfromtimestamp( float(t_list[0]) )
# move channel files from old directory to one with new channel name
def channelRename( oldRoomName, newRoomName ):
# check if any files need to be moved
if not os.path.isdir( oldRoomName ):
return
mkdir( newRoomName )
for fileName in os.listdir( oldRoomName ):
shutil.move( os.path.join( oldRoomName, fileName ), newRoomName )
os.rmdir( oldRoomName )
def writeMessageFile( fileName, messages ):
directory = os.path.dirname(fileName)
# if there's no data to write to the file, return
if not messages:
return
if not os.path.isdir( directory ):
mkdir( directory )
with open(fileName, 'w') as outFile:
json.dump( messages, outFile, indent=4)
# parse messages by date
def parseMessages( roomDir, messages, roomType ):
nameChangeFlag = roomType + "_name"
currentFileDate = ''
currentMessages = []
for message in messages:
#first store the date of the next message
ts = parseTimeStamp( message['ts'] )
fileDate = '{:%Y-%m-%d}'.format(ts)
#if it's on a different day, write out the previous day's messages
if fileDate != currentFileDate:
outFileName = u'{room}/{file}.json'.format( room = roomDir, file = currentFileDate )
writeMessageFile( outFileName, currentMessages )
currentFileDate = fileDate
currentMessages = []
# check if current message is a name change
# dms won't have name change events
if roomType != "im" and ( 'subtype' in message ) and message['subtype'] == nameChangeFlag:
roomDir = message['name']
oldRoomPath = message['old_name']
newRoomPath = roomDir
channelRename( oldRoomPath, newRoomPath )
currentMessages.append( message )
outFileName = u'{room}/{file}.json'.format( room = roomDir, file = currentFileDate )
writeMessageFile( outFileName, currentMessages )
def filterConversationsByName(channelsOrGroups, channelOrGroupNames):
return [conversation for conversation in channelsOrGroups if conversation['name'] in channelOrGroupNames]
def promptForPublicChannels(channels):
channelNames = [channel['name'] for channel in channels]
selectedChannels = pick(channelNames, 'Select the Public Channels you want to export:', multi_select=True)
return [channels[index] for channelName, index in selectedChannels]
# fetch and write history for all public channels
def fetchPublicChannels(channels):
if dryRun:
print("Public Channels selected for export:")
for channel in channels:
print(channel['name'])
print()
return
for channel in channels:
channelDir = channel['name'].encode('utf-8')
print("Fetching history for Public Channel: {0}".format(channelDir))
channelDir = channel['name'].encode('utf-8')
mkdir( channelDir )
messages = getHistory(slack.conversations, channel['id'])
parseMessages( channelDir, messages, 'channel')
# write channels.json file
def dumpChannelFile():
print("Making channels file")
private = []
mpim = []
for group in groups:
if group['is_mpim']:
mpim.append(group)
continue
private.append(group)
# slack viewer wants DMs to have a members list, not sure why but doing as they expect
for dm in dms:
dm['members'] = [dm['user'], tokenOwnerId]
#We will be overwriting this file on each run.
with open('channels.json', 'w') as outFile:
json.dump( channels , outFile, indent=4)
with open('groups.json', 'w') as outFile:
json.dump( private , outFile, indent=4)
with open('mpims.json', 'w') as outFile:
json.dump( mpim , outFile, indent=4)
with open('dms.json', 'w') as outFile:
json.dump( dms , outFile, indent=4)
def filterDirectMessagesByUserNameOrId(dms, userNamesOrIds):
userIds = [userIdsByName.get(userNameOrId, userNameOrId) for userNameOrId in userNamesOrIds]
return [dm for dm in dms if dm['user'] in userIds]
def promptForDirectMessages(dms):
dmNames = [userNamesById.get(dm['user'], dm['user'] + " (name unknown)") for dm in dms]
selectedDms = pick(dmNames, 'Select the 1:1 DMs you want to export:', multi_select=True)
return [dms[index] for dmName, index in selectedDms]
# fetch and write history for all direct message conversations
# also known as IMs in the slack API.
def fetchDirectMessages(dms):
if dryRun:
print("1:1 DMs selected for export:")
for dm in dms:
print(userNamesById.get(dm['user'], dm['user'] + " (name unknown)"))
print()
return
for dm in dms:
name = userNamesById.get(dm['user'], dm['user'] + " (name unknown)")
print("Fetching 1:1 DMs with {0}".format(name))
dmId = dm['id']
mkdir(dmId)
messages = getHistory(slack.conversations, dm['id'])
parseMessages( dmId, messages, "im" )
def promptForGroups(groups):
groupNames = [group['name'] for group in groups]
selectedGroups = pick(groupNames, 'Select the Private Channels and Group DMs you want to export:', multi_select=True)
return [groups[index] for groupName, index in selectedGroups]
# fetch and write history for specific private channel
# also known as groups in the slack API.
def fetchGroups(groups):
if dryRun:
print("Private Channels and Group DMs selected for export:")
for group in groups:
print(group['name'])
print()
return
for group in groups:
groupDir = group['name']
mkdir(groupDir)
messages = []
print("Fetching history for Private Channel / Group DM: {0}".format(group['name']))
messages = getHistory(slack.conversations, group['id'])
parseMessages( groupDir, messages, 'group' )
# fetch all users for the channel and return a map userId -> userName
def getUserMap():
global userNamesById, userIdsByName
for user in users:
userNamesById[user['id']] = user['name']
userIdsByName[user['name']] = user['id']
# stores json of user info
def dumpUserFile():
#write to user file, any existing file needs to be overwritten.
with open( "users.json", 'w') as userFile:
json.dump( users, userFile, indent=4 )
# get basic info about the slack channel to ensure the authentication token works
def doTestAuth():
testAuth = slack.auth.test().body
teamName = testAuth['team']
currentUser = testAuth['user']
print("Successfully authenticated for team {0} and user {1} ".format(teamName, currentUser))
return testAuth
# Since Slacker does not Cache.. populate some reused lists
def bootstrapKeyValues():
global users, channels, groups, dms
users = slack.users.list().body['members']
print("Found {0} Users".format(len(users)))
sleep(1)
channels = slack.conversations.list(limit = 1000, types=('public_channel')).body['channels']
print("Found {0} Public Channels".format(len(channels)))
sleep(1)
groups = slack.conversations.list(limit = 1000, types=('private_channel', 'mpim')).body['channels']
print("Found {0} Private Channels or Group DMs".format(len(groups)))
# need to retrieve channel memberships for the slack-export-viewer to work
for n in range(len(groups)):
groups[n]["members"] = slack.conversations.members(limit=1000, channel=groups[n]['id']).body['members']
print("Retrieved members of {0}".format(groups[n]['name']))
sleep(1)
dms = slack.conversations.list(limit = 1000, types=('im')).body['channels']
print("Found {0} 1:1 DM conversations\n".format(len(dms)))
sleep(1)
getUserMap()
# Returns the conversations to download based on the command-line arguments
def selectConversations(allConversations, commandLineArg, filter, prompt):
global args
if isinstance(commandLineArg, list) and len(commandLineArg) > 0:
return filter(allConversations, commandLineArg)
elif commandLineArg != None or not anyConversationsSpecified():
if args.prompt:
return prompt(allConversations)
else:
return allConversations
else:
return []
# Returns true if any conversations were specified on the command line
def anyConversationsSpecified():
global args
return args.publicChannels != None or args.groups != None or args.directMessages != None
# This method is used in order to create a empty Channel if you do not export public channels
# otherwise, the viewer will error and not show the root screen. Rather than forking the editor, I work with it.
def dumpDummyChannel():
channelName = channels[0]['name']
mkdir( channelName )
fileDate = '{:%Y-%m-%d}'.format(datetime.today())
outFileName = u'{room}/{file}.json'.format( room = channelName, file = fileDate )
writeMessageFile(outFileName, [])
def finalize():
os.chdir('..')
if zipName:
shutil.make_archive(zipName, 'zip', outputDirectory, None)
shutil.rmtree(outputDirectory)
exit()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Export Slack history')
parser.add_argument('--token', required=True, help="Slack API token")
parser.add_argument('--zip', help="Name of a zip file to output as")
parser.add_argument(
'--dryRun',
action='store_true',
default=False,
help="List the conversations that will be exported (don't fetch/write history)")
parser.add_argument(
'--publicChannels',
nargs='*',
default=None,
metavar='CHANNEL_NAME',
help="Export the given Public Channels")
parser.add_argument(
'--groups',
nargs='*',
default=None,
metavar='GROUP_NAME',
help="Export the given Private Channels / Group DMs")
parser.add_argument(
'--directMessages',
nargs='*',
default=None,
metavar='USER_NAME',
help="Export 1:1 DMs with the given users")
parser.add_argument(
'--prompt',
action='store_true',
default=False,
help="Prompt you to select the conversations to export")
args = parser.parse_args()
users = []
channels = []
groups = []
dms = []
userNamesById = {}
userIdsByName = {}
slack = Slacker(args.token)
testAuth = doTestAuth()
tokenOwnerId = testAuth['user_id']
bootstrapKeyValues()
dryRun = args.dryRun
zipName = args.zip
outputDirectory = "{0}-slack_export".format(datetime.today().strftime("%Y%m%d-%H%M%S"))
mkdir(outputDirectory)
os.chdir(outputDirectory)
if not dryRun:
dumpUserFile()
dumpChannelFile()
selectedChannels = selectConversations(
channels,
args.publicChannels,
filterConversationsByName,
promptForPublicChannels)
selectedGroups = selectConversations(
groups,
args.groups,
filterConversationsByName,
promptForGroups)
selectedDms = selectConversations(
dms,
args.directMessages,
filterDirectMessagesByUserNameOrId,
promptForDirectMessages)
if len(selectedChannels) > 0:
fetchPublicChannels(selectedChannels)
if len(selectedGroups) > 0:
if len(selectedChannels) == 0:
dumpDummyChannel()
fetchGroups(selectedGroups)
if len(selectedDms) > 0:
fetchDirectMessages(selectedDms)
finalize()