Sebi

Slack large file lister

Mar 8th, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. # List all files on Slack bigger than set value
  2.  
  3. # Modified from: http://www.shiftedup.com/2014/11/13/how-to-bulk-remove-files-from-slack
  4.  
  5. # https://api.slack.com/custom-integrations/legacy-tokens
  6. _token = "PASTE YOUR TOKEN HERE"
  7.  
  8. # in MiB
  9. _min_file_size = 10
  10.  
  11. import requests
  12. import json
  13. import calendar
  14. from datetime import datetime, timedelta
  15.  
  16. cnt = 0
  17. def printfile(f):
  18.   global cnt
  19.   cnt = cnt + 1
  20.   id = f["id"]
  21.   size = f["size"]
  22.   type = f["pretty_type"]
  23.   title = f["title"]
  24.  
  25.   size = f["size"]
  26.   size = (int)(size/1024/1024)
  27.  
  28.   # Only "hosted" files have size
  29.   if size < _min_file_size:
  30.     return
  31.   print "%4i: %s %4imb %20s  %s" % (cnt, id, size, type, title)
  32.  
  33. if __name__ == '__main__':
  34.     page = 1
  35.     while page < 100:
  36.         files_list_url = 'https://slack.com/api/files.list'
  37.         date = str(calendar.timegm((datetime.now() + timedelta(-30))
  38.             .utctimetuple()))
  39.         data = {"token": _token, "ts_to": date, "page": page, "count": 500}
  40.         response = requests.post(files_list_url, data = data)
  41.         if len(response.json()["files"]) == 0:
  42.             break
  43.         for f in response.json()["files"]:
  44.           printfile(f)
  45.         page = page + 1
  46.     print "DONE!"
Add Comment
Please, Sign In to add comment