Created
February 20, 2016 21:34
-
-
Save joehack3r/8e539e9bd39fd61cdcaa to your computer and use it in GitHub Desktop.
Delete .wav and .mp3 files from 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
#!/usr/bin/env python | |
import re | |
import requests | |
import json | |
import calendar | |
from datetime import datetime, timedelta | |
_days_to_keep = 180 | |
_token = "" | |
_domain = "" | |
if (len(_token) != 49): | |
print("Issue with Slack API token.") | |
exit(1) | |
if (len(_domain) == 0): | |
print("Issue with Slack domain.") | |
exit(1) | |
def main(): | |
last_ts = 0 | |
launch_ts = str(calendar.timegm((datetime.now() + timedelta(-_days_to_keep)).utctimetuple())) | |
# print("This loop may enter an infinite loop because only certain files are being removed.") | |
# print("If the Slack API allows filtering on file type, this can be fixed.") | |
# print("Until then, when the output stops, break the script.") | |
while True: | |
files_list_url = 'https://slack.com/api/files.list' | |
data = {"token": _token, "ts_from" : last_ts, "ts_to": launch_ts} | |
response = requests.post(files_list_url, data = data) | |
if len(response.json()["files"]) == 0: | |
break | |
for f in response.json()["files"]: | |
max_ts = max(last_ts, f["timestamp"]) | |
# Only remove wav and mp3 files (typically our standups) | |
if re.search('\.wav$', f["name"]) or re.search('\.mp3$', f["name"]): | |
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"}) | |
# Adding some logic to avoid the infinite loop | |
if max_ts > last_ts: | |
last_ts = max_ts | |
else: | |
last_ts +=1 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment