Created
May 16, 2018 12:53
-
-
Save jitran/e30147b398edd69726521eef5e634f32 to your computer and use it in GitHub Desktop.
Delete Slack Files. Rewrite of https://gist.github.com/jackcarter/d86808449f0d95060a40
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
""" slack_delete.py | |
Removes files uploaded to Slack | |
Usage: | |
virtualenv slack | |
source slack/bin/activate | |
pip install requests | |
./slack_delete.py | |
deactivate | |
""" | |
import requests | |
import time | |
import json | |
import datetime | |
from collections import OrderedDict | |
# Obtain here: https://api.slack.com/custom-integrations/legacy-tokens | |
TOKEN = '' | |
# Params for file listing. More info here: https://api.slack.com/methods/files.list | |
# How many? (Maximum is 1000, otherwise it defaults to 100) | |
COUNT = 1000 | |
# Types? | |
TYPES = 'all' | |
# TYPES = 'spaces,snippets,images,gdocs,zips,pdfs' | |
# TYPES = 'zips' | |
def greater_kb(file, kb): | |
""" File is greater than KB | |
:param file (dict): file to be compared | |
:param kb (int): size in kb | |
:return files(bool): returns True if the file size is greater than kb | |
""" | |
return file['size'] / 1024 > kb | |
def smaller_kb(file, kb): | |
""" File is smaller than KB | |
:param file (dict): file to be compared | |
:param kb (int): size in kb | |
:return files(bool): returns True if the file size is less than kb | |
""" | |
return file['size'] / 1024 < kb | |
def filter_by_size(files, greater_or_smaller, kb): | |
""" Filter files by size | |
:param files (list): list of files to be filtered | |
:param greater_or_smaller (func): comparison function | |
:param kb (int): size in kb to filter files by | |
:return files(list): filtered files list (dict) | |
""" | |
return [file for file in files if greater_or_smaller(file, kb)] | |
def metadata(file): | |
""" Generate metadata for file | |
:param file (dict): file to generate metadata for | |
:return metadata(OrderedDict) | |
""" | |
order = ['Title', 'Name', 'Created', 'Size', 'Filetype', | |
'Comment', 'Permalink', 'Download', 'User', 'Channels'] | |
metadata = { | |
'Title': file['title'], | |
'Name': file['name'], | |
'Created': datetime.datetime.utcfromtimestamp(file['created']).strftime('%B %d, %Y %H:%M:%S'), | |
'Size': str(file['size'] / 1024) + ' KB', | |
'Filetype': file['filetype'], | |
'Comment': file['initial_comment'] if 'initial_comment' in file else '', | |
'Permalink': file['permalink'], | |
'Download': file['url_private'], | |
'User': file['user'], | |
'Channels': file['channels'] | |
} | |
return OrderedDict((key, metadata[key]) for key in order) | |
def list_files(days=30, user=''): | |
""" List files | |
:param days (int): list files older than the days specified | |
:param user (str): filter files by user id | |
:return files(list): list of matching files (dict) | |
""" | |
ts_to = int(time.time()) - days * 24 * 60 * 60 | |
params = { | |
'token': TOKEN, | |
'ts_to': ts_to, | |
'count': COUNT, | |
'types': TYPES, | |
'user': user, | |
} | |
url = 'https://slack.com/api/files.list' | |
response = requests.get(url, params=params) | |
return response.json()['files'] | |
def delete_files(files): | |
""" Delete files | |
:param files (list): list of files to be deleted | |
:return none | |
""" | |
num_files = len(files) | |
file_ids = map(lambda f: f['id'], files) | |
url = 'https://slack.com/api/files.delete' | |
print("Deleting %i files" % (num_files)) | |
for index, file_id in enumerate(file_ids): | |
params = { | |
'token': TOKEN, | |
'file': file_id | |
} | |
response = requests.get(url, params=params) | |
content = response.json() | |
print((index + 1, "of", num_files, "-", | |
file_id, content['ok'])) | |
def print_files(files): | |
""" Print files | |
:param files (list): list of files to be printed | |
:return none | |
""" | |
for file in files: | |
print("[%s] %s [%s]" % (file['Created'], file['Title'], file['Size'])) | |
if __name__ == '__main__': | |
# Set it to delete only this user's files. Handy if you are not admin. | |
member_id = '' | |
# days = 30 | |
# print("Retrieving files older than %s days" % (days)) | |
# files = list_files(days=days, user=member_id) | |
# print("Total %i files" % (len(files))) | |
# print_files([metadata(file) for file in files]) | |
days = 90 | |
size = 500 | |
print("Retrieving files older than %s day(s) and greater than %s KB" % (days, size)) | |
files = list_files(days=days, user=member_id) | |
files = filter_by_size(files, greater_kb, size) | |
print("Total %i files" % (len(files))) | |
print_files([metadata(file) for file in files]) | |
#delete_files(files) # Commented out, so you don't accidentally run this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment