diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index d64369e363..5475231cc5 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -2,9 +2,7 @@ import os.path __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' -import textwrap -import os -import glob +import textwrap, os, glob, functools from calibre.customize import FileTypePlugin, MetadataReaderPlugin, \ MetadataWriterPlugin, PreferencesPlugin, InterfaceActionBase from calibre.constants import numeric_version @@ -95,10 +93,12 @@ class ComicMetadataReader(MetadataReaderPlugin): def get_metadata(self, stream, ftype): if ftype == 'cbr': - from calibre.libunrar import extract_member as extract_first + from calibre.libunrar import extract_first_alphabetically as extract_first extract_first else: - from calibre.libunzip import extract_member as extract_first + from calibre.libunzip import extract_member + extract_first = functools.partial(extract_member, + sort_alphabetically=True) from calibre.ebooks.metadata import MetaInformation ret = extract_first(stream) mi = MetaInformation(None, None) diff --git a/src/calibre/libunrar.py b/src/calibre/libunrar.py index 5dc66eaa36..e05afc4e38 100644 --- a/src/calibre/libunrar.py +++ b/src/calibre/libunrar.py @@ -269,3 +269,16 @@ def extract_member(path, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I), path = _extract_member(path, match, name) return path, open(path, 'rb').read() +def extract_first_alphabetically(path): + if hasattr(path, 'read'): + data = path.read() + f = NamedTemporaryFile(suffix='.rar') + f.write(data) + f.flush() + path = f.name + + names_ = [x for x in names(path) if os.path.splitext(x)[1][1:].lower() in + ('png', 'jpg', 'jpeg', 'gif')] + names_.sort() + return extract_member(path, name=names_[0], match=None) + diff --git a/src/calibre/libunzip.py b/src/calibre/libunzip.py index f384af1073..09681bd325 100644 --- a/src/calibre/libunzip.py +++ b/src/calibre/libunzip.py @@ -43,9 +43,12 @@ def extract(filename, dir): zf = zipfile.ZipFile( filename ) zf.extractall(dir) -def extract_member(filename, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I)): +def extract_member(filename, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', + re.I), sort_alphabetically=False): zf = zipfile.ZipFile(filename) - names = zf.namelist() + names = list(zf.namelist()) + if sort_alphabetically: + names.sort() for name in names: if match.search(name): return name, zf.read(name)