[Tutor] Downloading Slack Files

Alan Gauld alan.gauld at yahoo.co.uk
Tue Aug 16 20:23:26 EDT 2016


On 16/08/16 21:25, Malcolm Boone wrote:

> 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've no idea what a slack account is but I'm assuming
some kind of web server... I've made a few general
observations, I don;t know if they will help much.

> import requests
> import json

You import json but do not appear to use it below.
(There are calls to a json attribute of the response,
but not to the json module)

> import urllib
> import calendar
> from datetime import datetime, timedelta
> 
> _token = "REDACTED"
> _domain = "REDACTED"
> 
> if __name__ == '__main__':
>     while 1:

It would probabl;y be better style to put the code in a function
(traditionally 'main()' ) and call the function from the if clause.

Also its better style nowadays to use

while True

rather than while 1

although they do the same thing.

>         files_list_url = 'https://slack.com/api/files.list'
>         date = str(calendar.timegm((datetime.now() + timedelta(-30))
>             .utctimetuple()))

I can't help but feel there is an easier way to do that...
But if it works I won't quibble too much.

>         data = {"token": _token, "ts_to": date}
>         response = requests.post(files_list_url, data = data)
>         if len(response.json()["files"]) == 0:
>             break

The break above is the only opportunity to break out of the
infinite while loop. Sop long as you get something back its
going to keep going. And you don't appear to delete anything
so I assume you always get stuff back.

>         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

I assume the domain part is what signifies your company's files?
Is there really no separator between "files.list" and the timestamp?

>             requests.post(urllib.urlretrieve, data = {
>                 "token": _token,
>                 "file": f["id"],
>                 "set_active": "true",
>                 "_attempts": "1"})

> 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.

You don't appear to do anything in the for loop that will
change the response above so I don't think the break will
ever be called. That probably explains your infinite loop.
You probably need to sanity check the downloaded file
(when you get one) and then explicitly delete the
server copy, or mark it as read or whatever stops
the server telling you about it..



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list