Skip to content

Instantly share code, notes, and snippets.

@feliam
Last active March 11, 2018 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feliam/cea566b1c5608e4968a26f38118bf248 to your computer and use it in GitHub Desktop.
Save feliam/cea566b1c5608e4968a26f38118bf248 to your computer and use it in GitHub Desktop.
Delete Slack older files that are not liked. Rewrite of https://gist.github.com/jackcarter/d86808449f0d95060a40
'''
Get your client_id client_secret be registering a new app in your slack
Got to:
https://qgdm.slack.com/oauth?client_id=???????????????????&scope=files:write:user files:read users:read users:read.email reactions:read reactions:write
After authorizing...
http://127.0.0.1:8080/?code=??????????????
Take the code and put it..
https://slack.com/api/oauth.access?client_id=?????&client_secret=????
Then you get something like...
{"ok":true,"access_token":"xoxp-????????????????????????????????????????","scope":"identify,files:read,reactions:read,users:read,users:read.email,files:write:user,reactions:write","user_id":"U0HPFKS7J","team_name":"QGDM","team_id":"T0HPF6JS2"}
Take the token and put it in the script
xoxp-?????????????????????????????????????????
'''
import os
import requests
import time
import json
token = 'xoxp-?????????????????????????????????????????'
deleted_folder = 'backup' #Where to put deleted files
max_storage = 5000000000 - 5000000 *10 *30
keep_minimal_size = 30*1024 #files of less than 10k stay
#Delete files older than this:
ts_to = int(time.time()) #- 365*3 * 24 * 60 * 60
ts_now = int(time.time())
ts_month_ago = int(time.time()) - 30 * 24 * 60 * 60
ts_2months_ago = int(time.time()) - 30*2 * 24 * 60 * 60
ts_year_ago = int(time.time()) - 365 * 24 * 60 * 60
ts_2years_Ago = int(time.time()) - 365*2 * 24 * 60 * 60
def list_files(start_page=1):
params = {
'token': token
,'count': 1000
,'ts_to': ts_now
,'type': 'images'
,'page': start_page
}
retrieved = []
uri = 'https://slack.com/api/files.list'
response = requests.get(uri, params=params)
response = json.loads(response.text)
assert response.get('ok', False), 'Something went wrong'
if response['paging']['page'] < response['paging']['pages']:
return response['files'] + list_files(start_page=start_page+1)
else:
return response['files']
def file_info(file_id):
params = {
'token': token
,'file': file_id
}
uri = 'https://slack.com/api/files.info'
response = requests.get(uri, params=params)
return json.loads(response.text)['file']
def get_reactions(file_id):
count = count + 1
params = {
'token': token
,'file': file_id
,'full': true
}
uri = 'https://slack.com/api/reactions.get'
response = requests.get(uri, params=params)
response = json.loads(response.text)
assert response.get('ok', False), 'Something went wrong'
print response['file']['reactions']
def delete_file(file_id):
params = {
'token': token
,'file': file_id
}
uri = 'https://slack.com/api/files.delete'
response = requests.get(uri, params=params)
return json.loads(response.text)['ok']
def download_file(file_id):
f = file_info(file_id)
import urllib2
response = urllib2.urlopen(f['url_private_download'])
return response.read()
def user_info(user_id):
params = {
'token': token
,'user': user_id
}
uri = 'https://slack.com/api/users.info'
response = requests.get(uri, params=params)
response = json.loads(response.text)
assert response.get('ok', False), 'Something went wrong'
return response['user']
files = list_files()
file_ids = []
used_space = sum((f['size'] for f in files))
print "Total used space bfore deletion:", used_space
print "Max allowed storage", max_storage
for f in sorted(files, key=lambda f: f['timestamp']):
#older files first
if used_space <= max_storage:
break
#Check if file is liked (or unliked) a lot
keep_it = 0
for reaction in f.get('reactions', []):
if reaction['name'] == '+1':
keep_it += reaction['count']
if reaction['name'] == '-1':
keep_it -= reaction['count']
if keep_it > 0:
continue
#If not sure use other parameters to decide
if keep_it == 0:
#Ignore very small files
if f.get("size", 0) < keep_minimal_size :
continue
#Check if file is starred
if f.get("is_starred", False):
continue
#Check if file has comments
if f.get("comments_count", 0):
continue
#ok still spending a lot of storage
#file is not liked a lot
#file is not starred
user = user_info(f['user'])
username = user['profile']['display_name_normalized']
filename = os.path.join(deleted_folder, "%s_%s_%s" %( f['timestamp'], username, f['name']) )
print "Backup and delete file", f['name'], filename
import urllib2
url = f['url_private_download']
request = urllib2.Request(url)
request.add_header('Authorization', 'Bearer %s'%token)
response = urllib2.urlopen(request)
content = response.read()
file(filename, 'w+').write(content)
print 'delete file id', f['id']
delete_file(f['id'])
used_space -= f['size']
print "Total used space after deletion:", used_space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment