Skip to content

Instantly share code, notes, and snippets.

@lanmaster53
Last active June 22, 2018 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanmaster53/c3c8bffb7f58dc626301 to your computer and use it in GitHub Desktop.
Save lanmaster53/c3c8bffb7f58dc626301 to your computer and use it in GitHub Desktop.
import argparse
import datetime
import urllib
import urllib2
import json
import sys
_token = 'YOUR_SLACK_API_KEY'
_files_list_url = 'https://slack.com/api/files.list'
_files_delete_url = 'https://slack.com/api/files.delete'
_user_info_url = 'https://slack.com/api/auth.test'
parser = argparse.ArgumentParser()
parser.add_argument('--exts', help='comma delimited list of file extensions to filter (default=all)', metavar='gpg,gif,etc.', default='', action='store')
parser.add_argument('--list-exts', help='list file extensions', action='store_true')
parser.add_argument('--list-files', help='list files', action='store_true')
parser.add_argument('--rm-files', help='remove files', action='store_true')
args = parser.parse_args()
def main():
# get user id
data = {'token': _token}
jsonobj = request(_user_info_url, data)
# get user's files
data = {'token': _token, 'user': jsonobj['user_id'], 'page': 1}
print '[*] Retrieving files... '
files = []
# retrieve all files associated with the user
while True:
jsonobj = request(_files_list_url, data)
files += jsonobj["files"]
if len(jsonobj["files"]) == 0:
break
data['page'] = jsonobj['paging']['page'] + 1
print '[*] %d total files found.' % (len(files))
# execute according to the given arguments
if args.exts:
exts = [x.strip() for x in args.exts.split(',')]
files = filter_files(files, exts)
print '[*] %d filtered files remain.' % (len(files))
if args.list_exts:
file_exts = list(set([f['name'].split('.')[-1] for f in files]))
print '[*] %d extensions found.' % (len(file_exts))
print '[!] Extensions: %s' % ', '.join(file_exts)
if args.list_files:
print '[*] Files:'
for file in files:
print '[!] %s' % file['name']
if args.rm_files:
ans = ''
# query for deletion of each file
for f in files:
if ans.upper() != 'A':
name = f['name']
created = datetime.datetime.fromtimestamp(f['created']).strftime('%Y-%m-%d %H:%M:%S')
print '[%s] %s' % (created, name)
try:
ans = raw_input('Delete? (Y)es/(N)o/(A)ll remaining files [N]: ')
except KeyboardInterrupt:
print ''
break
if ans.upper() in ('Y', 'A'):
delete_file(f)
def filter_files(files, exts):
_files = []
for file in files:
if file['name'].endswith(tuple(exts)):
_files.append(file)
return _files
def request(url, data):
payload = urllib.urlencode(data)
req = urllib2.Request(url, payload)
resp = urllib2.urlopen(req)
return json.load(resp)
def delete_file(f):
sys.stdout.write('[*] Deleting file %s... ' % (f['name']))
sys.stdout.flush()
data = {'token': _token, 'file': f['id']}
jsonobj = request(_files_delete_url, data)
print 'Success' if jsonobj['ok'] else 'Fail'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment