From 1fcf1e01b23c6220151f943ce2f9a995ffde85e8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 10 Apr 2015 13:06:02 +0530 Subject: [PATCH] When getting cover from comic files, use smart filename sorting to find the first filename, recognizing numbers inside the filenames. --- src/calibre/libunzip.py | 8 ++++---- src/calibre/utils/unrar.py | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/calibre/libunzip.py b/src/calibre/libunzip.py index 86dc2de4a4..172d1ce512 100644 --- a/src/calibre/libunzip.py +++ b/src/calibre/libunzip.py @@ -40,15 +40,15 @@ def extract(filename, dir): """ Extract archive C{filename} into directory C{dir} """ - zf = zipfile.ZipFile( filename ) + zf = zipfile.ZipFile(filename) zf.extractall(dir) -def extract_member(filename, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', - re.I), sort_alphabetically=False): +def extract_member(filename, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I), sort_alphabetically=False): zf = zipfile.ZipFile(filename) names = list(zf.namelist()) if sort_alphabetically: - names.sort() + from calibre.utils.icu import numeric_sort_key + names.sort(key=numeric_sort_key) for name in names: if match.search(name): return name, zf.read(name) diff --git a/src/calibre/utils/unrar.py b/src/calibre/utils/unrar.py index a7b46b1f5d..96f0d2820b 100644 --- a/src/calibre/utils/unrar.py +++ b/src/calibre/utils/unrar.py @@ -185,8 +185,9 @@ def extract_member(stream, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I), return h['filename'], et.getvalue() def extract_first_alphabetically(stream): + from calibre.utils.icu import numeric_sort_key names_ = sorted([x for x in names(stream) if os.path.splitext(x)[1][1:].lower() in - {'png', 'jpg', 'jpeg', 'gif'}]) + {'png', 'jpg', 'jpeg', 'gif'}], key=numeric_sort_key) return extract_member(stream, name=names_[0], match=None) # Test normal RAR file {{{