Implement #1021 (CBR and CBZ cover detection)

This commit is contained in:
Kovid Goyal 2008-11-09 12:23:55 -08:00
parent c5922e0970
commit f9c2956ada
3 changed files with 37 additions and 39 deletions

View File

@ -20,8 +20,8 @@ from calibre.ebooks.metadata.opf import OPFReader
from calibre.ebooks.metadata.rtf import set_metadata as set_rtf_metadata from calibre.ebooks.metadata.rtf import set_metadata as set_rtf_metadata
from calibre.ebooks.lrf.meta import set_metadata as set_lrf_metadata from calibre.ebooks.lrf.meta import set_metadata as set_lrf_metadata
from calibre.ebooks.metadata.epub import set_metadata as set_epub_metadata from calibre.ebooks.metadata.epub import set_metadata as set_epub_metadata
from calibre.libunrar import extract_first as rar_extract_first from calibre.libunrar import extract_member as rar_extract_first
from calibre.libunzip import extract_first as zip_extract_first from calibre.libunzip import extract_member as zip_extract_first
from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.metadata import MetaInformation
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
@ -111,17 +111,12 @@ def get_metadata(stream, stream_type='lrf', use_libprs_metadata=False):
return base return base
def get_comic_cover(stream, type): def get_comic_cover(stream, type):
with TemporaryDirectory('_comic_cover') as tdir:
extract_first = zip_extract_first if type == 'zip' else rar_extract_first extract_first = zip_extract_first if type == 'zip' else rar_extract_first
extract_first(stream, tdir) ret = extract_first(stream)
files = os.listdir(tdir) if ret is not None:
print tdir, files path, data = ret
if files: ext = os.path.splitext(path)[1][1:]
path = os.path.join(tdir, files[0]) return (ext.lower(), data)
ext = os.path.splitext(path)[1].lower()
if ext:
ext = ext[1:]
return (ext, open(path, 'rb').read())

View File

@ -6,13 +6,14 @@ This module provides a thin ctypes based wrapper around libunrar.
See ftp://ftp.rarlabs.com/rar/unrarsrc-3.7.5.tar.gz See ftp://ftp.rarlabs.com/rar/unrarsrc-3.7.5.tar.gz
""" """
import os, ctypes, sys import os, ctypes, sys, re
from ctypes import Structure, c_char_p, c_uint, c_void_p, POINTER, \ from ctypes import Structure, c_char_p, c_uint, c_void_p, POINTER, \
byref, c_wchar_p, c_int, c_char, c_wchar byref, c_wchar_p, c_int, c_char, c_wchar
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from StringIO import StringIO from StringIO import StringIO
from calibre import iswindows, load_library, CurrentDir from calibre import iswindows, load_library, CurrentDir
from calibre.ptempfile import TemporaryDirectory
_librar_name = 'libunrar' _librar_name = 'libunrar'
cdll = ctypes.cdll cdll = ctypes.cdll
@ -185,15 +186,14 @@ def extract(path, dir):
os.chdir(cwd) os.chdir(cwd)
_libunrar.RARCloseArchive(arc_data) _libunrar.RARCloseArchive(arc_data)
def extract_first(path, dir): def extract_member(path, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I)):
if hasattr(path, 'read'): if hasattr(path, 'read'):
data = path.read() data = path.read()
f = NamedTemporaryFile(suffix='.rar') f = NamedTemporaryFile(suffix='.rar')
f.write(data) f.write(data)
f.flush() f.flush()
path = f.name path = f.name
if not os.path.isdir( dir ): with TemporaryDirectory('_libunrar') as dir:
os.makedirs(dir)
with CurrentDir(dir): with CurrentDir(dir):
open_archive_data = RAROpenArchiveDataEx(ArcName=path, OpenMode=RAR_OM_EXTRACT, CmtBuf=None) open_archive_data = RAROpenArchiveDataEx(ArcName=path, OpenMode=RAR_OM_EXTRACT, CmtBuf=None)
arc_data = _libunrar.RAROpenArchiveEx(byref(open_archive_data)) arc_data = _libunrar.RAROpenArchiveEx(byref(open_archive_data))
@ -201,11 +201,15 @@ def extract_first(path, dir):
if open_archive_data.OpenResult != 0: if open_archive_data.OpenResult != 0:
raise UnRARException(_interpret_open_error(open_archive_data.OpenResult, path)) raise UnRARException(_interpret_open_error(open_archive_data.OpenResult, path))
header_data = RARHeaderDataEx(CmtBuf=None) header_data = RARHeaderDataEx(CmtBuf=None)
while True:
if _libunrar.RARReadHeaderEx(arc_data, byref(header_data)) != 0: if _libunrar.RARReadHeaderEx(arc_data, byref(header_data)) != 0:
raise UnRARException('%s has no files'%path) raise UnRARException('%s has no files'%path)
PFCode = _libunrar.RARProcessFileW(arc_data, RAR_EXTRACT, None, None) PFCode = _libunrar.RARProcessFileW(arc_data, RAR_EXTRACT, None, None)
if PFCode != 0: if PFCode != 0:
raise UnRARException(_interpret_process_file_error(PFCode)) raise UnRARException(_interpret_process_file_error(PFCode))
if match.search(header_data.FileNameW):
return header_data.FileNameW.replace('/', os.sep), \
open(os.path.join(dir, *header_data.FileNameW.split('/')), 'rb').read()
finally: finally:
_libunrar.RARCloseArchive(arc_data) _libunrar.RARCloseArchive(arc_data)

View File

@ -3,7 +3,7 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net' __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os import os, re
from calibre.utils import zipfile from calibre.utils import zipfile
def update(pathtozip, patterns, filepaths, names, compression=zipfile.ZIP_DEFLATED, verbose=True): def update(pathtozip, patterns, filepaths, names, compression=zipfile.ZIP_DEFLATED, verbose=True):
@ -43,10 +43,9 @@ def extract(filename, dir):
zf = zipfile.ZipFile( filename ) zf = zipfile.ZipFile( filename )
zf.extractall(dir) zf.extractall(dir)
def extract_first(filename, dir): def extract_member(filename, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I)):
zf = zipfile.ZipFile(filename) zf = zipfile.ZipFile(filename)
names = zf.namelist() names = zf.namelist()
if not names: for name in names:
raise ValueError('%s has no files'%filename) if match.search(name):
bytes = zf.read(names[0]) return name, zf.read(name)
open(os.path.join(dir, names[0]), 'wb').write(bytes)