Skip to content

Commit

Permalink
Don't check Kindle device book's ASIN
Browse files Browse the repository at this point in the history
Update on deivce book is unreliable, users should always upload new
books to Kindle device.
  • Loading branch information
xxyzz committed Nov 22, 2023
1 parent f6c77c9 commit dbf4bec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 41 deletions.
8 changes: 8 additions & 0 deletions error_dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,11 @@ def unsupported_ww_lang_dialog(book_lang: str, gloss_lang: str) -> None:
book_lang, gloss_lang
),
)


def kindle_has_same_book_dialog(parent: Any) -> None:
warning_dialog(
_("Find same book on Kindle device"),
_("Remove the book from your device then try again."),
parent,
)
45 changes: 14 additions & 31 deletions metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ def random_asin() -> str:
return asin


def validate_asin(asin: str | None, mi: Any) -> tuple[str, bool]:
def validate_asin(asin: str | None, mi: Any) -> str:
# check ASIN, create a random one if doesn't exist
update_asin = False
if asin is None or re.fullmatch(r"B[0-9A-Z]{9}", asin) is None:
asin = random_asin()
mi.set_identifier("mobi-asin", asin)
update_asin = True
return asin, update_asin
return asin


class KFXJson(TypedDict):
Expand All @@ -120,41 +118,28 @@ class KFXJson(TypedDict):
type: int


def get_asin_etc(
data: "ParseJobData", device_book_path: str | None = None, set_en_lang: bool = False
) -> None:
def get_asin_etc(data: "ParseJobData", set_en_lang: bool = False) -> None:
if data.book_fmt == "KFX":
from calibre_plugins.kfx_input.kfxlib import YJ_Book

yj_book = YJ_Book(
str(device_book_path) if device_book_path is not None else data.book_path
)
yj_book = YJ_Book(data.book_path)
yj_md = yj_book.get_metadata()
book_asin = getattr(yj_md, "asin", "")
data.acr = getattr(yj_md, "asset_id", "")
book_lang = getattr(yj_md, "language", "en")
if device_book_path is None:
data.asin, data.update_asin = validate_asin(book_asin, data.mi)
elif book_asin != data.asin:
data.update_asin = True
data.asin = validate_asin(book_asin, data.mi)
update_asin = data.asin != book_asin
update_lang = False
if set_en_lang and book_lang != "en":
update_lang = True
book_lang = "en"
if data.update_asin or update_lang:
yj_book = update_kfx_metedata(
str(device_book_path)
if device_book_path is not None
else data.book_path,
data.asin,
book_lang,
)
if device_book_path is None:
data.kfx_json = json.loads(yj_book.convert_to_json_content())["data"]
if update_asin or update_lang:
yj_book = update_kfx_metedata(data.book_path, data.asin, book_lang)
data.kfx_json = json.loads(yj_book.convert_to_json_content())["data"]
elif data.book_fmt != "EPUB":
from calibre.ebooks.metadata.mobi import MetadataUpdater

with open(device_book_path or data.book_path, "r+b") as f:
with open(data.book_path, "r+b") as f:
data.acr = f.read(32).rstrip(b"\x00").decode("utf-8") # Palm db name
data.revision = get_mobi_revision(f)
f.seek(0)
Expand All @@ -164,22 +149,20 @@ def get_asin_etc(
113
) or mu.original_exth_records.get(504)
book_asin = asin_bytes.decode(mu.codec) if asin_bytes is not None else None
if device_book_path is None:
data.asin, data.update_asin = validate_asin(book_asin, data.mi)
elif book_asin != data.asin:
data.update_asin = True
data.asin = validate_asin(book_asin, data.mi)
locale = mu.record0[0x5C:0x60] # MOBI header locale
mi_lang = data.mi.language
update_asin = data.asin != book_asin
update_lang = False
if set_en_lang and locale[2:] != (9).to_bytes(2, "big"):
update_lang = True
locale = (9).to_bytes(4, "big")
mi_lang = "eng"
if data.update_asin or update_lang:
if update_asin or update_lang:
data.mi.language = mi_lang
mu.record0[0x5C:0x60] = locale
mu.update(data.mi, asin=data.asin)
if device_book_path is None:

data.mobi_html = extract_mobi(data.book_path)


Expand Down
1 change: 0 additions & 1 deletion parse_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class ParseJobData:
asin: str = ""
acr: str = ""
revision: str = ""
update_asin: bool = False
kfx_json: KFXJson | None = None
mobi_html: bytes | None = b""
mobi_codec: str = ""
Expand Down
12 changes: 3 additions & 9 deletions send_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from calibre.gui2.dialogs.message_box import JobError

from .database import get_ll_path, get_x_ray_path, is_same_klld
from .error_dialogs import kindle_epub_dialog
from .metadata import get_asin_etc
from .error_dialogs import kindle_epub_dialog, kindle_has_same_book_dialog
from .parse_job import ParseJobData
from .utils import (
get_plugin_path,
Expand Down Expand Up @@ -75,13 +74,8 @@ def send_files(self, job: Any) -> None:
device_mount_point = Path(self.device_manager.device._main_prefix)
device_book_path = device_mount_point.joinpath(paths.pop())
if job is None:
# update device book ASIN if it doesn't have the same ASIN
get_asin_etc(self.job_data, device_book_path, set_en_lang)
if self.job_data.update_asin: # Re-upload book cover
self.gui.update_thumbnail(self.job_data.mi)
self.device_manager.device.upload_kindle_thumbnail(
self.job_data.mi, self.job_data.book_path
)
kindle_has_same_book_dialog(self.gui)
return

self.move_files_to_kindle(device_mount_point, device_book_path)
library_book_path = Path(self.job_data.book_path)
Expand Down

0 comments on commit dbf4bec

Please sign in to comment.