aboutsummaryrefslogtreecommitdiff
path: root/noto.py
blob: 605df62c23b25043514bee84421537f55af6b939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3

import argparse
import os
import re
import sqlite3

class Noto:
    def __init__(self, path):
        con = sqlite3.connect(path)
        cur = con.cursor()
        query = ("select "
                 "Bookmark.Text,"
                 "Bookmark.ContentID "
                 "from "
                 "Bookmark,"
                 "content "
                 "WHERE "
                 "Bookmark.ContentID = content.ContentID;")
        cur.execute(query)
        self.data = cur.fetchall()
        cur.close()
        con.close()

    def _sanitize(self, name):
        name = re.sub(r"[^a-zA-Z0-9_.\ ]", "", name)
        name = re.sub(" +", " ", name)
        return os.path.splitext(os.path.basename(name))[0]

    def _prepare(self, highlight):
        res = highlight.strip()
        res = res + '.' if res and res[-1] not in ('.', '!', '?') else res
        res = res[0].upper() + res[1:]
        return res

    def _write(self, name, highlight):
        with open(name + ".txt", "a+") as record:
            record.write(highlight)
            record.write("\n\n")

    def process(self):
        for entry in self.data:
            name = os.path.basename(entry[1].split("!!")[0])
            name = self._sanitize(name)
            highlight = self._prepare(entry[0])

            self._write(name, highlight)

if __name__ == "__main__":
    desc = "noto: extract highlights from Kobo e-reader data store"
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("--path", dest="path", type=str, action="store",
                        help="path to KoboReader.sqlite database",
                        required=True)
    args = parser.parse_args()

    noto = Noto(args.path)
    noto.process()