From 3b2d8f10fedde065a9204785ab5618cf1e32155f Mon Sep 17 00:00:00 2001 From: xxyzz Date: Fri, 13 Nov 2020 17:38:28 +0800 Subject: [PATCH] get rid of azw3 offset, thanks to jhowell --- README.md | 2 -- __init__.py | 4 ++-- config.py | 17 +---------------- parse_job.py | 12 +++++++----- 4 files changed, 10 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index b7d9bc3..b3bce05 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,6 @@ Add the plugin to "The context menu for the books in the calibre library" in "Pr If your Kindle device is connected, it will send the file to your device. -For AZW3 books you may have to adjust the offset manually. For example, if you find the word is 16 characters(include white space) behind where it should be, set -16 in the plugin settings. - ## License This work is licensed under GPL version 3 or later. diff --git a/__init__.py b/__init__.py index 30dff4d..aadc778 100644 --- a/__init__.py +++ b/__init__.py @@ -7,7 +7,7 @@ class WordDumbDumb(InterfaceActionBase): description = 'Create Kindle Word Wise file.' supported_platforms = ['linux', 'osx', 'windows'] author = 'xxyzz' - version = (1, 2, 0) + version = (1, 3, 0) minimum_calibre_version = (5, 0, 0) # Python3 actual_plugin = 'calibre_plugins.worddumb.ui:WordDumb' @@ -19,4 +19,4 @@ def config_widget(self): return ConfigWidget() def save_settings(self, config_widget): - config_widget.save_settings() + pass diff --git a/config.py b/config.py index bae1743..cd0cb1c 100644 --- a/config.py +++ b/config.py @@ -1,29 +1,17 @@ #!/usr/bin/env python3 from calibre.utils.config import JSONConfig -from PyQt5.Qt import QWidget, QHBoxLayout, QLabel, QLineEdit, QPushButton, QVBoxLayout +from PyQt5.Qt import QWidget, QPushButton, QVBoxLayout import webbrowser prefs = JSONConfig('plugins/worddumb') -prefs.defaults['offset'] = '0' - class ConfigWidget(QWidget): def __init__(self): QWidget.__init__(self) self.vl = QVBoxLayout() self.setLayout(self.vl) - self.hl = QHBoxLayout() - self.vl.addLayout(self.hl) - - self.label = QLabel('Offset:') - self.hl.addWidget(self.label) - - self.offset = QLineEdit(self) - self.offset.setText(prefs['offset']) - self.hl.addWidget(self.offset) - self.label.setBuddy(self.offset) self.donate_button = QPushButton('Donate', self) self.donate_button.clicked.connect(self.donate) @@ -33,9 +21,6 @@ def __init__(self): self.github_button.clicked.connect(self.github) self.vl.addWidget(self.github_button) - def save_settings(self): - prefs['offset'] = self.offset.text() - def donate(self): webbrowser.open('https://liberapay.com/xxyzz/donate') diff --git a/parse_job.py b/parse_job.py index c188263..e1f5693 100644 --- a/parse_job.py +++ b/parse_job.py @@ -1,11 +1,10 @@ #!/usr/bin/env python3 from calibre.ebooks.mobi.reader.mobi6 import MobiReader +from calibre.ebooks.mobi.reader.mobi8 import Mobi8Reader from calibre.utils.logging import default_log from calibre_plugins.worddumb.database import connect_ww_database, \ create_lang_layer, match_word -from calibre_plugins.worddumb.config import prefs -from pathlib import Path import re def do_job(gui, books, abort, log, notifications): @@ -33,6 +32,12 @@ def parse_book(pathtoebook, book_fmt): offset = mobiReader.kf8_boundary + 2 mobiReader.extract_text(offset=offset) html = mobiReader.mobi_html + if book_fmt.lower() == 'azw3': + m8r = Mobi8Reader(mobiReader, default_log) + m8r.kf8_sections = mobiReader.sections[offset-1:] + m8r.read_indices() + m8r.build_parts() + html = b''.join(m8r.parts) # match text between HTML tags for match_text in re.finditer(b">[^<>]+<", html): @@ -41,7 +46,4 @@ def parse_book(pathtoebook, book_fmt): for match_word in re.finditer(b"[a-zA-Z]+", text): lemma = text[match_word.start():match_word.end()] start = match_text.start() + match_word.start() - if book_fmt.lower() == 'azw3': - start -= 14 # this value works for some books of mine - start += int(prefs['offset']) yield (start, lemma.decode('utf-8'))