forked from mannynotfound/slackscrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_channels_info.py
executable file
·54 lines (43 loc) · 1.67 KB
/
get_channels_info.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
#!/usr/bin/env python
from json_utils import load_json, dump_json
from slackscrape import scrape_slack
from slackclient import SlackClient
import operator
import argparse
import os
config = load_json('./env.json')
def ensure_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
return directory
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('-u', '--update', help = 'update channels', action="store_true")
args = vars(ap.parse_args())
channel_args = {
'exclude_archived': 0,
}
sc = SlackClient(config['token'])
response = sc.api_call('channels.list', **channel_args)
channels = response['channels']
for idx, channel in enumerate(channels):
chan_name = channel['name']
print('{} | {} - {} MEMBERS'.format(idx, chan_name, channel['num_members']))
chan_path = ensure_dir('./output/channels/{}'.format(chan_name))
info_path = ensure_dir('./output/channels/{}/info'.format(chan_name))
try:
old_json = load_json('{}/{}.json'.format(info_path, chan_name))
if not args['update']:
print('Already have channel {}, skipping ...'.format(chan_name))
continue
except Exception as e:
print('No existing channel {} info, fetching ...'.format(chan_name))
slack_args = {
'channel': channel['id'],
}
channel_info = sc.api_call('channels.info', **slack_args)
try:
dump_json('{}/{}.json'.format(info_path, chan_name), channel_info)
except Exception as e:
print('ERROR DUMPING {}'.format(chan_name))
print(e)