Kindle driver: Create cover thumbnails on the device when transferring KFX format books

Merge branch 'master' of https://github.com/j-howell/calibre
This commit is contained in:
Kovid Goyal 2018-08-06 09:25:42 +05:30
commit 1837438d85
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 3 deletions

View File

@ -463,18 +463,28 @@ class KINDLE2(KINDLE):
self.upload_apnx(path, filename, metadata, filepath)
def thumbpath_from_filepath(self, filepath):
from calibre.ebooks.metadata.kfx import (CONTAINER_MAGIC, read_book_key_kfx)
from calibre.ebooks.mobi.reader.headers import MetadataHeader
from calibre.utils.logging import default_log
thumb_dir = os.path.join(self._main_prefix, 'system', 'thumbnails')
if not os.path.exists(thumb_dir):
return
with lopen(filepath, 'rb') as f:
mh = MetadataHeader(f, default_log)
if mh.exth is None or not mh.exth.uuid or not mh.exth.cdetype:
is_kfx = f.read(4) == CONTAINER_MAGIC
f.seek(0)
uuid = cdetype = None
if is_kfx:
uuid, cdetype = read_book_key_kfx(f)
else:
mh = MetadataHeader(f, default_log)
if mh.exth is not None:
uuid = mh.exth.uuid
cdetype = mh.exth.cdetype
if not uuid or not cdetype:
return
return os.path.join(thumb_dir,
'thumbnail_{uuid}_{cdetype}_portrait.jpg'.format(
uuid=mh.exth.uuid, cdetype=mh.exth.cdetype))
uuid=uuid, cdetype=cdetype))
def upload_kindle_thumbnail(self, metadata, filepath):
coverdata = getattr(metadata, 'thumbnail', None)

View File

@ -278,6 +278,17 @@ def dump_metadata(m):
pprint(d)
def read_book_key_kfx(stream, read_cover=True):
' Read the metadata.kfx file that is found in the sdr book folder for KFX files '
c = Container(stream.read())
m = extract_metadata(c.decode())
def val(x):
return m[x][0] if x in m else ''
return (val('content_id') or val('ASIN')), val('cde_content_type')
def read_metadata_kfx(stream, read_cover=True):
' Read the metadata.kfx file that is found in the sdr book folder for KFX files '
c = Container(stream.read())