-
Notifications
You must be signed in to change notification settings - Fork 5
/
slack_download_delete.py
53 lines (42 loc) · 1.45 KB
/
slack_download_delete.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
import requests
import time
import json
import urllib.request
token = ''
# Delete files older than this:
ts_to = int(time.time()) - 30 * 24 * 60 * 60
def list_files():
params = {
'token': token
, 'ts_to': ts_to
, 'count': 1000
}
uri = 'https://slack.com/api/files.list'
response = requests.get(uri, params=params)
return json.loads(response.text)['files']
def download_files(files, delete=False):
count = 0
num_files = len(files)
for file in files:
count = count + 1
header = {
'Authorization': ('Bearer ' + token)
}
if 'url_private_download' in file:
r = requests.get(file['url_private_download'], headers=header, stream=True)
if r.status_code == 200:
with open((file['id'] + '_' + file['name']), 'wb') as f:
for chunk in r:
f.write(chunk)
if delete:
params = {
'token': token
, 'file': file['id']
}
delete_uri = 'https://slack.com/api/files.delete'
response = requests.get(delete_uri, params=params)
print(count, "of", num_files, " deleted -", file['id'], json.loads(response.text)['ok'])
print(count, "of", num_files, "-", file['id'])
print(file['url_private_download'])
files = list_files()
download_files(files, delete=True)