mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
extZ metadata: Get cover, update OPF without losing other data such as spine, and guide.
This commit is contained in:
parent
1d6521aa5e
commit
184692b587
@ -7,13 +7,10 @@ __copyright__ = '2011, John Schember <john@nachtimwald.com>'
|
||||
Read meta information from extZ (TXTZ, HTMLZ...) files.
|
||||
'''
|
||||
|
||||
import os
|
||||
|
||||
from cStringIO import StringIO
|
||||
|
||||
from calibre.ebooks.metadata import MetaInformation
|
||||
from calibre.ebooks.metadata.opf2 import OPF, metadata_to_opf
|
||||
from calibre.ptempfile import TemporaryDirectory
|
||||
from calibre.ebooks.metadata.opf2 import OPF
|
||||
from calibre.utils.zipfile import ZipFile, safe_replace
|
||||
|
||||
def get_metadata(stream, extract_cover=True):
|
||||
@ -23,23 +20,32 @@ def get_metadata(stream, extract_cover=True):
|
||||
mi = MetaInformation(_('Unknown'), [_('Unknown')])
|
||||
stream.seek(0)
|
||||
|
||||
with TemporaryDirectory('_untxtz_mdata') as tdir:
|
||||
try:
|
||||
with ZipFile(stream) as zf:
|
||||
opf_name = get_first_opf_name(stream)
|
||||
opf_stream = StringIO(zf.read(opf_name))
|
||||
mi = OPF(opf_stream).to_book_metadata()
|
||||
except:
|
||||
return mi
|
||||
try:
|
||||
with ZipFile(stream) as zf:
|
||||
opf_name = get_first_opf_name(stream)
|
||||
opf_stream = StringIO(zf.read(opf_name))
|
||||
opf = OPF(opf_stream)
|
||||
mi = opf.to_book_metadata()
|
||||
if extract_cover:
|
||||
cover_name = opf.raster_cover
|
||||
if cover_name:
|
||||
mi.cover_data = ('jpg', zf.read(cover_name))
|
||||
except:
|
||||
return mi
|
||||
return mi
|
||||
|
||||
def set_metadata(stream, mi):
|
||||
opf = StringIO(metadata_to_opf(mi))
|
||||
try:
|
||||
opf_name = get_first_opf_name(stream)
|
||||
with ZipFile(stream) as zf:
|
||||
opf_stream = StringIO(zf.read(opf_name))
|
||||
opf = OPF(opf_stream)
|
||||
except:
|
||||
opf_name = 'metadata.opf'
|
||||
safe_replace(stream, opf_name, opf)
|
||||
opf = OPF(StringIO())
|
||||
opf.smart_update(mi, replace_metadata=True)
|
||||
newopf = StringIO(opf.render())
|
||||
safe_replace(stream, opf_name, newopf)
|
||||
|
||||
def get_first_opf_name(stream):
|
||||
with ZipFile(stream) as zf:
|
||||
|
Loading…
x
Reference in New Issue
Block a user