[Tutor] Downloading Slack Files

khalil zakaria Zemmoura zemmoura.khalil at gmail.com
Sun Aug 28 05:10:51 EDT 2016


I noticed that you didn't store what requests downloaded in a new file you
created. The usual pattern to use is to download a file and writing it to
the HD drive. You have to create a file with "With
open(file_name.extention, "wb") as f: " and write to that file.

Here is the code that I suggest
With open(filename, 'wb') as fd:
      for chunk in r.iter_content(chunk_size):
          fd.write(chunk)

I apologize if the display of the email is messy since I am using Gmail on
my phone.

Le 17 août 2016 00:56, "Malcolm Boone" <oasis.boone at gmail.com> a écrit :

> 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.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list