mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Fix regression in epub2lrf when converting EPUB files that specify a HTML page as a cover
This commit is contained in:
parent
dad9ec898f
commit
e3b148e84a
@ -2,14 +2,14 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
import os, sys, shutil, logging
|
import os, sys, shutil, logging
|
||||||
from tempfile import mkdtemp
|
|
||||||
from calibre.ebooks.lrf import option_parser as lrf_option_parser
|
from calibre.ebooks.lrf import option_parser as lrf_option_parser
|
||||||
from calibre.ebooks import ConversionError, DRMError
|
from calibre.ebooks import ConversionError, DRMError
|
||||||
from calibre.ebooks.lrf.html.convert_from import process_file as html_process_file
|
from calibre.ebooks.lrf.html.convert_from import process_file as html_process_file
|
||||||
from calibre.ebooks.metadata.opf import OPF
|
from calibre.ebooks.metadata.opf import OPF
|
||||||
from calibre.ebooks.metadata.epub import OCFDirReader
|
from calibre.ebooks.metadata.epub import OCFDirReader
|
||||||
from calibre.utils.zipfile import ZipFile
|
from calibre.utils.zipfile import ZipFile
|
||||||
from calibre import __appname__, setup_cli_handlers
|
from calibre import setup_cli_handlers
|
||||||
|
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||||
|
|
||||||
|
|
||||||
def option_parser():
|
def option_parser():
|
||||||
@ -22,17 +22,16 @@ _('''Usage: %prog [options] mybook.epub
|
|||||||
|
|
||||||
def generate_html(pathtoepub, logger):
|
def generate_html(pathtoepub, logger):
|
||||||
if not os.access(pathtoepub, os.R_OK):
|
if not os.access(pathtoepub, os.R_OK):
|
||||||
raise ConversionError, 'Cannot read from ' + pathtoepub
|
raise ConversionError('Cannot read from ' + pathtoepub)
|
||||||
tdir = mkdtemp(prefix=__appname__+'_')
|
tdir = PersistentTemporaryDirectory('_epub2lrf')
|
||||||
os.rmdir(tdir)
|
#os.rmdir(tdir)
|
||||||
try:
|
try:
|
||||||
ZipFile(pathtoepub).extractall(tdir)
|
ZipFile(pathtoepub).extractall(tdir)
|
||||||
|
except:
|
||||||
|
raise ConversionError, '.epub extraction failed'
|
||||||
if os.path.exists(os.path.join(tdir, 'META-INF', 'encryption.xml')):
|
if os.path.exists(os.path.join(tdir, 'META-INF', 'encryption.xml')):
|
||||||
raise DRMError(os.path.basename(pathtoepub))
|
raise DRMError(os.path.basename(pathtoepub))
|
||||||
except:
|
|
||||||
if os.path.exists(tdir) and os.path.isdir(tdir):
|
|
||||||
shutil.rmtree(tdir)
|
|
||||||
raise ConversionError, '.epub extraction failed'
|
|
||||||
return tdir
|
return tdir
|
||||||
|
|
||||||
def process_file(path, options, logger=None):
|
def process_file(path, options, logger=None):
|
||||||
|
@ -1982,17 +1982,7 @@ def try_opf(path, options, logger):
|
|||||||
PILImage.open(cover)
|
PILImage.open(cover)
|
||||||
options.cover = cover
|
options.cover = cover
|
||||||
except:
|
except:
|
||||||
for prefix in opf.possible_cover_prefixes():
|
pass
|
||||||
if options.cover:
|
|
||||||
break
|
|
||||||
for suffix in ['.jpg', '.jpeg', '.gif', '.png', '.bmp']:
|
|
||||||
cpath = os.path.join(os.path.dirname(path), prefix+suffix)
|
|
||||||
try:
|
|
||||||
PILImage.open(cpath)
|
|
||||||
options.cover = cpath
|
|
||||||
break
|
|
||||||
except:
|
|
||||||
continue
|
|
||||||
if not getattr(options, 'cover', None) and orig_cover is not None:
|
if not getattr(options, 'cover', None) and orig_cover is not None:
|
||||||
options.cover = orig_cover
|
options.cover = orig_cover
|
||||||
if getattr(opf, 'spine', False):
|
if getattr(opf, 'spine', False):
|
||||||
|
@ -418,6 +418,7 @@ class OPF(object):
|
|||||||
tags_path = XPath('descendant::*[re:match(name(), "subject", "i")]')
|
tags_path = XPath('descendant::*[re:match(name(), "subject", "i")]')
|
||||||
isbn_path = XPath('descendant::*[re:match(name(), "identifier", "i") and '+
|
isbn_path = XPath('descendant::*[re:match(name(), "identifier", "i") and '+
|
||||||
'(re:match(@scheme, "isbn", "i") or re:match(@opf:scheme, "isbn", "i"))]')
|
'(re:match(@scheme, "isbn", "i") or re:match(@opf:scheme, "isbn", "i"))]')
|
||||||
|
identifier_path = XPath('descendant::*[re:match(name(), "identifier", "i")]')
|
||||||
application_id_path = XPath('descendant::*[re:match(name(), "identifier", "i") and '+
|
application_id_path = XPath('descendant::*[re:match(name(), "identifier", "i") and '+
|
||||||
'(re:match(@opf:scheme, "calibre|libprs500", "i") or re:match(@scheme, "calibre|libprs500", "i"))]')
|
'(re:match(@opf:scheme, "calibre|libprs500", "i") or re:match(@scheme, "calibre|libprs500", "i"))]')
|
||||||
manifest_path = XPath('descendant::*[re:match(name(), "manifest", "i")]/*[re:match(name(), "item", "i")]')
|
manifest_path = XPath('descendant::*[re:match(name(), "manifest", "i")]/*[re:match(name(), "item", "i")]')
|
||||||
@ -719,6 +720,27 @@ class OPF(object):
|
|||||||
return property(fget=fget, fset=fset)
|
return property(fget=fget, fset=fset)
|
||||||
|
|
||||||
|
|
||||||
|
def guess_cover(self):
|
||||||
|
'''
|
||||||
|
Try to guess a cover. Needed for some old/badly formed OPF files.
|
||||||
|
'''
|
||||||
|
if self.base_dir and os.path.exists(self.base_dir):
|
||||||
|
for item in self.identifier_path(self.metadata):
|
||||||
|
scheme = None
|
||||||
|
for key in item.attrib.keys():
|
||||||
|
if key.endswith('scheme'):
|
||||||
|
scheme = item.get(key)
|
||||||
|
break
|
||||||
|
if scheme is None:
|
||||||
|
continue
|
||||||
|
if item.text:
|
||||||
|
prefix = item.text.replace('-', '')
|
||||||
|
for suffix in ['.jpg', '.jpeg', '.gif', '.png', '.bmp']:
|
||||||
|
cpath = os.access(os.path.join(self.base_dir, prefix+suffix), os.R_OK)
|
||||||
|
if os.access(os.path.join(self.base_dir, prefix+suffix), os.R_OK):
|
||||||
|
return cpath
|
||||||
|
|
||||||
|
|
||||||
@apply
|
@apply
|
||||||
def cover():
|
def cover():
|
||||||
|
|
||||||
@ -728,6 +750,10 @@ class OPF(object):
|
|||||||
for item in self.guide:
|
for item in self.guide:
|
||||||
if item.type.lower() == t:
|
if item.type.lower() == t:
|
||||||
return item.path
|
return item.path
|
||||||
|
try:
|
||||||
|
return self.guess_cover()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
def fset(self, path):
|
def fset(self, path):
|
||||||
if self.guide is not None:
|
if self.guide is not None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user