def get_metadata(stream): from calibre.ebooks.metadata.archive import is_comic from calibre.ebooks.metadata.meta import get_metadata path = getattr(stream, 'name', False) if not path: pt = PersistentTemporaryFile('_rar-meta.rar') pt.write(stream.read()) pt.close() path = pt.name path = os.path.abspath(path) file_names = list(names(path)) if is_comic(file_names): return get_metadata(stream, 'cbr') for f in file_names: stream_type = os.path.splitext(f)[1].lower() if stream_type: stream_type = stream_type[1:] if stream_type in ('lit', 'opf', 'prc', 'mobi', 'fb2', 'epub', 'rb', 'imp', 'pdf', 'lrf', 'azw'): with TemporaryDirectory() as tdir: with CurrentDir(tdir): stream = extract_member(path, match=None, name=f, as_file=True)[1] return get_metadata(stream, stream_type) raise ValueError('No ebook found in RAR archive')
def get_cbr_page_count(book_path): from calibre.libunrar import names pages = set() # The calibre rar utility seems to double up the names - don't know why! for name in names(book_path): if '__MACOSX' in name: continue ext = os.path.splitext(name)[1].lower() if ext in COMIC_PAGE_EXTENSIONS: pages.add(name) return len(pages)
def get_metadata(stream): from calibre.ebooks.metadata.archive import is_comic from calibre.ebooks.metadata.meta import get_metadata path = getattr(stream, "name", False) if not path: pt = PersistentTemporaryFile("_rar-meta.rar") pt.write(stream.read()) pt.close() path = pt.name path = os.path.abspath(path) file_names = list(names(path)) if is_comic(file_names): return get_metadata(stream, "cbr") for f in file_names: stream_type = os.path.splitext(f)[1].lower() if stream_type: stream_type = stream_type[1:] if stream_type in ( "lit", "opf", "prc", "mobi", "fb2", "epub", "rb", "imp", "pdf", "lrf", "azw", "azw1", "azw3", ): with TemporaryDirectory() as tdir: with CurrentDir(tdir): stream = extract_member(path, match=None, name=f, as_file=True)[1] return get_metadata(stream, stream_type) raise ValueError("No ebook found in RAR archive")
def run(self, archive): is_rar = archive.lower().endswith('.rar') if is_rar: from calibre.libunrar import extract_member, names else: zf = ZipFile(archive, 'r') if is_rar: fnames = names(archive) else: fnames = zf.namelist() fnames = [x for x in fnames if '.' in x] if is_comic(fnames): ext = '.cbr' if is_rar else '.cbz' of = self.temporary_file('_archive_extract'+ext) with open(archive, 'rb') as f: of.write(f.read()) of.close() return of.name if len(fnames) > 1 or not fnames: return archive fname = fnames[0] ext = os.path.splitext(fname)[1][1:] if ext.lower() not in ('lit', 'epub', 'mobi', 'prc', 'rtf', 'pdf', 'mp3', 'pdb', 'azw', 'azw1', 'azw3', 'fb2'): return archive of = self.temporary_file('_archive_extract.'+ext) with closing(of): if is_rar: data = extract_member(archive, match=None, name=fname)[1] of.write(data) else: of.write(zf.read(fname)) return of.name
def run(self, archive): is_rar = archive.lower().endswith('.rar') if is_rar: from calibre.libunrar import extract_member, names else: zf = ZipFile(archive, 'r') if is_rar: fnames = names(archive) else: fnames = zf.namelist() fnames = [x for x in fnames if '.' in x] if is_comic(fnames): ext = '.cbr' if is_rar else '.cbz' of = self.temporary_file('_archive_extract' + ext) with open(archive, 'rb') as f: of.write(f.read()) of.close() return of.name if len(fnames) > 1 or not fnames: return archive fname = fnames[0] ext = os.path.splitext(fname)[1][1:] if ext.lower() not in ('lit', 'epub', 'mobi', 'prc', 'rtf', 'pdf', 'mp3', 'pdb', 'azw', 'azw1', 'fb2'): return archive of = self.temporary_file('_archive_extract.' + ext) with closing(of): if is_rar: data = extract_member(archive, match=None, name=fname)[1] of.write(data) else: of.write(zf.read(fname)) return of.name