Created
September 10, 2018 12:04
-
-
Save kryjex/133bb46ed646e27931e54344f1653258 to your computer and use it in GitHub Desktop.
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/local/bin/python | |
import urllib, json, time, urllib2 | |
# find files older than X days and delete them | |
# https://api.slack.com/docs/oauth-test-tokens | |
token = "xoxp-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
# https://api.slack.com/methods/users.identity looks like "U2XXXXXXX" | |
user = "U2XXXXXXX" | |
channel = "CBXXXXXXX" | |
def main(): | |
olderthan = 15 | |
numberoffiles = 100 | |
print "Deleting up to {} files older than {} days".format(numberoffiles, olderthan) | |
files = file_list(olderthan, numberoffiles) | |
for file in files['files']: | |
resp = delete_file(file['id'], file['url_private_download'], file['name']) | |
if resp['ok']: | |
print "Deleted {} : {}".format(file['id'], file['permalink']) | |
elif resp['error']: | |
print "ERROR: {} when deleting: {} : {}".format(resp['error'], file['id'], file['permalink']) | |
def file_list(daysago=100, filecount=1): | |
ts = long(time.time()) - (60 * 60 * daysago) | |
url = "https://slack.com/api/files.list?token={}&channel={}&user={}&ts_to={}&count={}".format(token,channel,user, ts, filecount) | |
return call(url) | |
def delete_file(file_id, file_url, name): | |
with open(name,'wb') as f: | |
request = urllib2.Request(file_url, headers={'Authorization': 'Bearer ' + token}) | |
f.write(urllib2.urlopen(request).read()) | |
f.close() | |
url = "https://slack.com/api/files.delete?token={}&file={}".format(token, file_id) | |
return call(url) | |
def call(url): | |
response = urllib.urlopen(url) | |
data = json.loads(response.read()) | |
return data | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment