def extract_resources(self, sections): from calibre.ebooks.mobi.writer2.resources import PLACEHOLDER_GIF resource_map = [] container = None for x in ('fonts', 'images'): os.mkdir(x) for start, end in self.resource_offsets: for i, sec in enumerate(sections[start:end]): fname_idx = i + 1 data = sec[0] typ = data[:4] href = None if typ in { b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n', b'BOUN', b'FDST', b'DATP', b'AUDI', b'VIDE', b'RESC', b'CMET', b'PAGE' }: pass # Ignore these records elif typ == b'FONT': font = read_font_record(data) href = "fonts/%05d.%s" % (fname_idx, font['ext']) if font['err']: self.log.warn('Reading font record %d failed: %s' % (fname_idx, font['err'])) if font['headers']: self.log.debug('Font record headers: %s' % font['headers']) with open(href.replace('/', os.sep), 'wb') as f: f.write(font['font_data'] if font['font_data'] else font['raw_data']) if font['encrypted']: self.encrypted_fonts.append(href) elif typ == b'CONT': if data == b'CONTBOUNDARY': container = None continue container = Container(data) elif typ == b'CRES': data, imgtype = container.load_image(data) if data is not None: href = 'images/%05d.%s' % (container.resource_index, imgtype) with open(href.replace('/', os.sep), 'wb') as f: f.write(data) elif typ == b'\xa0\xa0\xa0\xa0' and len( data) == 4 and container is not None: container.resource_index += 1 elif container is None: if not (len(data) == len(PLACEHOLDER_GIF) and data == PLACEHOLDER_GIF): imgtype = find_imgtype(data) href = 'images/%05d.%s' % (fname_idx, imgtype) with open(href.replace('/', os.sep), 'wb') as f: f.write(data) resource_map.append(href) return resource_map
def extract_resources(self, sections): from calibre.ebooks.mobi.writer2.resources import PLACEHOLDER_GIF resource_map = [] container = None for x in ('fonts', 'images'): os.mkdir(x) for start, end in self.resource_offsets: for i, sec in enumerate(sections[start:end]): fname_idx = i+1 data = sec[0] typ = data[:4] href = None if typ in {b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n', b'BOUN', b'FDST', b'DATP', b'AUDI', b'VIDE', b'RESC', b'CMET', b'PAGE'}: pass # Ignore these records elif typ == b'FONT': font = read_font_record(data) href = "fonts/%05d.%s" % (fname_idx, font['ext']) if font['err']: self.log.warn('Reading font record %d failed: %s'%( fname_idx, font['err'])) if font['headers']: self.log.debug('Font record headers: %s'%font['headers']) with open(href.replace('/', os.sep), 'wb') as f: f.write(font['font_data'] if font['font_data'] else font['raw_data']) if font['encrypted']: self.encrypted_fonts.append(href) elif typ == b'CONT': if data == b'CONTBOUNDARY': container = None continue container = Container(data) elif typ == b'CRES': data, imgtype = container.load_image(data) if data is not None: href = 'images/%05d.%s'%(container.resource_index, imgtype) with open(href.replace('/', os.sep), 'wb') as f: f.write(data) elif typ == b'\xa0\xa0\xa0\xa0' and len(data) == 4 and container is not None: container.resource_index += 1 elif container is None: if not (len(data) == len(PLACEHOLDER_GIF) and data == PLACEHOLDER_GIF): imgtype = find_imgtype(data) href = 'images/%05d.%s'%(fname_idx, imgtype) with open(href.replace('/', os.sep), 'wb') as f: f.write(data) resource_map.append(href) return resource_map
def extract_resources(self, sections): from calibre.ebooks.mobi.writer2.resources import PLACEHOLDER_GIF resource_map = [] container = None for x in ("fonts", "images"): os.mkdir(x) for start, end in self.resource_offsets: for i, sec in enumerate(sections[start:end]): fname_idx = i + 1 data = sec[0] typ = data[:4] href = None if typ in { b"FLIS", b"FCIS", b"SRCS", b"\xe9\x8e\r\n", b"BOUN", b"FDST", b"DATP", b"AUDI", b"VIDE", b"RESC", b"CMET", b"PAGE", }: pass # Ignore these records elif typ == b"FONT": font = read_font_record(data) href = "fonts/%05d.%s" % (fname_idx, font["ext"]) if font["err"]: self.log.warn("Reading font record %d failed: %s" % (fname_idx, font["err"])) if font["headers"]: self.log.debug("Font record headers: %s" % font["headers"]) with open(href.replace("/", os.sep), "wb") as f: f.write(font["font_data"] if font["font_data"] else font["raw_data"]) if font["encrypted"]: self.encrypted_fonts.append(href) elif typ == b"CONT": if data == b"CONTBOUNDARY": container = None continue container = Container(data) elif typ == b"CRES": data, imgtype = container.load_image(data) if data is not None: href = "images/%05d.%s" % (container.resource_index, imgtype) with open(href.replace("/", os.sep), "wb") as f: f.write(data) elif typ == b"\xa0\xa0\xa0\xa0" and len(data) == 4 and container is not None: container.resource_index += 1 elif container is None: if not (len(data) == len(PLACEHOLDER_GIF) and data == PLACEHOLDER_GIF): imgtype = find_imgtype(data) href = "images/%05d.%s" % (fname_idx, imgtype) with open(href.replace("/", os.sep), "wb") as f: f.write(data) resource_map.append(href) return resource_map