Skip to content

Instantly share code, notes, and snippets.

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 Kevin-Prichard/ebc31311160f8431917748dd89e421b0 to your computer and use it in GitHub Desktop.
Save Kevin-Prichard/ebc31311160f8431917748dd89e421b0 to your computer and use it in GitHub Desktop.
How to deal with: "Your file was uploaded — it’s safe and sound in Slack. Unfortunately your workspace doesn’t have any storage space left."
#!/usr/bin/env python2.7
import traceback
import requests
import shutil
import time
import json
import os
import re
# Replace with your Slack Legacy Web API token.
# Get yours here: https://api.slack.com/custom-integrations/legacy-tokens
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 delete_file(file_id):
count = 0
params = {
'token': token,
'file': file_id,
}
uri = 'https://slack.com/api/files.delete'
print "Deleting {}".format(file_id)
response = requests.get(uri, params=params)
print file_id, json.loads(response.text)['ok']
def download_file(uri, pathname):
resp = requests.get(uri, stream=True, headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.27; rv:99.1) Gecko/11101011 Firefox/99.1",
"Authorization": "Bearer {}".format(token)
})
if resp.status_code == 200:
length = resp.headers["Content-Length"]
with open(pathname, 'wb') as f:
resp.raw.decode_content = True
shutil.copyfileobj(resp.raw, f)
return int(length)
def download_and_delete_files(dest_dir):
files = list_files()
for i, f in enumerate(files):
try:
print i, f["size"], f["url_private_download"]
fn = re.sub(r"\W+", "_", f["title"].lower())+"--"+os.path.basename(f["url_private_download"])
path = os.path.join(dest_dir, fn)
size = download_file(f["url_private_download"], path)
if os.stat(path).st_size == size:
delete_file(f["id"])
except Exception as e:
print "Hmm lost {} due to {}\n{}".format(f["id"], str(e), ''.join(traceback.format_list(e)))
if __name__ == '__main__':
download_and_delete_files(os.getcwd())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment