Exemplo n.º 1
0
    def perform_download(self, token):
        self._start_datetime = datetime.datetime.now()
        self._fire_start_callbacks()
        putio_file = self.get_putio_file()
        dest = self.get_destination_directory()
        filename = self.get_filename()

        final_path = os.path.join(dest, filename.decode('utf-8'))
        download_path = "{}.part".format(final_path)

        # ensure the path into which the download is going to be donwloaded exists. We know
        # that the 'dest' directory exists but in some cases the filename on put.io may
        # have directories within it (for an archive, as an example).  In addition, some
        # post-processing may delete directories, so let's just recreate the directory
        if not os.path.exists(os.path.dirname(download_path)):
            os.makedirs(os.path.dirname(download_path))

        success = False
        with open(download_path, 'wb') as f:

            def transfer_callback(offset, chunk):
                self._downloaded += len(chunk)
                f.seek(offset)
                f.write(chunk)
                f.flush()
                self._fire_progress_callbacks()

            success = multipart_downloader.download(
                putiopy.BASE_URL + '/files/{}/download'.format(putio_file.id),
                self.get_size(),
                transfer_callback,
                params={'oauth_token': token})
            f.flush()
            os.fsync(f)

        # download to part file is complete.  Now move to its final destination
        if success:
            if os.path.exists(final_path):
                os.remove(final_path)
            shutil.move(download_path,
                        download_path[:-5])  # same but without '.part'
            self._finish_datetime = datetime.datetime.now()
            self._fire_completion_callbacks()

        return success
Exemplo n.º 2
0
    def perform_download(self, token):
        self._start_datetime = datetime.datetime.now()
        self._fire_start_callbacks()
        putio_file = self.get_putio_file()
        dest = self.get_destination_directory()
        filename = self.get_filename()

        final_path = os.path.join(dest, filename)
        download_path = "{}.part".format(final_path)

        # ensure the path into which the download is going to be donwloaded exists. We know
        # that the 'dest' directory exists but in some cases the filename on put.io may
        # have directories within it (for an archive, as an example).  In addition, some
        # post-processing may delete directories, so let's just recreate the directory
        if not os.path.exists(os.path.dirname(download_path)):
            os.makedirs(os.path.dirname(download_path))

        success = False
        with open(download_path, 'wb') as f:
            def transfer_callback(offset, chunk):
                self._downloaded += len(chunk)
                f.seek(offset)
                f.write(chunk)
                f.flush()
                self._fire_progress_callbacks()

            success = multipart_downloader.download(
                putio.BASE_URL + '/files/{}/download'.format(putio_file.id),
                self.get_size(),
                transfer_callback,
                params={'oauth_token': token})

        # download to part file is complete.  Now move to its final destination
        if success:
            if os.path.exists(final_path):
                os.remove(final_path)
            os.rename(download_path, download_path[:-5])  # same but without '.part'
            self._finish_datetime = datetime.datetime.now()
            self._fire_completion_callbacks()

        return success