Skip to content

Instantly share code, notes, and snippets.

@jasonrwang
Forked from shauns/slack.py
Last active July 14, 2017 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrwang/a7034700fe7a01478ed59e026f91413e to your computer and use it in GitHub Desktop.
Save jasonrwang/a7034700fe7a01478ed59e026f91413e to your computer and use it in GitHub Desktop.
Deleting and listing slack files, sorting by size
#!/usr/bin/python
# Adapted from shauns
# https://gist.github.com/shauns/49f92d3979cced222bdbfbf3abb3a044
from slacker import Slacker
import itertools
# This grants you access to the Slack account in question.
# Get a token here: https://api.slack.com/custom-integrations/legacy-tokens
slack = Slacker('<API TOKEN>')
pages = slack.files.list(page=1).body['paging']['pages']
all_the_files = list(itertools.chain(*[slack.files.list(page=i).body['files'] for i in range(1,pages + 1)]))
# Remove files from array which are only links to external files
all_the_files = filter(lambda x: x['is_external'] == False, all_the_files)
# Sort array so largest files are at the top
biggest_first = sorted(all_the_files, key=lambda f: f['size'], reverse=True)
# Print results and show cumulative size of listed files
cumulative = 0
for i in range(0,24):
file_info = "Size:\t" + str(biggest_first[i]['size'] / 1000000) + " MB\tName:\t" + biggest_first[i]['name']
print(file_info)
cumulative = cumulative + biggest_first[i]['size'] / 1000000
cumulative_size = "Cumulative size of 25 largest files:\t" + str(cumulative) + " MB"
print(cumulative_size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment