[Tutor] Downloading Slack Files

Malcolm Boone oasis.boone at gmail.com
Tue Aug 16 16:25:16 EDT 2016


Hello everyone!

I'm trying to run a python script that will download all files uploaded to
my companies Slack account from the past 30 days. I'm new to Python, so to
achieve this, I've just been editing a script that I use to delete all
Slack files from the past 30 days.

Here is the code I'm using currently with error:

import requests
import json
import urllib
import calendar
from datetime import datetime, timedelta

_token = "REDACTED"
_domain = "REDACTED"

if __name__ == '__main__':
    while 1:
        files_list_url = 'https://slack.com/api/files.list'
        date = str(calendar.timegm((datetime.now() + timedelta(-30))
            .utctimetuple()))
        data = {"token": _token, "ts_to": date}
        response = requests.post(files_list_url, data = data)
        if len(response.json()["files"]) == 0:
            break
        for f in response.json()["files"]:
            print ("Downloading file" + f["name"] + "...")
            timestamp = str(calendar.timegm(datetime.now().utctimetuple()))
            urllib.urlretrieve = "https://" + _domain + ".
slack.com/api/files.list" + timestamp
            requests.post(urllib.urlretrieve, data = {
                "token": _token,
                "file": f["id"],
                "set_active": "true",
                "_attempts": "1"})
    print ("DONE!")

So far, when I run this script, all that appears to be happening is it's
printing a list of all of the file names. But from what I can tell, it's
not actually downloading anything. The internet in our office isn't the
greatest and it's printing a new file name every second, but since most of
these are video files, I know that it isn't actually downloading anything
in this amount of time. I've also tried searching my PC for any of the file
names, with nothing turning up. My HD space also goes through no change
after running the script.

The other issue, is that the script never ends (probably a simple solution,
but again I'm pretty new to this). It keeps printing the list of file names
over and over until I manually close out of Python.

Any help would be greatly appreciated!

I'm using a Windows 10 PC running Python 3.


More information about the Tutor mailing list