Created
January 16, 2018 12:34
-
-
Save tzermias/5999a72b5f8b174c0731628881df5f20 to your computer and use it in GitHub Desktop.
Calculate file usage per user for shared files in Slack
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 | |
import requests | |
import json | |
# Calculate file usage per user for shared files in Slack | |
# [email protected] | |
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