[Tutor] Downloading Slack Files
David Rock
david at graniteweb.com
Tue Aug 16 20:21:18 EDT 2016
> On Aug 16, 2016, at 15:25, Malcolm Boone <oasis.boone at gmail.com> wrote:
>
> 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!")
>
>
> 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.
I’m not sure about the downloading part, but the reason it never stops is because this test is never true:
> if len(response.json()["files"]) == 0:
> break
Since you are downloading and not removing anything, there’s always going to be files so you will never break out of the while loop.
I think you need to get the list of files first, outside of the loop, then loop over that list to download.
—
David Rock
david at graniteweb.com
More information about the Tutor
mailing list