forked from zach-snell/slack-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_export.py
696 lines (613 loc) · 26.5 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
from slack_sdk import WebClient
import json
import argparse
import os
import io
import shutil
import copy
from datetime import datetime
from pick import pick
from time import sleep
import glob
import http
import urllib
import sys
import emoji
chat_place_holder = """<li class="chat-block border border-primary rounded shadow-sm p-3 mb-5 bg-white">
<div class="chat-text"> chattext <div class="chat-avatar">
</div></div>
<div class="border-top pt-2 mt-3">
<span class="chat-name font-weight-light text-black-50 me-3"> chatsender </span>
|
<span class="chat-hour font-weight-light text-black-50 ms-3"> chattime </span>
</div>
</li>
"""
thread_place_holder = """<li class="thread-chat chat-block border border-light rounded shadow-sm p-3 mb-3 bg-white">
<div class="chat-text"> chattext <div class="chat-avatar">
</div></div>
<div class="border-top pt-2 mt-3">
<span class="chat-name font-weight-light text-black-50 me-3"> chatsender </span>
|
<span class="chat-hour font-weight-light text-black-50 ms-3"> chattime </span>
</div>
</li>
"""
thread_start = """<ul class="thread-chat-listborder border border-light rounded p-3 mb-5 ms-5 bg-light">
"""
thread_end = """</ul>
"""
# 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(client, channelId, pageSize = 500):
messages = []
lastTimestamp = None
response = slack.conversations_history(
channel = channelId,
latest = lastTimestamp,
oldest = 0,
limit = pageSize
)
messages = response['messages']
while response['has_more'] == True:
response = slack.conversations_history(
cursor = response['response_metadata']['next_cursor'],
channel = channelId,
latest = lastTimestamp,
oldest = 0,
limit = pageSize
)
messages.extend(response['messages'])
sleep(1)
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, ensure_ascii=False)
# 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:
end = 0
fails = 0
counter = 0
history_flag = False
channelDir = channel['name']#.encode('utf-8')
print(u"Fetching history for Public Channel: {0}".format(channelDir))
channelDir = channel['name']#.encode('utf-8')
while end == 0:
try:
if history_flag == False:
mkdir( channelDir )
messages = getHistory(slack, channel['id'])
print("Fetching threads")
print("Checking " + str(len(messages)) + " messages!")
while counter < len(messages):
if 'thread_ts' in messages[counter]:
replies = getThread(channel['id'], messages[counter]['thread_ts'])
replies.sort(key = lambda replies: replies['ts'])
messages[counter]['replies'] = replies[1:]
sleep(0.5)
counter += 1
if counter % 200 == 0:
print("Checked 200 messages. Only " + str(len(messages) - counter) + " messages left!")
parseMessages( channelDir, messages, 'channel')
end = 1
except urllib.error.URLError:
fails += 1
if (fails == 6):
sys.exit("too many failed attempts. Maybe check internet connection.")
print("Retrying...")
sleep(fails ** fails)
# 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, ensure_ascii=False)
with open('groups.json', 'w') as outFile:
json.dump( private , outFile, indent=4, ensure_ascii=False)
with open('mpims.json', 'w') as outFile:
json.dump( mpim , outFile, indent=4, ensure_ascii=False)
with open('dms.json', 'w') as outFile:
json.dump( dms , outFile, indent=4, ensure_ascii=False)
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:
end = 0
fails = 0
name = userNamesById.get(dm['user'], dm['user'] + " (name unknown)")
counter = 0
history_flag = False
dmId = dm['id']
print(u"Fetching 1:1 DMs with {0}".format(name))
while end == 0:
try:
if history_flag == False:
mkdir(dmId)
messages = getHistory(slack, dm['id'])
history_flag = True
print("Fetching threads")
print("Checking " + str(len(messages)) + " messages!")
while counter < len(messages):
if 'thread_ts' in messages[counter]:
replies = getThread(dm['id'], messages[counter]['thread_ts'])
replies.sort(key = lambda replies: replies['ts'])
messages[counter]['replies'] = replies[1:]
sleep(1)
counter += 1
if counter % 200 == 0:
print("Checked 200 messages. Only " + str(len(messages) - counter) + " messages left!")
parseMessages( dmId, messages, "im" )
end = 1
except urllib.error.URLError:
fails += 1
if (fails == 6):
sys.exit("too many failed attempts. Maybe check internet connection.")
print("Retrying...")
sleep(fails ** fails)
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]
def getThread(channelId, ts, pageSize = 500):
messages = []
lastTimestamp = None
response = slack.conversations_replies(
channel = channelId,
ts = ts,
latest = lastTimestamp,
oldest = 0,
limit = pageSize
)
messages = response['messages']
while response['has_more'] == True:
response = slack.conversations_replies(
cursor = response['response_metadata']['next_cursor'],
channel = channelId,
ts = ts,
latest = lastTimestamp,
oldest = 0,
limit = pageSize
)
messages.extend(response['messages'])
sleep(1)
messages.sort(key = lambda message: message['ts'])
return messages
# 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:
end = 0
fails = 0
history_flag = False
counter = 0
messages = []
groupDir = group['name']
print(u"Fetching history for Private Channel / Group DM: {0}".format(group['name']))
while end == 0:
try:
if (history_flag == False):
mkdir(groupDir)
messages = getHistory(slack,group['id'])
history_flag = True
print("Fetching threads")
print("Checking " + str(len(messages)) + " messages!")
while counter < len(messages):
if 'thread_ts' in messages[counter]:
replies = getThread(group['id'], messages[counter]['thread_ts'])
replies.sort(key = lambda replies: replies['ts'])
messages[counter]['replies'] = replies[1:]
sleep(1)
counter += 1
if counter % 200 == 0:
print("Checked 200 messages. Only " + str(len(messages) - counter) + " messages left!")
#messages = slack.conversations_history(channel=group['id'])
parseMessages( groupDir, messages, 'group' )
end = 1
except urllib.error.URLError:
fails += 1
if (fails == 6):
sys.exit("too many failed attempts. Maybe check internet connection.")
print("Retrying...")
sleep(fails ** fails)
# 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 , ensure_ascii=False)
# get basic info about the slack channel to ensure the authentication token works
def doTestAuth():
testAuth = slack.api_test()
if testAuth['ok'] == True:
print("Successfully authenticated.")
return testAuth
else:
exit(testAuth['error'])
def bootstrapKeyValues():
global users, channels, groups, dms
data = slack.users_list()
users.extend(data['members'])
while data['response_metadata']['next_cursor']:
data = slack.users_list(cursor = data['response_metadata']['next_cursor'])
users.extend(data['members'])
sleep(1)
print(u"Found {0} Users".format(len(users)))
sleep(1)
data = slack.conversations_list(types="public_channel")
channels.extend(data['channels'])
while data['response_metadata']['next_cursor']:
data = slack.conversations_list(types="public_channel", cursor = data['response_metadata']['next_cursor'])
channels.extend(data['channels'])
sleep(1)
print(u"Found {0} Public Channels".format(len(channels)))
sleep(1)
data = slack.conversations_list(types="private_channel,mpim")
groups.extend(data['channels'])
while data['response_metadata']['next_cursor']:
data = slack.conversations_list(types="private_channel,mpim", cursor = data['response_metadata']['next_cursor'])
groups.extend(data['channels'])
sleep(1)
print(u"Found {0} Private Channels or Group DMs".format(len(groups)))
sleep(1)
data = slack.conversations_list(types="im")
dms.extend(data['channels'])
while data['response_metadata']['next_cursor']:
data = slack.conversations_list(types="im", cursor = data['response_metadata']['next_cursor'])
dms.extend(data['channels'])
sleep(1)
print(u"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():
global chat_place_holder
global thread_place_holder
global thread_start
chatplace = chat_place_holder
threadplace = thread_place_holder
threadstart = thread_start
chats = ""
users = {}
with open('../users.json') as users_json:
data = json.load(users_json)
for user in data:
profile = user['profile']
users[user['id']] = profile['real_name']
dirnames = {}
htmlreader = open('../chat_template.html')
htmltemplate = htmlreader.read()
htmlreader.close()
for root, dirs, files in os.walk('./', topdown=False):
for name in dirs:
user_names = []
concatfilename = './' + name + '/concat.json'
with open(concatfilename, 'wb') as outfile:
for filename in sorted(glob.glob('./' + name + '/*.json')):
if filename == concatfilename:
continue
with open(filename, 'rb') as readfile:
shutil.copyfileobj(readfile, outfile)
print(f"Parsing {name}...")
outputfilename = './' + name + '/out.txt'
outputhtmlpath = './' + name + '/out.html'
linksfile = './' + name + '/links.txt'
reader = open(concatfilename, 'r')
data = reader.read().replace('][', ',')
reader.close()
reader = open(concatfilename, 'w')
reader.write(data)
reader.close()
links = []
with open(concatfilename) as data_json, open(outputfilename, 'w') as output:
data_json = data_json.read()
if len(data_json) == 0:
continue
data = json.loads(data_json)
for message in data:
try:
for file in message['files']:
links.append(file['url_private_download'])
except KeyError:
pass
try:
text = message['text']
try:
for file in message['files']:
text += "\n" + file['url_private_download']
except KeyError:
pass
output.write(datetime.fromtimestamp(int(float(message['ts']))).strftime("%a, %d %b %Y %H:%M:%S") + ' ' + users[message['user']] + ": " + text + '\n\r')
chatplace = chatplace.replace('chattime', datetime.fromtimestamp(int(float(message['ts']))).strftime("%a, %d %b %Y %H:%M:%S")).replace('chattext', emoji.emojize(text, use_aliases=True)).replace('chatsender', users[message['user']])
chats += chatplace
chatplace = chat_place_holder
if 'replies' in message:
chats += threadstart
for reply in message['replies']:
rep_text = reply['text']
try:
for file in reply['files']:
rep_text += "\n" + file['url_private_download']
except KeyError:
pass
output.write(' ' + datetime.fromtimestamp(int(float(reply['ts']))).strftime("%a, %d %b %Y %H:%M:%S") + ' ' + users[reply['user']] + ": " + rep_text + '\n\r')
threadplace = threadplace.replace('chattime', datetime.fromtimestamp(int(float(reply['ts']))).strftime("%a, %d %b %Y %H:%M:%S")).replace('chattext', emoji.emojize(rep_text, use_aliases=True)).replace('chatsender', users[reply['user']])
chats += threadplace
threadplace = thread_place_holder
chats += thread_end
user_names.append(users[message['user']])
user_names = list(dict.fromkeys(user_names))
except KeyError:
output.write(datetime.fromtimestamp(int(float(message['ts']))).strftime("%a, %d %b %Y %H:%M:%S") + ' ' + ": " + text + '\n\r')
chatplace = chatplace.replace('chattime', datetime.fromtimestamp(int(float(message['ts']))).strftime("%a, %d %b %Y %H:%M:%S")).replace('chattext', emoji.emojize(text, use_aliases=True)).replace('chatsender', "Unknown Sender")
chats += chatplace
chatplace = chat_place_holder
if 'replies' in message:
chats += threadstart
for reply in message['replies']:
rep_text = reply['text']
try:
for file in reply['files']:
rep_text += "\n" + file['url_private_download']
except KeyError:
pass
output.write(' ' + datetime.fromtimestamp(int(float(reply['ts']))).strftime("%a, %d %b %Y %H:%M:%S") + ' ' + ": " + rep_text + '\n\r')
threadplace = threadplace.replace('chattime', datetime.fromtimestamp(int(float(reply['ts']))).strftime("%a, %d %b %Y %H:%M:%S")).replace('chattext', emoji.emojize(rep_text, use_aliases=True)).replace('chatsender', "Unknown Sender")
chats += threadplace
threadplace = thread_place_holder
chats += thread_end
if (len(user_names) == 2):
dirnames['./' + name] = f'./{user_names[0]}-{user_names[1]}'
print(name + ' ' + user_names[0] + user_names[1])
with open(outputhtmlpath, 'w') as outhtml:
outhtml.write(htmltemplate.replace('chatplaceholder', chats).replace("channel_name", name))
with open(linksfile, 'w') as lfile:
for link in links:
lfile.write(link)
lfile.write('\n')
print("Done!")
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 = WebClient(token=args.token)
testAuth = doTestAuth()
tokenOwnerId = testAuth['user_id']
try:
u = open("users.json")
c = open("channels.json")
d = open("dms.json")
g = open("groups.json")
m = open("mpims.json")
u_data = json.loads(u.read())
c_data = json.loads(c.read())
d_data = json.loads(d.read())
g_data = json.loads(g.read())
m_data = json.loads(m.read())
for user in u_data:
users.append(user)
for ch in c_data:
channels.append(ch)
for dm in d_data:
dms.append(dm)
for gp in g_data:
groups.append(gp)
for mp in m_data:
groups.append(user)
u.close()
c.close()
d.close()
g.close()
m.close()
except FileNotFoundError:
print("Fetching data from server.")
end = 0
fails = 0
while end == 0:
try:
bootstrapKeyValues()
end = 1
except http.client.IncompleteRead:
fails += 1
print("Retrying...")
if (fails == 6):
sys.exit("Too many failed attempts. Maybe check internet connection.")
sleep(fails ** fails)
dumpUserFile()
dumpChannelFile()
dryRun = args.dryRun
zipName = args.zip
outputDirectory = "{date}-{token}-slack_export".format(token = args.token, date = datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))
mkdir(outputDirectory)
os.chdir(outputDirectory)
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()