def set_metadata(self, stream, mi, type):
        """Set standard ePub metadata then properly set the cover image."""
        common.log.debug(
            "KEPUBMetadataWriter::set_metadata - self.__class__={0}".format(
                self.__class__
            )
        )
        super(KEPUBMetadataWriter, self).set_metadata(stream, mi, type)

        stream.seek(0)
        reader = get_zip_reader(stream, root=os.getcwd())

        found_cover = False
        covers = reader.opf.raster_cover_path(reader.opf.metadata)
        if len(covers) > 0:
            common.log.debug(
                "KEPUBMetadataWriter::set_metadata - covers={0}".format(covers)
            )
            cover_id = covers[0].get("content")
            common.log.debug(
                "KEPUBMetadataWriter::set_metadata - cover_id={0}".format(cover_id)
            )
            for item in reader.opf.itermanifest():
                if item.get("id", None) == cover_id:
                    mt = item.get("media-type", "")
                    if mt and mt.startswith("image/"):
                        common.log.debug(
                            "KEPUBMetadataWriter::set_metadata - found cover"
                        )
                        item.set("properties", "cover-image")
                        found_cover = True
                        break
            if not found_cover:
                common.log.debug(
                    "KEPUBMetadataWriter::set_metadata - looking for cover "
                    "using href"
                )
                for item in reader.opf.itermanifest():
                    if item.get("href", None) == cover_id:
                        mt = item.get("media-type", "")
                        if mt and mt.startswith("image/"):
                            common.log(
                                "KEPUBMetadataWriter::set_metadata -found " "cover"
                            )
                            item.set("properties", "cover-image")
                            found_cover = True

            if found_cover:
                opfbytes = reader.read_bytes(reader.opf_path)
                if isinstance(reader.archive, LocalZipFile):
                    reader.archive.safe_replace(
                        reader.container[OPF.MIMETYPE], opfbytes
                    )
                else:
                    safe_replace(stream, reader.container[OPF.MIMETYPE], opfbytes)
Beispiel #2
0
    def set_metadata(self, stream, mi, type):
        """Set standard ePub metadata then properly set the cover image."""
        default_log(
            "KEPUBMetadataWriter::set_metadata - self.__class__={0}".format(
                self.__class__
            )
        )
        super(KEPUBMetadataWriter, self).set_metadata(stream, mi, type)

        stream.seek(0)
        reader = get_zip_reader(stream, root=os.getcwdu())

        found_cover = False
        covers = reader.opf.raster_cover_path(reader.opf.metadata)
        if len(covers) > 0:
            default_log("KEPUBMetadataWriter::set_metadata - covers={0}".format(covers))
            cover_id = covers[0].get("content")
            default_log(
                "KEPUBMetadataWriter::set_metadata - cover_id={0}".format(cover_id)
            )
            for item in reader.opf.itermanifest():
                if item.get("id", None) == cover_id:
                    mt = item.get("media-type", "")
                    if mt and mt.startswith("image/"):
                        default_log("KEPUBMetadataWriter::set_metadata - found cover")
                        item.set("properties", "cover-image")
                        found_cover = True
                        break
            if not found_cover:
                default_log(
                    "KEPUBMetadataWriter::set_metadata - looking for cover "
                    "using href"
                )
                for item in reader.opf.itermanifest():
                    if item.get("href", None) == cover_id:
                        mt = item.get("media-type", "")
                        if mt and mt.startswith("image/"):
                            default_log(
                                "KEPUBMetadataWriter::set_metadata -found " "cover"
                            )
                            item.set("properties", "cover-image")
                            found_cover = True

            if found_cover:
                newopf = StringIO(reader.opf.render())
                if isinstance(reader.archive, LocalZipFile):
                    reader.archive.safe_replace(reader.container[OPF.MIMETYPE], newopf)
                else:
                    safe_replace(stream, reader.container[OPF.MIMETYPE], newopf)
Beispiel #3
0
    def set_metadata(self, stream, mi, type):
        debug_print("KEPUBMetadataWriter::set_metadata - self.__class__=%s" %
                    (self.__class__))
        super(KEPUBMetadataWriter, self).set_metadata(stream, mi, type)

        stream.seek(0)
        reader = get_zip_reader(stream, root=os.getcwdu())
        raster_cover = reader.opf.raster_cover

        found_cover = False
        covers = reader.opf.raster_cover_path(reader.opf.metadata)
        if len(covers) > 0:
            debug_print("KEPUBMetadataWriter::set_metadata - covers=", covers)
            cover_id = covers[0].get('content')
            debug_print("KEPUBMetadataWriter::set_metadata - cover_id=",
                        cover_id)
            for item in reader.opf.itermanifest():
                if item.get('id', None) == cover_id:
                    mt = item.get('media-type', '')
                    if mt and mt.startswith('image/'):
                        debug_print(
                            "KEPUBMetadataWriter::set_metadata - found cover")
                        item.set("properties", "cover-image")
                        found_cover = True
                        break
            if not found_cover:
                debug_print(
                    "KEPUBMetadataWriter::set_metadata - looking for cover using href"
                )
                for item in reader.opf.itermanifest():
                    if item.get('href', None) == cover_id:
                        mt = item.get('media-type', '')
                        if mt and mt.startswith('image/'):
                            debug_print(
                                "KEPUBMetadataWriter::set_metadata -found cover"
                            )
                            item.set("properties", "cover-image")
                            found_cover = True

            if found_cover:
                newopf = StringIO(reader.opf.render())
                if isinstance(reader.archive, LocalZipFile):
                    reader.archive.safe_replace(reader.container[OPF.MIMETYPE],
                                                newopf)
                else:
                    safe_replace(stream, reader.container[OPF.MIMETYPE],
                                 newopf)
Beispiel #4
0
    def set_metadata(self, stream, mi, type):
        debug_print("KEPUBMetadataWriter::set_metadata - self.__class__=%s" %
                    (self.__class__))
        super(KEPUBMetadataWriter, self).set_metadata(stream, mi, type)

        stream.seek(0)
        reader = get_zip_reader(stream, root=os.getcwdu())
        raster_cover = reader.opf.raster_cover

        found_cover = False
        covers = reader.opf.raster_cover_path(reader.opf.metadata)
        if len(covers) > 0:
            debug_print("KEPUBMetadataWriter::set_metadata - covers=", covers)
            cover_id = covers[0].get('content')
            debug_print("KEPUBMetadataWriter::set_metadata - cover_id=",
                        cover_id)
            for item in reader.opf.itermanifest():
                if item.get('id', None) == cover_id:
                    mt = item.get('media-type', '')
                    if mt and mt.startswith('image/'):
                        debug_print(
                            "KEPUBMetadataWriter::set_metadata - found cover")
                        item.set("properties", "cover-image")
                        found_cover = True
                        break
            if not found_cover:
                debug_print(
                    "KEPUBMetadataWriter::set_metadata - looking for cover using href")
                for item in reader.opf.itermanifest():
                    if item.get('href', None) == cover_id:
                        mt = item.get('media-type', '')
                        if mt and mt.startswith('image/'):
                            debug_print(
                                "KEPUBMetadataWriter::set_metadata -found cover")
                            item.set("properties", "cover-image")
                            found_cover = True

            if found_cover:
                newopf = StringIO(reader.opf.render())
                if isinstance(reader.archive, LocalZipFile):
                    reader.archive.safe_replace(reader.container[OPF.MIMETYPE],
                                                newopf)
                else:
                    safe_replace(stream, reader.container[OPF.MIMETYPE],
                                 newopf)