cenkalti / putio.py

A python wrapper for put.io APIv2
http://put.io
MIT License
72 stars 41 forks source link

Download no longer works. #30

Closed rawdigits closed 7 years ago

rawdigits commented 7 years ago

I have been getting an error on every attempt to download a file. I have tried multiple OAUTH tokens. Listing files works fine, but downloading produces a file with the following text:

{
  "error_message": "You have to login with proper credentials",
  "error_type": "invalid_grant",
  "error_uri": "http://api.put.io/v2/docs",
  "status": "ERROR",
  "status_code": 401
}
rawdigits commented 7 years ago

In quite possibly the laziest and hackiest way possible, I worked around this by just calling wget to do the download. It somehow successfully downloads after the initial 401 reply. I'm pretty sure this is a problem/change on the put.io side, so this is fine until I hear back from them on the bug.

    def _download_file(self, dest, delete_after_download, chunk_size):
        name = _str(self.name)
        USERNAME = "[redacted]"
        PASSWORD = "[redacted]"
        OAUTH_TOKEN = "[redacted]"
        os.system("cd {}; wget --quiet https://{}:{}@s06.put.io/download/{}?oauth_token={}".format(dest, USERNAME, PASSWORD, self.id, OAUTH_TOKEN))
        os.system("cd {}; mv {}?oauth_token={} {}".format(dest, self.id, OAUTH_TOKEN, self.name))
        filepath = os.path.join(dest, name)
#        if os.path.exists(filepath):
#            first_byte = os.path.getsize(filepath)
#
#            if first_byte == self.size:
#                logger.warning('file %s exists and is the correct size %d' % (filepath, self.size))
#        else:
#            first_byte = 0
#
#        logger.debug('file %s is currently %d, should be %d' % (filepath, first_byte, self.size))
#
#        if self.size == 0:
#            # Create an empty file
#            open(filepath, 'w').close()
#            logger.debug('created empty file %s' % filepath)
#        else:
#            if first_byte < self.size:
#                with open(filepath, 'ab') as f:
#                    headers = {'Range': 'bytes=%d-' % first_byte}
#
#                    logger.debug('request range: bytes=%d-' % first_byte)
#                    response = self.client.request('/files/%s/download' % self.id,
#                                                   headers=headers,
#                                                   raw=True,
#                                                   stream=True)
#
#                    for chunk in response.iter_content(chunk_size=chunk_size):
#                        if chunk:  # filter out keep-alive new chunks
#                            f.write(chunk)

        if self._verify_file(filepath):
            if delete_after_download:
                self.delete()
cenkalti commented 7 years ago

You are right. There is a problem with download function. It should raise an exception instead of saving a file with wrong response body. I will send a fix in few minutes.

The real problem here is about authentication though. @rawdigits where did you get the oauth_token? At the end of a download link in website or did you create an OAuth application from settings page?

cenkalti commented 7 years ago

You can download the new version of the package with: pip install -U putio.py