def get_comic_metadata_from_cbr(self): ''' Reads the comic metadata from the comic cbr file as comictagger metadata and returns the metadata depending on do_action ''' from calibre.utils.unrar import RARFile, extract_member, names ffile = self.db.format(self.book_id, "cbr", as_path=True) with open(ffile, 'rb') as stream: # get the cix metadata fnames = list(names(stream)) for name in fnames: if name.lower() == "comicinfo.xml": self.cix_metadata = extract_member(stream, match=None, name=name)[1] self.cix_metadata = ComicInfoXml().metadataFromString(self.cix_metadata) break # get the cbi metadata zr = RARFile(stream, get_comment=True) comment = zr.comment if ComicBookInfo().validateString(comment): self.cbi_metadata = ComicBookInfo().metadataFromString(comment) delete_temp_file(ffile) self._get_combined_metadata()
def get_comic_metadata_from_cbr(self): ''' Reads the comic metadata from the comic cbr file as comictagger metadata and returns the metadata depending on do_action ''' from calibre.utils.unrar import extract_member, names, comment ffile = self.db.format(self.book_id, "cbr", as_path=True) with open(ffile, 'rb') as stream: # get the cix metadata fnames = list(names(stream)) for name in fnames: if name.lower() == "comicinfo.xml": self.cix_metadata = extract_member(stream, match=None, name=name)[1] self.cix_metadata = ComicInfoXml().metadataFromString( self.cix_metadata) break # get the cbi metadata comments = comment(ffile) if ComicBookInfo().validateString(comments): self.cbi_metadata = ComicBookInfo().metadataFromString( comments) delete_temp_file(ffile) self._get_combined_metadata()
def run(self, archive): from calibre.utils.zipfile import ZipFile is_rar = archive.lower().endswith('.rar') if is_rar: from calibre.utils.unrar import extract_member, names else: zf = ZipFile(archive, 'r') if is_rar: with open(archive, 'rb') as rf: fnames = list(names(rf)) else: fnames = zf.namelist() def fname_ok(fname): bn = os.path.basename(fname).lower() if bn == 'thumbs.db': return False if '.' not in bn: return False if bn.rpartition('.')[-1] in {'diz', 'nfo'}: return False if '__MACOSX' in fname.split('/'): return False return True fnames = list(filter(fname_ok, fnames)) 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', 'docx', 'doc', 'odt' }: return archive of = self.temporary_file('_archive_extract.' + ext) with closing(of): if is_rar: with open(archive, 'rb') as f: data = extract_member(f, match=None, name=fname)[1] of.write(data) else: of.write(zf.read(fname)) return of.name
def run(self, archive): from calibre.utils.zipfile import ZipFile is_rar = archive.lower().endswith('.rar') if is_rar: from calibre.utils.unrar import extract_member, names else: zf = ZipFile(archive, 'r') if is_rar: with open(archive, 'rb') as rf: fnames = list(names(rf)) else: fnames = zf.namelist() def fname_ok(fname): bn = os.path.basename(fname).lower() if bn == 'thumbs.db': return False if '.' not in bn: return False if bn.rpartition('.')[-1] in {'diz', 'nfo'}: return False if '__MACOSX' in fname.split('/'): return False return True fnames = list(filter(fname_ok, fnames)) 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', 'docx', 'doc', 'odt'}: return archive of = self.temporary_file('_archive_extract.'+ext) with closing(of): if is_rar: with open(archive, 'rb') as f: data = extract_member(f, match=None, name=fname)[1] of.write(data) else: of.write(zf.read(fname)) return of.name
def get_metadata(stream): from calibre.ebooks.metadata.archive import is_comic from calibre.ebooks.metadata.meta import get_metadata file_names = list(names(stream)) 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'}: name, data = extract_member(stream, match=None, name=f) stream = BytesIO(data) stream.name = os.path.basename(name) return get_metadata(stream, stream_type) raise ValueError('No ebook found in RAR archive')
def run(self, archive): from calibre.utils.zipfile import ZipFile is_rar = archive.lower().endswith('.rar') if is_rar: from calibre.utils.unrar import extract_member, names else: zf = ZipFile(archive, 'r') if is_rar: with open(archive, 'rb') as rf: fnames = list(names(rf)) else: fnames = zf.namelist() fnames = [ x for x in fnames if '.' in x and x.lower().rpartition('/')[-1] != 'thumbs.db' ] 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: with open(archive, 'rb') as f: data = extract_member(f, match=None, name=fname)[1] of.write(data) else: of.write(zf.read(fname)) return of.name
def run(self, archive): from calibre.utils.zipfile import ZipFile is_rar = archive.lower().endswith('.rar') if is_rar: from calibre.utils.unrar import extract_member, names else: zf = ZipFile(archive, 'r') if is_rar: with open(archive, 'rb') as rf: fnames = list(names(rf)) else: fnames = zf.namelist() fnames = [x for x in fnames if '.' in x and x.lower().rpartition('/')[-1] != 'thumbs.db'] 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: with open(archive, 'rb') as f: data = extract_member(f, match=None, name=fname)[1] of.write(data) else: of.write(zf.read(fname)) return of.name