Skip to content

Instantly share code, notes, and snippets.

@tzermias
Created January 16, 2018 12:34
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 tzermias/5999a72b5f8b174c0731628881df5f20 to your computer and use it in GitHub Desktop.
Save tzermias/5999a72b5f8b174c0731628881df5f20 to your computer and use it in GitHub Desktop.
Calculate file usage per user for shared files in Slack
#!/usr/bin/env python
import requests
import json
# Calculate file usage per user for shared files in Slack
# zacharias.tzermias@gmail.com
API_ENDPOINT="https://slack.com/api/%s"
TOKEN = "YOUR-TOKEN-HERE"
USAGE = dict()
def main():
# Fetch files.list
r = requests.post(API_ENDPOINT % ("files.list"), data={'token': TOKEN,'count':'1000'})
out = r.json()
# Iterate files array
for f in out['files']:
#print f['size'], f['user'], f['name']
# Calculate usage for a each user
slack_user = f['user']
if USAGE.has_key(slack_user):
USAGE[slack_user] += f['size']
else:
USAGE[slack_user] = 0
# Print slack usage statistics per user
print "Slack Usage"
print "User\t\tSize in bytes"
for u in USAGE.keys():
r = requests.post(API_ENDPOINT % ("users.profile.get"), data={'token': TOKEN,'user': u})
out = r.json()
display_name = out['profile']['display_name']
print "%s\t\t%s" % (display_name if display_name != "" else u, USAGE[u])
if __name__ == '__main__':
main()
# vim ts=4 sts=4 et sw=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment