Пример #1
0
    def open_epub(cls, url, content=None):
        """Cracks open an EPUB to expose its contents

        :param url: A url representing the EPUB, only used for errors and in
            the absence of the `content` parameter
        :param content: A string representing the compressed EPUB

        :return: A tuple containing a ZipFile of the EPUB and the path to its
        package
        """
        if not (url or content):
            raise ValueError("Cannot open epub without url or content")
        if url and not content:
            # Get the epub from the url if no content has been made available.
            content = HTTP.get_with_timeout(url).content
        content = StringIO(content)

        with ZipFile(content) as zip_file:
            if not cls.CONTAINER_FILE in zip_file.namelist():
                raise ValueError("Invalid EPUB file, not modifying: %s" % url)

            with zip_file.open(cls.CONTAINER_FILE) as container_file:
                container = container_file.read()
                rootfiles_element = etree.fromstring(container).find("{urn:oasis:names:tc:opendocument:xmlns:container}rootfiles")

                if rootfiles_element is None:
                    raise ValueError("Invalid EPUB file, not modifying: %s" % url)

                rootfile_element = rootfiles_element.find("{urn:oasis:names:tc:opendocument:xmlns:container}rootfile")
                if rootfile_element is None:
                    raise ValueError("Invalid EPUB file, not modifying: %s" % url)

                package_document_path = rootfile_element.get('full-path')
            yield zip_file, package_document_path
Пример #2
0
    def open_epub(cls, url, content=None):
        """Cracks open an EPUB to expose its contents

        :param url: A url representing the EPUB, only used for errors and in
            the absence of the `content` parameter
        :param content: A string representing the compressed EPUB

        :return: A tuple containing a ZipFile of the EPUB and the path to its
            package
        """
        if not (url or content):
            raise ValueError("Cannot open epub without url or content")
        if url and not content:
            # Get the epub from the url if no content has been made available.
            content = HTTP.get_with_timeout(url).content
        content = BytesIO(content)

        with ZipFile(content) as zip_file:
            if not cls.CONTAINER_FILE in zip_file.namelist():
                raise ValueError("Invalid EPUB file, not modifying: %s" % url)

            with zip_file.open(cls.CONTAINER_FILE) as container_file:
                container = container_file.read()
                rootfiles_element = etree.fromstring(container).find(
                    "{urn:oasis:names:tc:opendocument:xmlns:container}rootfiles"
                )

                if rootfiles_element is None:
                    raise ValueError("Invalid EPUB file, not modifying: %s" %
                                     url)

                rootfile_element = rootfiles_element.find(
                    "{urn:oasis:names:tc:opendocument:xmlns:container}rootfile"
                )
                if rootfile_element is None:
                    raise ValueError("Invalid EPUB file, not modifying: %s" %
                                     url)

                package_document_path = rootfile_element.get('full-path')
            yield zip_file, package_document_path