Created
July 23, 2017 08:28
-
-
Save croth1/c6c59bbadcf74c853b9d9dec42b6bd0f to your computer and use it in GitHub Desktop.
Comandline tool to clean up slack files by passed days.
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
# This script is based on https://gist.github.com/jackcarter/d86808449f0d95060a40 | |
from __future__ import print_function | |
import argparse | |
from time import time | |
import json | |
import sys | |
import math | |
import requests | |
PY3 = sys.version_info > (3, 0) | |
if not PY3: | |
input = raw_input | |
def main(): | |
parser = argparse.ArgumentParser('clean_my_slack_files') | |
parser.add_argument('slack_api_token', help='your api token from slack.') | |
parser.add_argument('--day_limit', default=90, type=int, | |
help='only files older than now - day_limit will be deleted.') | |
args = parser.parse_args() | |
delete_timestamp = int(time()) - args.day_limit * 24 * 60 * 60 | |
file_list_params = { | |
'token': args.slack_api_token, | |
'ts_to': delete_timestamp, | |
'count': 1000 | |
} | |
uri = 'https://slack.com/api/files.list' | |
response = requests.get(uri, params=file_list_params) | |
files_json = json.loads(response.text)['files'] | |
total_size = sum(file['size'] for file in files_json) | |
print('This operation would delete %s files and free %s storage.' % | |
(len(files_json), convert_size(total_size))) | |
if len(files_json) == 0: | |
print('Nothing to do here. Bye.') | |
sys.exit(0) | |
attempt_no = 0 | |
while attempt_no < 3: | |
answer = input('Do you want to continue? (y/n): ') | |
if answer.lower() == 'n': | |
print('No files deleted. Bye.') | |
sys.exit(0) | |
elif answer.lower() == 'y': | |
delete_files(files_json, token=args.slack_api_token) | |
sys.exit(0) | |
else: | |
print('Sorry, I didn\'t understand you.') | |
attempt_no += 1 | |
print() | |
print('Sorry, I have no idea what you are talking about. Bye.') | |
sys.exit(1) | |
def delete_files(files_json, token): | |
num_files = len(files_json) | |
for current, file_json in enumerate(files_json, start=1): | |
file_id = file_json['id'] | |
params = { | |
'token': token, | |
'file': file_id | |
} | |
uri = 'https://slack.com/api/files.delete' | |
response = requests.get(uri, params=params) | |
if json.loads(response.text)['ok']: | |
print('(%s/%s) successfully deleted file %s and freed %s.' % | |
(current, num_files, file_id, convert_size(file_json['size']))) | |
else: | |
print('WARNING: could not delete file %s.' % file_id) | |
# https://stackoverflow.com/a/14822210/2272172 | |
def convert_size(size_bytes): | |
if size_bytes == 0: | |
return "0B" | |
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | |
i = int(math.floor(math.log(size_bytes, 1024))) | |
p = math.pow(1024, i) | |
s = round(size_bytes / p, 2) | |
return "%s %s" % (s, size_name[i]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment