Skip to content

Commit

Permalink
get rid of azw3 offset, thanks to jhowell
Browse files Browse the repository at this point in the history
  • Loading branch information
xxyzz committed Nov 13, 2020
1 parent eb1cdfc commit 3b2d8f1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 25 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -19,4 +19,4 @@ def config_widget(self):
return ConfigWidget()

def save_settings(self, config_widget):
config_widget.save_settings()
pass
17 changes: 1 addition & 16 deletions config.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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')

Expand Down
12 changes: 7 additions & 5 deletions parse_job.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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'))

0 comments on commit 3b2d8f1

Please sign in to comment.