def metadata_from_path(cls, path): if path.endswith('.kfx'): from calibre.ebooks.metadata.kfx import read_metadata_kfx try: kfx_path = path with lopen(kfx_path, 'rb') as f: if f.read(8) != b'\xeaDRMION\xee': f.seek(0) mi = read_metadata_kfx(f) else: kfx_path = os.path.join( path.rpartition('.')[0] + '.sdr', 'assets', 'metadata.kfx') with lopen(kfx_path, 'rb') as mf: mi = read_metadata_kfx(mf) except Exception: import traceback traceback.print_exc() if DEBUG: prints('failed kfx path:', kfx_path) mi = cls.metadata_from_formats([path]) else: mi = cls.metadata_from_formats([path]) if mi.title == _('Unknown') or ('-asin' in mi.title and '-type' in mi.title): match = cls.WIRELESS_FILE_NAME_PATTERN.match( os.path.basename(path)) if match is not None: mi.title = match.group('title') if not isinstance(mi.title, unicode): mi.title = mi.title.decode(sys.getfilesystemencoding(), 'replace') return mi
def metadata_from_path(cls, path): if path.endswith('.kfx'): from calibre.ebooks.metadata.kfx import read_metadata_kfx try: kfx_path = path with lopen(kfx_path, 'rb') as f: if f.read(8) != b'\xeaDRMION\xee': f.seek(0) mi = read_metadata_kfx(f) else: kfx_path = os.path.join(path.rpartition('.')[0] + '.sdr', 'assets', 'metadata.kfx') with lopen(kfx_path, 'rb') as mf: mi = read_metadata_kfx(mf) except Exception: import traceback traceback.print_exc() if DEBUG: prints('failed kfx path:', kfx_path) mi = cls.metadata_from_formats([path]) else: mi = cls.metadata_from_formats([path]) if mi.title == _('Unknown') or ('-asin' in mi.title and '-type' in mi.title): match = cls.WIRELESS_FILE_NAME_PATTERN.match(os.path.basename(path)) if match is not None: mi.title = match.group('title') if not isinstance(mi.title, unicode): mi.title = mi.title.decode(sys.getfilesystemencoding(), 'replace') return mi
def metadata_from_path(cls, path): if path.replace('\\', '/').endswith('.sdr/assets/metadata.kfx'): from calibre.ebooks.metadata.kfx import read_metadata_kfx try: with lopen(path, 'rb') as f: mi = read_metadata_kfx(f) except Exception: import traceback traceback.print_exc() path = get_kfx_path(path) mi = cls.metadata_from_formats([get_kfx_path(path)]) else: mi = cls.metadata_from_formats([path]) if mi.title == _('Unknown') or ('-asin' in mi.title and '-type' in mi.title): match = cls.WIRELESS_FILE_NAME_PATTERN.match(os.path.basename(path)) if match is not None: mi.title = match.group('title') if not isinstance(mi.title, unicode): mi.title = mi.title.decode(sys.getfilesystemencoding(), 'replace') return mi