Skip to content

Commit

Permalink
feat(wordlist): add wordlist feature to kobuddy
Browse files Browse the repository at this point in the history
This adds a new flag 'wordlist' which allows to retrieve the wordlist of
the connected kobo device.
  • Loading branch information
hashier authored and karlicoss committed Aug 22, 2023
1 parent 7b36158 commit a3e0562
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/kobuddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,28 @@ def _load_highlights(bfile: Path, books: Books):
yield Highlight(bm, book=book)


def _load_wordlist(bfile: Path):
logger = get_logger()
logger.info(f"Using %s for highlights", bfile)
db = dataset_connect_ro(bfile)
for bm in db.query('SELECT * FROM WordList'):
yield bm['Text']


def get_highlights(**kwargs) -> List[Highlight]:
return list(sorted(_iter_highlights(**kwargs), key=lambda h: h.created))


def get_wordlist() -> Iterator[str]:
yielded: Set[str] = set()

for bfile in DATABASES:
for h in _load_wordlist(bfile):
if h not in yielded:
yield h
yielded.add(h)


# TODO Activity -- sort of interesting (e.g RecentBook). wonder what is Action (it's always 2)

# TODO not sure if need to be exposed
Expand Down Expand Up @@ -1027,3 +1046,8 @@ def print_annotations():
""".strip('\n')
print(h)
print("------")


def print_wordlist() -> None:
for i in get_wordlist():
print(i)
5 changes: 4 additions & 1 deletion src/kobuddy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import argparse
from pathlib import Path

from kobuddy import set_databases, print_progress, print_books, print_annotations, get_logger
from kobuddy import set_databases, print_progress, print_books, print_annotations, print_wordlist, get_logger

def setup_logger(logger, level=None, format=None, datefmt=None):
import logging
Expand Down Expand Up @@ -42,6 +42,7 @@ def main():
sp.add_parser('books' , help='print all books')
sp.add_parser('progress' , help='print all book reading progress')
sp.add_parser('annotations', help='print all annotations (bookmarks/highlights/comments)')
sp.add_parser('wordlist' , help='print all words from the wordlist')
bp = sp.add_parser('backup', help='backup the database from your Kobo device', description='''
You can run it via cron, for example every minute. When you connect your device via USB, the database will be backed up.
Expand All @@ -65,6 +66,8 @@ def main():
print_books()
elif args.mode == 'annotations':
print_annotations()
elif args.mode == 'wordlist':
print_wordlist()
else:
raise RuntimeError(f'Unexpected mode {args.mode}')

Expand Down

0 comments on commit a3e0562

Please sign in to comment.