Skip to content

Commit 896f3ea

Browse files
committed
add: initial
1 parent 904bd57 commit 896f3ea

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

README.org

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Currently only tested with Tolino Shine 2 HD 1.94, *German* firmware

export.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
4+
import org
5+
6+
import re
7+
8+
from datetime import datetime
9+
10+
import shutil
11+
import os
12+
13+
state_file_name = '.last_export.orgexport'
14+
15+
def main():
16+
17+
18+
ap = argparse.ArgumentParser()
19+
ap.add_argument('inputfile')
20+
21+
args = ap.parse_args()
22+
23+
#do not try to import notes from your ebookreader from before 1970
24+
last_export = datetime(year=1970, month=1, day=1)
25+
last_export_lines = []
26+
state_file = os.path.join(os.path.dirname(args.inputfile), state_file_name)
27+
28+
if os.path.exists(state_file):
29+
with open(state_file, 'r') as f:
30+
lines = f.read()
31+
lines = lines.split('\n')
32+
last_export_lines = [l for l in lines if l]
33+
last_export = datetime.strptime(last_export_lines[-1], org.date_format)
34+
35+
with open(args.inputfile, 'r') as inputfile:
36+
text = inputfile.read()
37+
38+
note_sep = '''
39+
-----------------------------------
40+
41+
'''
42+
43+
notes = text.split(note_sep)
44+
45+
note_re = re.compile(r'\n'.join([
46+
r'(?P<title>.+)'
47+
, r'((?P<type>Lesezeichen|Markierung|Notiz)[  ]+auf Seite[  ]+(?P<page>\d+): )'
48+
#note is optional
49+
+ r'''((?P<note>(?:.|\n)*)
50+
)?''' + r'"(?P<quote>(?:.|\n)*)"'
51+
, r'''Hinzugefügt am (?P<day>\d{2}).(?P<month>\d{2}).(?P<year>\d{4}) \| (?P<hour>\d{1,2}):(?P<minute>\d{2})
52+
''']))
53+
54+
types = dict(
55+
Markierung='highlighted'
56+
, Notiz='note'
57+
, Lesezeichen='marker'
58+
)
59+
60+
dont_export = ['marker']
61+
62+
for note in notes:
63+
if not note:
64+
continue
65+
#print('---------"' + note + '"')
66+
m = note_re.match(note)
67+
68+
assert(m)
69+
70+
d = {}
71+
for k in ('year', 'month', 'day', 'hour', 'minute'):
72+
d[k] = int(m.group(k))
73+
74+
created = datetime(**d)
75+
76+
if created < last_export:
77+
continue
78+
79+
book = m.group('title')
80+
page = m.group('page')
81+
typ = types[m.group('type')]
82+
if typ in dont_export:
83+
continue
84+
quote = m.group('quote')
85+
86+
title = quote
87+
title = org.wrap(title, '"')
88+
if typ == 'note':
89+
title = m.group('note')
90+
91+
title.replace('\n', ' ')
92+
n = 20
93+
content = ''
94+
title_words = title.split()
95+
if len(title_words) > n:
96+
#content += '[…] ' + org.wrap(' '.join(title_words[n:])) + '\n\n'
97+
title = ' '.join(title_words[:n])
98+
title += ' […]'
99+
100+
101+
content += '\n#+begin_quote\n' + quote + '\n#+end_quote\n'
102+
103+
content += '\nFrom "' + book + '"' + ', p.' + page + '\n'
104+
105+
s = org.headline(title, content, created, tags=[typ])
106+
print(s)
107+
print('\n')
108+
109+
last_export_lines.append(datetime.now().strftime(org.date_format))
110+
with open(state_file, 'w+') as f:
111+
f.write('\n'.join(last_export_lines))
112+
113+
if __name__ == '__main__':
114+
main()

org.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
date_format = '%Y-%m-%d %a %H:%M'
2+
3+
def drawer_keyword(name):
4+
return wrap(name.upper(), ':')
5+
6+
def drawer_value(key, value):
7+
return drawer_keyword(key) + ' ' + value
8+
9+
def drawer(name, entries):
10+
return '''%s
11+
%s
12+
:END:
13+
''' % (drawer_keyword(name), '\n'.join([drawer_value(k, v) for k,v in entries]))
14+
15+
def date(date):
16+
return date.strftime(date_format)
17+
#return '2016-11-24 Thu 11:10'
18+
19+
def wrap(s, c='"'):
20+
return c + s + c
21+
22+
def paren(x, s='(', e=')'):
23+
return s + x + e
24+
25+
def brackets(x):
26+
return paren(x, '[', ']')
27+
28+
def inactive_date(date):
29+
return brackets(date)
30+
31+
def state_change(state, time):
32+
return '- %s %s' % (state, inactive_date(date(time)))
33+
34+
def headline(title, content, created=None, todo='', state_changes=[], properties=None, tags=[], indent=1):
35+
s = indent * '*' + ' '
36+
if todo:
37+
s += todo + ' '
38+
s += title
39+
if tags:
40+
s += ' ' + wrap(':'.join(tags), ':')
41+
s += '\n'
42+
if not properties:
43+
properties = []
44+
if created:
45+
properties.append(('created', inactive_date(date(created))))
46+
s += drawer('properties', properties) + '\n'
47+
s += content
48+
if state_changes:
49+
s += '\n\n:LOGBOOK:\n'
50+
s += '\n'.join([state_change(s, t) for s, t in state_changes])
51+
s += '\n:END:'
52+
53+
return s
54+

0 commit comments

Comments
 (0)