-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeleteoldfiles.py
executable file
·82 lines (60 loc) · 2.15 KB
/
deleteoldfiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/local/bin/python
import urllib, json, time
import os, ConfigParser
# Token https://api.slack.com/docs/oauth-test-tokens
# find files older than X and delete them
token = None
user = None
def main():
global token
global user
olderthan = 10
numberoffiles = 50
config = load_config()
if config.has_section('slack'):
if(config.has_option('slack', 'token')):
token = config.get('slack', 'token')
user = user_id()['user_id']
if token is not None or user is not None:
delete_files(olderthan, numberoffiles)
def load_config(path=None):
if path:
configfile = path
else:
configfile = os.path.expanduser('secrets.conf')
config = ConfigParser.RawConfigParser()
try:
config.read(configfile)
except Exception as e:
print "Unable to read config file at {}: {}".format(configfile, e)
return config
def user_id():
url = "https://slack.com/api/auth.test?token={}".format(token)
return call(url)
def file_list(daysago=100, filecount=1):
ts = long(time.time()) - (60 * 60 * daysago)
url = "https://slack.com/api/files.list?token={}&user={}&ts_to={}&count={}".format(token, user, ts, filecount)
return call(url)
def delete_files(olderthan, numberoffiles):
print "Deleting up to {} files older than {} days".format(numberoffiles, olderthan)
files = file_list(olderthan, numberoffiles)
if len(files['files']) == 0:
print "No files found"
for file in files['files']:
resp = delete_file(file['id'])
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 delete_file(file_id):
url = "https://slack.com/api/files.delete?token={}&file={}".format(token, file_id)
return call(url)
def list_users():
url = "https://slack.com/api/users.list?token={}&count=500".format(token)
return call(url)
def call(url):
response = urllib.urlopen(url)
data = json.loads(response.read())
return data
if __name__ == "__main__":
main()