From c7294fbefd9f7a37c155ca5b147d9aa1cf6e1e5f Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Fri, 8 Sep 2017 23:27:52 +0200 Subject: [PATCH] https://github.com/kfei/slack-cleaner/pull/24 --- README.md | 3 +++ slack_cleaner/args.py | 3 +++ slack_cleaner/cli.py | 23 ++++++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 465bd61..469f0ef 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ slack-cleaner --token --file --user johndoe # Delete all snippets and images slack-cleaner --token --file --types snippets,images +# Purge messages from all channels for a user +slack-cleaner --token --message --purge yes --user johndoe + # Always have a look at help message slack-cleaner --help ``` diff --git a/slack_cleaner/args.py b/slack_cleaner/args.py index 944f947..f57721b 100644 --- a/slack_cleaner/args.py +++ b/slack_cleaner/args.py @@ -31,6 +31,8 @@ def __init__(self): # Channel, DM or group g_chan = p.add_mutually_exclusive_group() + g_chan.add_argument('--purge', + help='Purge messages from all channels') g_chan.add_argument('--channel', help='Channel name\'s, e.g., general') g_chan.add_argument('--direct', @@ -81,6 +83,7 @@ def __init__(self): self.delete_message = args.message self.delete_file = args.file + self.purge_name = args.purge self.channel_name = args.channel self.direct_name = args.direct self.group_name = args.group diff --git a/slack_cleaner/cli.py b/slack_cleaner/cli.py index 27053d2..8853246 100644 --- a/slack_cleaner/cli.py +++ b/slack_cleaner/cli.py @@ -46,6 +46,8 @@ # User dict user_dict = {} +# Channel dict +channel_dict = {} # Construct a local user dict for further usage def init_user_dict(): @@ -60,12 +62,29 @@ def init_user_dict(): # Init user dict init_user_dict() +# Construct a local channel dict for further usage +def init_channel_dict(): + res = slack.channels.list().body + if not res['ok']: + return + channels = res['channels'] + + for c in channels: + channel_dict[c['id']] = c['name'] + +# Init channel dict +init_channel_dict() def get_id_by_name(list_dict, key_name): for d in list_dict: if d['name'] == key_name: return d['id'] +def purge_channels(time_range, user_id=None, bot=False): + + for c in channel_dict: + print "Cleaning Channel: ",channel_dict[c] + clean_channel(c, time_range, user_id, args.bot) def clean_channel(channel_id, time_range, user_id=None, bot=False, keep_pinned=False): # Setup time range for query @@ -74,7 +93,9 @@ def clean_channel(channel_id, time_range, user_id=None, bot=False, keep_pinned=F _api_end_point = None # Set to the right API end point - if args.channel_name: + if args.purge_name: + _api_end_point = slack.channels.history + elif args.channel_name: _api_end_point = slack.channels.history elif args.direct_name: _api_end_point = slack.im.history