Created
March 22, 2017 08:51
-
-
Save Xowap/4b7011eb5087901a146003c71855869a to your computer and use it in GitHub Desktop.
A script to purge slack files
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
# vim: fileencoding=utf-8 tw=100 expandtab ts=4 sw=4 : | |
import requests | |
import time | |
import os | |
token = os.getenv('SLACK_TOKEN') | |
# Delete files older than this: | |
ts_to = int(time.time()) - (3600 * 24 * 30) | |
def list_files(): | |
params = { | |
'token': token, | |
'ts_to': ts_to, | |
'count': 1000, | |
} | |
uri = 'https://slack.com/api/files.list' | |
response = requests.get(uri, params=params) | |
return response.json()['files'] | |
def delete_files(file_ids): | |
num_files = len(file_ids) | |
for count, file_id in enumerate(file_ids, start=1): | |
params = { | |
'token': token, | |
'file': file_id, | |
} | |
uri = 'https://slack.com/api/files.delete' | |
response = requests.get(uri, params=params) | |
output = '{} of {} - {}'.format( | |
count, | |
num_files, | |
response.json()['ok'], | |
) | |
print(output) | |
def main(): | |
if not token: | |
print('You must provide a token in the SLACK_TOKEN environment variable.') | |
files = list_files() | |
file_ids = [f['id'] for f in files] | |
delete_files(file_ids) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment