Example #1
0
    def _download( self, url, fullpath, chunk_size, on_progress, on_finish, silent ):
        """ Implementation of actually download process"""
        response = urlopen(url)
        meta_data = dict(response.info().items())
        file_size = int(meta_data.get("Content-Length") or
                        meta_data.get("content-length"))
        self._bytes_received = 0
        start = clock()

        with open(fullpath, 'wb') as dst_file:
            # Print downloading message
            if not silent:
                print("\nDownloading: '{0}.{1}' (Bytes: {2}) \nto path: {3}\n\n".format(
                      self.filename, self.extension, sizeof(file_size), path))

            while True:
                self._buffer = response.read(chunk_size)
                if on_progress:
                    on_progress(self._bytes_received, file_size, start)
                if not self._buffer:
                    if on_finish:
                        on_finish(fullpath)
                    break

                self._bytes_received += len(self._buffer)
                dst_file.write(self._buffer)
Example #2
0
 def test_sizeof(self):
     """Accurately converts the bytes to its humanized equivalent"""
     eq_(utils.sizeof(1), '1 byte')
     eq_(utils.sizeof(2), '2 bytes')
     eq_(utils.sizeof(2400), '2 KB')
     eq_(utils.sizeof(2400000), '2 MB')
     eq_(utils.sizeof(2400000000), '2 GB')
     eq_(utils.sizeof(2400000000000), '2 TB')
     eq_(utils.sizeof(2400000000000000), '2 PB')
Example #3
0
 def test_sizeof(self):
     """Accurately converts the bytes to its humanized equivalent"""
     eq_(utils.sizeof(1), '1 byte')
     eq_(utils.sizeof(2), '2 bytes')
     eq_(utils.sizeof(2400), '2 KB')
     eq_(utils.sizeof(2400000), '2 MB')
     eq_(utils.sizeof(2400000000), '2 GB')
     eq_(utils.sizeof(2400000000000), '2 TB')
     eq_(utils.sizeof(2400000000000000), '2 PB')
Example #4
0
def test_sizeof():
    """Accurately converts the bytes to its humanized equivalent"""
    assert utils.sizeof(1) == '1 byte'
    assert utils.sizeof(2) == '2 bytes'
    assert utils.sizeof(2400) == '2 KB'
    assert utils.sizeof(2400000) == '2 MB'
    assert utils.sizeof(2400000000) == '2 GB'
    assert utils.sizeof(2400000000000) == '2 TB'
    assert utils.sizeof(2400000000000000) == '2 PB'
Example #5
0
    def download(self,
                 path=None,
                 chunk_size=8 * 1024,
                 on_progress=None,
                 on_finish=None,
                 force_overwrite=False):
        """
        Downloads the file of the URL defined within the class
        instance.

        Keyword arguments:
        path -- Destination directory
        chunk_size -- File size (in bytes) to write to buffer at a time
                      (default: 8 bytes).
        on_progress -- A function to be called every time the buffer was
                       written out. Arguments passed are the current and
                       the full size.
        on_finish -- To be called when the download is finished. The full
                     path to the file is passed as an argument.

        """

        if isdir(normpath(path)):
            path = (normpath(path) + '/' if path else '')
            fullpath = '{0}{1}.{2}'.format(path, self.filename, self.extension)
        else:
            fullpath = normpath(path)

        # Check for conflicting filenames
        if isfile(fullpath) and not force_overwrite:
            raise FileExistsError(
                "\n\nError: Conflicting filename:'{}'.\n\n".format(
                    self.filename))

        response = urlopen(self.url)
        meta_data = dict(response.info().items())
        file_size = int(
            meta_data.get("Content-Length") or meta_data.get("content-length"))
        self._bytes_received = 0
        start = clock()
        try:
            with open(fullpath, 'wb') as dst_file:
                # Print downloading message
                print(
                    "\nDownloading: '{0}.{1}' (Bytes: {2}) \nto path: {3}\n\n".
                    format(self.filename, self.extension, sizeof(file_size),
                           path))

                while True:
                    self._buffer = response.read(chunk_size)
                    if not self._buffer:
                        if on_finish:
                            on_finish(fullpath)
                        break

                    self._bytes_received += len(self._buffer)
                    dst_file.write(self._buffer)
                    if on_progress:
                        on_progress(self._bytes_received, file_size, start)

        # Catch possible exceptions occurring during download
        except IOError:
            raise IOError(
                "\n\nError: Failed to open file.\n"
                "Check that: ('{0}'), is a valid pathname.\n\n"
                "Or that ('{1}.{2}') is a valid filename.\n\n".format(
                    path, self.filename, self.extension))

        except BufferError:
            raise BufferError("\n\nError: Failed on writing buffer.\n"
                              "Failed to write video to file.\n\n")

        except KeyboardInterrupt:
            remove(fullpath)
            raise KeyboardInterrupt(
                "\n\nInterrupt signal given.\nDeleting incomplete video"
                "('{0}.{1}').\n\n".format(self.filename, self.extension))
Example #6
0
    def download(self, path=None, chunk_size=8 * 1024,
                 on_progress=None, on_finish=None):
        """
        Downloads the file of the URL defined within the class
        instance.

        Keyword arguments:
        path -- Destination directory
        chunk_size -- File size (in bytes) to write to buffer at a time
                      (default: 8 bytes).
        on_progress -- A function to be called every time the buffer was
                       written out. Arguments passed are the current and
                       the full size.
        on_finish -- To be called when the download is finished. The full
                     path to the file is passed as an argument.

        """

        path = (normpath(path) + '/' if path else '')
        fullpath = '{0}{1}.{2}'.format(path, self.filename, self.extension)

        # Check for conflicting filenames
        if isfile(fullpath):
            print("\n\nError: Conflicting filename:'{}'.\n\n".format(
                  self.filename))
            return 1

        response = urlopen(self.url)
        meta_data = dict(response.info().items())
        file_size = int(meta_data.get("Content-Length") or
                        meta_data.get("content-length"))
        self._bytes_received = 0
        start = time()
        try:
            with open(fullpath, 'wb') as dst_file:
                # Print downloading message
                print("\nDownloading: '{0}.{1}' (Bytes: {2}) \nto path: {3}\n\n".format(
                      self.filename, self.extension, sizeof(file_size), path))

                while True:
                    self._buffer = response.read(chunk_size)
                    if not self._buffer:
                        if on_finish:
                            on_finish(fullpath)
                        break

                    self._bytes_received += len(self._buffer)
                    dst_file.write(self._buffer)
                    if on_progress:
                        on_progress(self._bytes_received, file_size, start)

        # Catch possible exceptions occurring during download
        except IOError:
            print("\n\nError: Failed to open file.\n"
                  "Check that: ('{0}'), is a valid pathname.\n\n"
                  "Or that ('{1}.{2}') is a valid filename.\n\n".format(
                        path, self.filename, self.extension))
            return 2

        except BufferError:
            print("\n\nError: Failed on writing buffer.\n"
                  "Failed to write video to file.\n\n")
            return 1

        except KeyboardInterrupt:
            print("\n\nInterrupt signal given.\nDeleting incomplete video"
                  "('{0}.{1}').\n\n".format(self.filename, self.extension))
            remove(fullpath)
            return 1

        return 0
Example #7
0
    def download(self, path='', chunk_size=8 * 1024, on_progress=None,
                 on_finish=None, force_overwrite=False):
        """
        Downloads the file of the URL defined within the class
        instance.

        :param path:
            Destination directory
        :param chunk_size:
            File size (in bytes) to write to buffer at a time (default: 8
            bytes).
        :param on_progress:
            A function to be called every time the buffer was written
            out. Arguments passed are the current and the full size.
        :param on_finish:
            To be called when the download is finished. The full path to the
            file is passed as an argument.
        """

        if isdir(normpath(path)):
            path = (normpath(path) + '/' if path else '')
            fullpath = '{0}{1}.{2}'.format(path, self.filename, self.extension)
        else:
            fullpath = normpath(path)

        # Check for conflicting filenames
        if isfile(fullpath) and not force_overwrite:
            raise OSError("\Error: Conflicting filename:'{}'".format(
                self.filename))

        response = urlopen(self.url)
        meta_data = dict(response.info().items())
        file_size = int(meta_data.get("Content-Length") or
                        meta_data.get("content-length"))
        self._bytes_received = 0
        start = clock()
        try:
            with open(fullpath, 'wb') as dst_file:
                # Print downloading message.
                logging.info("Downloading: '%s.%s' (Bytes: %s) to path: %s",
                             self.filename, self.extension, sizeof(file_size),
                             path)
                while True:
                    self._buffer = response.read(chunk_size)
                    if not self._buffer:
                        if on_finish:
                            on_finish(fullpath)
                        break

                    self._bytes_received += len(self._buffer)
                    dst_file.write(self._buffer)
                    if on_progress:
                        on_progress(self._bytes_received, file_size, start)

        # Catch possible exceptions occurring during download
        except IOError:
            raise IOError("Error: Failed to open file. Check that: ('{0}'), "
                  "is a valid pathname. " "Or that ('{1}.{2}') is a valid "
                  "filename.".format(path, self.filename, self.extension))

        except BufferError:
            raise BufferError("Error: Failed on writing buffer. Failed to "
                  "write video to file.")

        except KeyboardInterrupt:
            remove(fullpath)
            raise KeyboardInterrupt("Interrupt signal given. Deleting "
                "incomplete video('{0}.{1}').".format(self.filename,
                self.extension))