Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 116 lines (91 sloc) 2.86 KB
#!/usr/bin/env python3
from os.path import lexists
from datetime import datetime
from pathlib import Path
import logging
import re
import sys
from kython.org import date2org, datetime2org, append_org_entry
from kython.state import JsonState
from kython.klogging import setup_logzero
from config import BOOKS, STATE_PATH, ORG_PATH
from my.books.kobo import Highlight, by_annotation # type: ignore
def get_logger():
return logging.getLogger('krill')
class Krill:
def __init__(self):
self.state = JsonState(STATE_PATH)
self.org_incoming_path = Path(ORG_PATH)
self.logger = get_logger()
def handled(self, item: Highlight):
return item.eid in self.state.get()
def mark_done(self, item: Highlight):
st = self.state.get()
st[item.eid] = str(item)
self.state.update(st)
def add(self, item: Highlight):
if self.handled(item):
self.logger.info(f"skipping {item}, as it's already been added to drill")
return
self.logger.info("adding %s", item)
append_org_entry(
self.org_incoming_path,
heading=item.text,
body='from ' + item.title,
created=item.dt,
tags=['drill'],
)
self.mark_done(item)
print(f"added {item} to drill")
def main():
logger = get_logger()
setup_logzero(logger, level=logging.DEBUG)
datas = by_annotation(lambda _: True)
by_title = {}
for d in datas:
ll = by_title.get(d.title, [])
ll.append(d)
by_title[d.title] = ll
# TODO go through all??
errors = False
drilly = []
for b in BOOKS:
if b not in by_title:
# TODO how to exclude books from drill?
logger.warning(f"Book {b} is tracked in drill, but not known to Kobo")
errors = True
else:
drilly.extend(by_title[b])
def is_candidate(i: Highlight):
if i.kind == 'bookmark':
return
words = i.text.strip().split()
ann = i.annotation
if ann is None:
ann = ''
if len(words) > 1 and ann.strip().lower() != 'drill':
return False
else: # I mean, what else could it be??
return True
extra = []
for b, lst in by_title.items():
if b not in BOOKS:
extra.extend([d for d in lst if is_candidate(d)])
filtered = []
for d in drilly:
if is_candidate(d):
filtered.append(d)
else:
logger.info(f"skipping {d}, it doesn't look like a good drill candidate")
drilly = filtered
drilly = list(sorted(drilly, key=lambda d: d.dt))
krill = Krill()
for d in drilly:
krill.add(d)
if len(extra) > 0:
logger.info("extra candidates for drill:")
for e in extra:
logger.info(e)
if errors:
sys.exit(1)
main()
You can’t perform that action at this time.