From 78c014bf2216534e0691412cd05b8b93dce47bf5 Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Thu, 7 Nov 2024 21:53:59 -0800 Subject: [PATCH] simplify cover upload on newer MTP based Kindles by replacing EBOK with PDOC (#767) * simplify covers by replacing ebok with pdoc * refactor * fix order --- kindlecomicconverter/KCC_gui.py | 2 +- kindlecomicconverter/comic2ebook.py | 5 +++-- kindlecomicconverter/dualmetafix.py | 10 +++++++--- kindlecomicconverter/kindle.py | 7 ++++++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/kindlecomicconverter/KCC_gui.py b/kindlecomicconverter/KCC_gui.py index 880ca0b..6b2370c 100644 --- a/kindlecomicconverter/KCC_gui.py +++ b/kindlecomicconverter/KCC_gui.py @@ -380,7 +380,7 @@ class WorkerThread(QtCore.QThread): except Exception: pass MW.addMessage.emit('Processing MOBI files... Done!', 'info', True) - k = kindle.Kindle() + k = kindle.Kindle(options.profile) if k.path and k.coverSupport: for item in outputPath: comic2ebook.options.covers[outputPath.index(item)][0].saveToKindle( diff --git a/kindlecomicconverter/comic2ebook.py b/kindlecomicconverter/comic2ebook.py index 2719ca7..c947fca 100755 --- a/kindlecomicconverter/comic2ebook.py +++ b/kindlecomicconverter/comic2ebook.py @@ -1227,7 +1227,7 @@ def makeBook(source, qtgui=None): print('Error: KindleGen failed to create MOBI!') print(errors) return filepath - k = kindle.Kindle() + k = kindle.Kindle(options.profile) if k.path and k.coverSupport: print("Kindle detected. Uploading covers...") for i in filepath: @@ -1249,12 +1249,13 @@ def makeBook(source, qtgui=None): def makeMOBIFix(item, uuid): + is_pdoc = options.profile in image.ProfileData.ProfilesKindlePDOC.keys() if not options.keep_epub: os.remove(item) mobiPath = item.replace('.epub', '.mobi') move(mobiPath, mobiPath + '_toclean') try: - dualmetafix.DualMobiMetaFix(mobiPath + '_toclean', mobiPath, bytes(uuid, 'UTF-8')) + dualmetafix.DualMobiMetaFix(mobiPath + '_toclean', mobiPath, bytes(uuid, 'UTF-8'), is_pdoc) return [True] except Exception as err: return [False, format(err)] diff --git a/kindlecomicconverter/dualmetafix.py b/kindlecomicconverter/dualmetafix.py index 7426f93..beddc5a 100644 --- a/kindlecomicconverter/dualmetafix.py +++ b/kindlecomicconverter/dualmetafix.py @@ -136,7 +136,11 @@ def del_exth(rec0, exth_num): class DualMobiMetaFix: - def __init__(self, infile, outfile, asin): + def __init__(self, infile, outfile, asin, is_pdoc): + cdetype = b'EBOK' + if is_pdoc: + cdetype = b'PDOC' + shutil.copyfile(infile, outfile) f = open(outfile, "r+b") self.datain = mmap.mmap(f.fileno(), 0) @@ -147,7 +151,7 @@ class DualMobiMetaFix: rec0 = self.datain_rec0 rec0 = del_exth(rec0, 501) rec0 = del_exth(rec0, 113) - rec0 = add_exth(rec0, 501, b'EBOK') + rec0 = add_exth(rec0, 501, cdetype) rec0 = add_exth(rec0, 113, asin) replacesection(self.datain, 0, rec0) @@ -174,7 +178,7 @@ class DualMobiMetaFix: rec0 = self.datain_kfrec0 rec0 = del_exth(rec0, 501) rec0 = del_exth(rec0, 113) - rec0 = add_exth(rec0, 501, b'EBOK') + rec0 = add_exth(rec0, 501, cdetype) rec0 = add_exth(rec0, 113, asin) replacesection(self.datain, datain_kf8, rec0) diff --git a/kindlecomicconverter/kindle.py b/kindlecomicconverter/kindle.py index 535f3a5..34ffcff 100644 --- a/kindlecomicconverter/kindle.py +++ b/kindlecomicconverter/kindle.py @@ -19,9 +19,12 @@ import os.path import psutil +from . import image + class Kindle: - def __init__(self): + def __init__(self, profile): + self.profile = profile self.path = self.findDevice() if self.path: self.coverSupport = self.checkThumbnails() @@ -29,6 +32,8 @@ class Kindle: self.coverSupport = False def findDevice(self): + if self.profile in image.ProfileData.ProfilesKindlePDOC.keys(): + return False for drive in reversed(psutil.disk_partitions(False)): if (drive[2] == 'FAT32' and drive[3] == 'rw,removable') or \ (drive[2] in ('vfat', 'msdos', 'FAT', 'apfs') and 'rw' in drive[3]):