/
api_test.py
325 lines (272 loc) · 10.3 KB
/
api_test.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
#!/usr/env python
import sys
import os
import time
import codecs
import datetime
from slacker import Slacker
from json import loads
from sys import argv
from config import API_TOKEN
# make sure this gets reset every time you use a new Slacker object
ACTIVE_USER_INFO = None
MEMBERS_BY_ID = None
ATTEMPT_LIMIT = 300
_BASE_PATH = "."
_SUBDIRS = {
"PM_SUBDIR" : "private_messages",
"GROUPS_SUBDIR" : "groups",
"FILES_SUBDIR" : "files",
"FAV_SUBDIR" : "favorites"
}
def _set_active_user_info(slack):
global ACTIVE_USER_INFO
response = None
attempts = 0
while not response and attempts < ATTEMPT_LIMIT:
response = slack.auth.test()
attempts += 1
if response:
if response.body["ok"]:
ACTIVE_USER_INFO = response.body
print ACTIVE_USER_INFO
return
elif "error" in response.body.keys():
print "API error (auth.test) : %s" % (
response.body["error"],
)
else:
print "API error (auth.test) : unknown error"
exit(1)
else:
print "API request (auth.test) failed after %d attempts; giving up" % attempts
exit(1)
def my_user_id(slack):
global ACTIVE_USER_INFO
if slack:
if not ACTIVE_USER_INFO:
_set_active_user_info(slack)
if ACTIVE_USER_INFO:
return ACTIVE_USER_INFO["user_id"]
return None
def my_user_name(slack):
global ACTIVE_USER_INFO
if slack:
if not ACTIVE_USER_INFO:
_set_active_user_info(slack)
if ACTIVE_USER_INFO:
return ACTIVE_USER_INFO["user"]
return None
def members_by_id(slack):
global MEMBERS_BY_ID
if slack:
if not MEMBERS_BY_ID:
response_user_list = slack.users.list()
ulist_ok = response_user_list.body["ok"]
if not ulist_ok:
print "There was an error fetching the user list..."
return None
all_members = response_user_list.body["members"]
MEMBERS_BY_ID = dict()
for member in all_members:
MEMBERS_BY_ID[member["id"]] = member
return MEMBERS_BY_ID
return None
def member_name(userid, slack):
if slack:
if "USLACKBOT" == userid:
return "Slackbot"
elif userid in members_by_id(slack).keys():
member = members_by_id(slack)[userid]
return member["name"]
return None
def groups_subdir(slack):
global _SUBDIRS
return user_path(slack) + os.path.sep + _SUBDIRS['PM_SUBDIR']
def favorites_subdir(slack):
global _SUBDIRS
return user_path(slack) + os.path.sep + _SUBDIRS['FAV_SUBDIR']
def files_subdir(slack):
global _SUBDIRS
return user_path(slack) + os.path.sep + _SUBDIRS['FILES_SUBDIR']
def pm_subdir(slack):
global _SUBDIRS
return user_path(slack) + os.path.sep + _SUBDIRS['PM_SUBDIR']
def user_path(slack):
global _BASE_PATH
if not slack:
print "Undefined Slack Object!"
exit(1)
return os.path.normpath(_BASE_PATH + os.path.sep + my_user_id(slack))
def create_user_dir(slack):
if not os.path.exists(user_path(slack)):
os.makedirs(user_path(slack))
all_subdirs = (
groups_subdir(slack),
favorites_subdir(slack),
files_subdir(slack),
pm_subdir(slack),
)
for subdir in all_subdirs:
if not os.path.exists(subdir):
os.makedirs(subdir)
open(user_path(slack) + os.path.sep + my_user_name(slack), 'w').close()
def save_groups_channel(channel_id, outpath, slack, earliest_ts=0):
if not slack:
print "Undefined slack object! Aborting PM channel archive."
return -1
outfile = open(outpath, 'w')
attempts = 0
message_count = -1
groups_history_has_more = True
ts_next = time.time()
while groups_history_has_more:
response_groups_history = None
while not response_groups_history and attempts < ATTEMPT_LIMIT:
response_groups_history = slack.groups.history(channel_id, ts_next, earliest_ts)
attempts += 1
if (attempts % 50) == 0:
print "...%d attempts on %s..." % (attempts, channel_id,)
if not response_groups_history:
print "Could not access Slack API for groups.history after %d attempts." % (attempts,)
if -1 == message_count:
return message_count
if not response_groups_history.body["ok"]:
error_code = response_groups_history.body["error"]
if error_code:
print "Request groups.history returned error: %s" % (error_code,)
else:
print "Request groups.history returned unknown error."
if -1 == message_count:
return message_count
groups_history_messages = response_groups_history.body["messages"]
outfile.write("messages = [\n")
for groups_message in groups_history_messages:
if 0 >= message_count:
msg = unicode(groups_message)
outfile.write(msg)
else:
msg = u",\n%s" % (groups_message,)
outfile.write(msg)
ts_next = groups_message["ts"]
message_count += 1
if -1 == message_count:
message_count = 0
message_count += len(groups_history_messages)
groups_history_has_more = response_groups_history.body["has_more"]
outfile.write("\n]\n")
outfile.close()
return message_count
def save_groups(slack, earliest_ts=0):
"""
for userid in members_by_id(slack).keys():
print "%s : %s" % (userid, member_name(userid, slack),)
"""
response_groups_list = slack.groups.list()
status_ok = response_groups_list.body["ok"]
if not status_ok:
print "Error getting groups."
return 1
groups_list = response_groups_list.body["groups"]
# print groups_list
for groups_channel in groups_list:
group_name = groups_channel["name"]
channel_id = groups_channel["id"]
ts = datetime.datetime.fromtimestamp(
time.time()
).strftime('%Y-%m-%d_%H%M_%S')
filename = "%s_%s_%s.json" % (group_name, channel_id, ts,)
filepath = groups_subdir(slack) + os.path.sep + filename
count = save_groups_channel(channel_id, filepath, slack)
if 0 > count:
print "Could not get messages for %s (%s)." % (group_name, channel_id,)
else:
print "%s (%s) : %d messages saved" % (group_name, channel_id, count, )
def save_pms_channel(channel_id, outpath, slack, earliest_ts=0):
if not slack:
print "Undefined slack object! Aborting PM channel archive."
return -1
outfile = open(outpath, 'w')
attempts = 0
message_count = -1
im_history_has_more = True
ts_next = time.time()
while im_history_has_more:
response_im_history = None
while not response_im_history and attempts < ATTEMPT_LIMIT:
response_im_history = slack.im.history(channel_id, ts_next, earliest_ts)
attempts += 1
if (attempts % 50) == 0:
print "...%d attempts on %s..." % (attempts, channel_id,)
if not response_im_history:
print "Could not access Slack API for im.history after %d attempts." % (attempts,)
if -1 == message_count:
return message_count
if not response_im_history.body["ok"]:
error_code = response_im_history.body["error"]
if error_code:
print "Request im.history returned error: %s" % (error_code,)
else:
print "Request im.history returned unknown error."
if -1 == message_count:
return message_count
im_history_messages = response_im_history.body["messages"]
outfile.write("messages = [\n")
for im_message in im_history_messages:
if 0 >= message_count:
msg = unicode(im_message)
outfile.write(msg)
else:
msg = u",\n%s" % (im_message,)
outfile.write(msg)
ts_next = im_message["ts"]
message_count += 1
if -1 == message_count:
message_count = 0
message_count += len(im_history_messages)
im_history_has_more = response_im_history.body["has_more"]
outfile.write("\n]\n")
outfile.close()
return message_count
def save_pms(slack, earliest_ts=0):
response_im_list = slack.im.list()
status_ok = response_im_list.body["ok"]
if not status_ok:
print "Error getting IM channels."
return 1
im_list = response_im_list.body["ims"]
# print im_list
for im_channel in im_list:
channel_id = im_channel["id"]
user_id = im_channel["user"]
user_name = member_name(user_id, slack)
ts = datetime.datetime.fromtimestamp(
time.time()
).strftime('%Y-%m-%d_%H%M_%S')
filename = "%s_%s_%s.json" % (user_name, user_id, ts,)
filepath = pm_subdir(slack) + os.path.sep + filename
count = save_pms_channel(channel_id, filepath, slack)
if 0 > count:
print "Could not get messages with %s (%s)." % (user_name, user_id,)
else:
print "%s (%s) : %d messages saved" % (user_name, user_id, count, )
def save_user_data(api_token):
slack = Slacker(api_token)
print slack.auth.test().body
# create_user_dir(slack)
# save_pms(slack)
# save_groups(slack)
def reset_active_user():
global ACTIVE_USER_INFO
ACTIVE_USER_INFO = None
def main(*args):
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
print "Arching data for %d users " % len(API_TOKEN)
for username in API_TOKEN.keys():
print "=== %s ===" % (username,)
save_user_data(API_TOKEN[username])
reset_active_user()
if __name__ == "__main__":
success = main(argv)
exit(success)