mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 18:54: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.
|
Read meta information from extZ (TXTZ, HTMLZ...) files.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os
|
|
||||||
|
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
|
||||||
from calibre.ebooks.metadata import MetaInformation
|
from calibre.ebooks.metadata import MetaInformation
|
||||||
from calibre.ebooks.metadata.opf2 import OPF, metadata_to_opf
|
from calibre.ebooks.metadata.opf2 import OPF
|
||||||
from calibre.ptempfile import TemporaryDirectory
|
|
||||||
from calibre.utils.zipfile import ZipFile, safe_replace
|
from calibre.utils.zipfile import ZipFile, safe_replace
|
||||||
|
|
||||||
def get_metadata(stream, extract_cover=True):
|
def get_metadata(stream, extract_cover=True):
|
||||||
@ -23,23 +20,32 @@ def get_metadata(stream, extract_cover=True):
|
|||||||
mi = MetaInformation(_('Unknown'), [_('Unknown')])
|
mi = MetaInformation(_('Unknown'), [_('Unknown')])
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
|
|
||||||
with TemporaryDirectory('_untxtz_mdata') as tdir:
|
try:
|
||||||
try:
|
with ZipFile(stream) as zf:
|
||||||
with ZipFile(stream) as zf:
|
opf_name = get_first_opf_name(stream)
|
||||||
opf_name = get_first_opf_name(stream)
|
opf_stream = StringIO(zf.read(opf_name))
|
||||||
opf_stream = StringIO(zf.read(opf_name))
|
opf = OPF(opf_stream)
|
||||||
mi = OPF(opf_stream).to_book_metadata()
|
mi = opf.to_book_metadata()
|
||||||
except:
|
if extract_cover:
|
||||||
return mi
|
cover_name = opf.raster_cover
|
||||||
|
if cover_name:
|
||||||
|
mi.cover_data = ('jpg', zf.read(cover_name))
|
||||||
|
except:
|
||||||
|
return mi
|
||||||
return mi
|
return mi
|
||||||
|
|
||||||
def set_metadata(stream, mi):
|
def set_metadata(stream, mi):
|
||||||
opf = StringIO(metadata_to_opf(mi))
|
|
||||||
try:
|
try:
|
||||||
opf_name = get_first_opf_name(stream)
|
opf_name = get_first_opf_name(stream)
|
||||||
|
with ZipFile(stream) as zf:
|
||||||
|
opf_stream = StringIO(zf.read(opf_name))
|
||||||
|
opf = OPF(opf_stream)
|
||||||
except:
|
except:
|
||||||
opf_name = 'metadata.opf'
|
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):
|
def get_first_opf_name(stream):
|
||||||
with ZipFile(stream) as zf:
|
with ZipFile(stream) as zf:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user