导出Nook Reader笔记

Nook 内置的Reader对epub和文字版的pdf支持还是比较好的(中文就坑爹了, 要自己换个字体进去), 可以直接进行文字高亮操作, 并可以标注笔记.

此脚本可以直接将笔记导出成txt格式, 并保留页码信息, 方便反查, 适用于对于整理笔记时候, 不想再一个一个字打的懒人 XD

导出格式:

  • 文件名: 书名.txt
  • 导出的笔记按照页码排序
  • 每条记录格式: -页码- 高亮文本[标注的笔记(如果有的话)]

要求:

  • device: Nook已经root, 并且有adb
  • PC: adb, 并且可以识别Nook设备.
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
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python2
import os
import tempfile
import sqlite3


class DBExportor(object):
    def __init__(self, db=None):
        if not db:
            db = self.pull_db()
        self.conn = sqlite3.connect(db)
        self.conn.row_factory = sqlite3.Row
        self.files = {}

    def pull_db(self):
        ''' pull Nook Reader's db from device and return the path of that db '''
        db_name = "annotations.db"
        pull_db = "/data/data/com.bn.nook.reader.activities/databases/" + db_name
        db_path = os.path.join(tempfile.gettempdir(), db_name)
        os.system("adb pull %s %s" % (pull_db, db_path))
        return db_path

    def saverow(self, row):
        ''' save one row to file '''
        # get fp
        fname = os.path.basename(row['ean'])
        if fname not in self.files:  # create outfile if necessary
            base = os.path.splitext(fname)[0]
            export_file = base + '.txt'
            self.files[fname] = open(export_file, 'w')
        fp = self.files[fname]

        # composite a record
        record = "-%s-" % row["pagenumber"]
        if row['highlighttext']:
            record += ' ' + row['highlighttext']
        if row['note']:
            record += ' [Note: %s]' % row['note']
        record += '\n'

        # write to file
        fp.write(record.encode('utf8'))

    def export_db(self):
        c = self.conn.cursor()

        table = "annotations"
        c.execute("SELECT * FROM %s" % table)
        # since pagenumber is a string, need convert to int
        rows = c.fetchall()
        rows.sort(key=lambda c: int(c['pagenumber']))
        for row in rows:
            self.saverow(row)
        c.close()

    def cleanup(self):
        self.conn.close()
        for f in self.files.values():
            f.close()

    def run(self):
        self.export_db()
        self.cleanup()


if __name__ == '__main__':
    exp = DBExportor()
    exp.run()

https://gist.github.com/oneyoung/11240995

download this file

留言