forked from amandasystems/kindle-clippings-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkindleclippingsparser.py
executable file
·146 lines (121 loc) · 5.41 KB
/
kindleclippingsparser.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
import codecs
class KindleClippingsParser():
class ParseError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def __init__(self, f):
self.fp = f
def parse(self):
notes = [unicode(n.strip().decode("utf-8")) for n in (self.fp.read().split("\n=========="))]
return (self.parse_note(note) for note in notes
if note != '')
def parse_note(self, note):
def collect_title(n, i):
title = unicode()
iterm = n[i:].split('\r\n')[0].rfind('(')
for index, c in enumerate(n[i:]):
if c == ' ':
# if the next character's an '(', we've found our terminator.
if index + 1 == iterm:
#print "got end of title."
return (title, index + 2 + i)
if note[index + 1] == '\n':
#raise self.ParseError("caught unexpected newline when looking for title (at %d). Got title '%s'" % (i, title))
return (title, index + 1 + i)
else:
title += c
def collect_author(n, i):
author = unicode()
for index, c in enumerate(n[i:]):
if c == ')':
# we've got our terminator!
#print "got end of author."
return (author, index + 1 + i)
elif note[index + 1] == '\n':
# print "Caught unexpected newline when looking for author. Assuming unknown or omitted."
return ("Unknown", i-1)
else:
author += c
def collect_note_highlight(n, i):
if n[i] == '\r' and n[i+1] == '\n' and n[i+2] == '-' and n[i+3] == ' ':
i += 4
note = unicode()
for index, c in enumerate(n[i:]):
if c == ' ':
#print "got end of note-highlight."
return (note, index + 1 + i)
else:
# here should follow a "-" and Note|Highlight
if n[i:][:4] == 'Note':
#print "it's a note"
return ("Note", index + 4 + i)
elif n[i:][:9] == 'Highlight':
#print "it's a highlight"
return ("Highlight", index + 9 + i)
elif n[i:][:8] == 'Bookmark':
return ("Bookmark", index + 8 + i)
else:
raise self.ParseError("parse error: expected Note, Highlight or Bookmark at %d" % i)
else:
raise self.ParseError("parse error at %d. Expected '\r\n- ', got '%s'." % (i, n[i:][:3]))
exit
def collect_location(n, i):
loc = unicode()
if n[i:][:4] == ' on ':
i += 4
for index, c in enumerate(n[i:]):
if c == '|':
#print "got end-of-location"
loc = loc.strip() + ','
i += 1 + index
break
else:
loc += c
if n[i:][:6] == " Loc. ":
i += 6
for index, c in enumerate(n[i:]):
if c == '|':
#print "got end-of-location"
return loc.strip(), i + 1 + index
else:
if c != ' ':
loc += c
else:
raise self.ParseError("parse error at %d. Expected ' Loc.'" % i)
def collect_datetime(n, i):
if n[i:][:10] == ' Added on ':
i += 10
try:
end = n[i:].index('\r\n\r\n')
except ValueError:
end = len(n)
try:
date = datetime.strptime(n[i:][:end], '%A, %B %d, %Y, %I:%M %p')
return date, end+i+4 # skip the two newlines
except ValueError:
raise self.ParseError("unable to parse date string '%s' at location %i. Around there was chars '%s'"
% (n[i:][:end], i, n[i:][:4]))
else:
raise self.ParseError("parse error at %d. Expected ' Added on '" % i)
def collect_text(n, i):
if not n[i:] == "<This item is copy protected>":
return unicode(n[i:]), len(n)
else:
# damned DRM
return None, len(n)
# END helper functions
index = 0
title, index = collect_title(note, index)
author, index = collect_author(note, index)
type, index = collect_note_highlight(note, index)
location, index = collect_location(note, index)
date, index = collect_datetime(note, index)
text, index = collect_text(note, index)
return {'title': title, 'author': author,
'type': type, 'location': location,
'date': date, 'text': text}