diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 412998f292..6bb418ed4f 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -3,7 +3,7 @@ __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' -import os, glob, functools, re +import os, glob, re from calibre import guess_type from calibre.customize import (FileTypePlugin, MetadataReaderPlugin, MetadataWriterPlugin, PreferencesPlugin, InterfaceActionBase, StoreBase) @@ -143,14 +143,11 @@ class ComicMetadataReader(MetadataReaderPlugin): elif id_.startswith(b'PK'): ftype = 'cbz' if ftype == 'cbr': - from calibre.utils.unrar import extract_first_alphabetically as extract_first - extract_first + from calibre.utils.unrar import extract_cover_image else: - from calibre.libunzip import extract_member - extract_first = functools.partial(extract_member, - sort_alphabetically=True) + from calibre.libunzip import extract_cover_image from calibre.ebooks.metadata import MetaInformation - ret = extract_first(stream) + ret = extract_cover_image(stream) mi = MetaInformation(None, None) stream.seek(0) if ftype in {'cbr', 'cbz'}: diff --git a/src/calibre/libunzip.py b/src/calibre/libunzip.py index 8599b372cb..b320b49656 100644 --- a/src/calibre/libunzip.py +++ b/src/calibre/libunzip.py @@ -58,3 +58,14 @@ def extract_member(filename, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I) for name in names: if match.search(name): return name, zf.read(name) + +comic_exts = {'png', 'jpg', 'jpeg', 'gif', 'webp'} + +def name_ok(name): + return bool(name and not name.startswith('__MACOSX/') and name.rpartition('.')[-1].lower() in comic_exts) + +def extract_cover_image(filename): + with zipfile.ZipFile(filename) as zf: + for name in sorted(zf.namelist(), key=sort_key): + if name_ok(name): + return name, zf.read(name) diff --git a/src/calibre/utils/unrar.py b/src/calibre/utils/unrar.py index c3940ad68f..307ba327fe 100644 --- a/src/calibre/utils/unrar.py +++ b/src/calibre/utils/unrar.py @@ -190,6 +190,12 @@ def extract_first_alphabetically(stream): {'png', 'jpg', 'jpeg', 'gif', 'webp'}], key=sort_key) return extract_member(stream, name=names_[0], match=None) +def extract_cover_image(stream): + from calibre.libunzip import sort_key, name_ok + for name in sorted(names(stream), key=sort_key): + if name_ok(name): + return extract_member(stream, name=name, match=None) + # Test normal RAR file {{{ def test_basic():