Skip to content

Instantly share code, notes, and snippets.

@morales2k
Last active June 8, 2016 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save morales2k/53fcf1f483f357e81d1f to your computer and use it in GitHub Desktop.
Save morales2k/53fcf1f483f357e81d1f to your computer and use it in GitHub Desktop.
Delete files from slack for each access token in the list. Based on: https://gist.github.com/thesoftwarejedi/d78af9ee12b7f7a9d3e7
import requests
import json
import calendar
import sys
import codecs
from datetime import datetime, timedelta
sys.stdout = codecs.getwriter("utf-8")(sys.stdout, 'strict')
_tokens = ["TOKEN_LIST"]
_domain = "your_domain"
if __name__ == '__main__':
for _token in _tokens:
print " /*************************************************************************************"
print "| Working with access token: " + _token
print " \*************************************************************************************"
whileCount = 1
while 1:
files_list_url = 'https://slack.com/api/files.list'
date = str(calendar.timegm((datetime.now() + timedelta(-10)).utctimetuple()))
data = {"token": _token, "ts_to": date}
response = requests.post(files_list_url, data=data)
fileCount = len(response.json()["files"])
if len(response.json()["files"]) == 0:
break
elif(whileCount >= fileCount and whileCount > 1):
print "We couldn't delete some files and looping over forever won't fix it either. Maybe some files are posted by other users on your private conversations."
break
else:
for f in response.json()["files"]:
#get user info for this file
userInfoUrl = "https://slack.com/api/users.info"
data = {"token": _token, "user": f['user']}
userCall = requests.post(userInfoUrl, data=data)
userRes = userCall.json()
print "# Deleting file " + str(whileCount) + " of " + str(fileCount) +": " + f["name"] + "...".decode('utf-8')
timestamp = str(calendar.timegm(datetime.now().utctimetuple()))
delete_url = "https://" + _domain + ".slack.com/api/files.delete?t=" + timestamp
data = {"token": _token, "file": f["id"], "set_active": "true", "_attempts": "1"}
delresp = requests.post(delete_url, data=data)
jsondelresp = delresp.json()
if(jsondelresp['ok'] is False):
print "# ERROR: " + jsondelresp["error"]
print "# File owner: " + userRes['user']['name'].decode('utf-8')
print "#"
whileCount += 1
print "DONE!"
print ""
raw_input("Press ENTER to exit the window...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment