mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
extZ metadata: Read and write first opf file found in archive.
This commit is contained in:
parent
d5119f0c2f
commit
1d6521aa5e
@ -25,14 +25,30 @@ def get_metadata(stream, extract_cover=True):
|
|||||||
|
|
||||||
with TemporaryDirectory('_untxtz_mdata') as tdir:
|
with TemporaryDirectory('_untxtz_mdata') as tdir:
|
||||||
try:
|
try:
|
||||||
zf = ZipFile(stream)
|
with ZipFile(stream) as zf:
|
||||||
zf.extract('metadata.opf', tdir)
|
opf_name = get_first_opf_name(stream)
|
||||||
with open(os.path.join(tdir, 'metadata.opf'), 'rb') as opff:
|
opf_stream = StringIO(zf.read(opf_name))
|
||||||
mi = OPF(opff).to_book_metadata()
|
mi = OPF(opf_stream).to_book_metadata()
|
||||||
except:
|
except:
|
||||||
return mi
|
return mi
|
||||||
return mi
|
return mi
|
||||||
|
|
||||||
def set_metadata(stream, mi):
|
def set_metadata(stream, mi):
|
||||||
opf = StringIO(metadata_to_opf(mi))
|
opf = StringIO(metadata_to_opf(mi))
|
||||||
safe_replace(stream, 'metadata.opf', opf)
|
try:
|
||||||
|
opf_name = get_first_opf_name(stream)
|
||||||
|
except:
|
||||||
|
opf_name = 'metadata.opf'
|
||||||
|
safe_replace(stream, opf_name, opf)
|
||||||
|
|
||||||
|
def get_first_opf_name(stream):
|
||||||
|
with ZipFile(stream) as zf:
|
||||||
|
names = zf.namelist()
|
||||||
|
opfs = []
|
||||||
|
for n in names:
|
||||||
|
if n.endswith('.opf') and '/' not in n:
|
||||||
|
opfs.append(n)
|
||||||
|
if not opfs:
|
||||||
|
raise Exception('No OPF found')
|
||||||
|
opfs.sort()
|
||||||
|
return opfs[0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user