Created
May 28, 2017 00:29
-
-
Save shinshin86/9896b9fa26e8caf5843302a27f6f3c93 to your computer and use it in GitHub Desktop.
Script to delete files uploaded to Slack.
This file contains hidden or 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
# This code is taken from these blog. | |
# https://www.shiftedup.com/2014/11/13/how-to-bulk-remove-files-from-slack | |
# http://qiita.com/sotayamashita/items/cb811f512162e61f56f4 | |
# | |
# And I am changing the implementation a little. | |
# (It got to get arguments, and modify for Python 3) | |
import requests | |
import json | |
import calendar | |
import sys | |
from datetime import datetime, timedelta | |
def files_delete(token, domain, day): | |
while 1: | |
files_list_url = 'https://slack.com/api/files.list' | |
date = str(calendar.timegm((datetime.now() + timedelta(int(day))) | |
.utctimetuple())) | |
data = {"token": token, "ts_to": date} | |
response = requests.post(files_list_url, data = data) | |
if len(response.json()["files"]) == 0: | |
break | |
for f in response.json()["files"]: | |
print("Deleting file " + f["name"] + "...") | |
timestamp = str(calendar.timegm(datetime.now().utctimetuple())) | |
delete_url = "https://" + domain + ".slack.com/api/files.delete?t=" + timestamp | |
requests.post(delete_url, data = { | |
"token": token, | |
"file": f["id"], | |
"set_active": "true", | |
"_attempts": "1"}) | |
print("DONE!") | |
if __name__ == '__main__': | |
args = sys.argv | |
if len(args) != 4: | |
print("Oops! Execution failed! Please check the execution method.") | |
print("python slack_file_delete.py {your token} {target domain} {target days ago?}") | |
exit() | |
else: | |
print("file delete start...") | |
print("-----> your token : ",args[1]) | |
print("-----> target domain : ", args[2]) | |
print("-----> target days ago? : ", args[3]) | |
files_delete(args[1], args[2], args[3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment