-
Notifications
You must be signed in to change notification settings - Fork 0
/
koreader-to-markdown.py
240 lines (182 loc) · 6.51 KB
/
koreader-to-markdown.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python
from datetime import datetime
import os
from pathlib import Path
import re
import warnings
import dotenv
import inquirer
import paramiko
from slpp import slpp as lua
def get_ssh(host, user, passphrase=None):
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(
hostname=host,
username=user,
password='',
passphrase=passphrase
)
return ssh
def exec_command(ssh, command):
stdin, stdout, stderr = ssh.exec_command(command)
stdout_read = stdout.read()
stderr_read = stderr.read()
if len(stderr_read) > 0:
raise Exception(stderr_read)
return stdout_read
def get_sidecar_paths(ssh, start_from):
sidecar_paths = []
if type(start_from) == str:
start_from = [start_from]
for start in start_from:
sidecar_stdout = exec_command(
ssh, 'find "{}" -type d -name "*.sdr"'.format(start)
)
sidecar_paths = sidecar_paths + sidecar_stdout.decode().split('\n')
return sidecar_paths
def get_sidecar_lua_path(ssh, sidecar_path):
sidecar_lua_stdout = exec_command(
ssh, 'find "{}" -type f -name "metadata.*.lua"'.format(sidecar_path)
)
sidecar_lua_paths = sidecar_lua_stdout.decode().split('\n')
return sidecar_lua_paths[0]
def get_sidecar_contents(ssh, sidecar_path):
sidecar_lua_path = get_sidecar_lua_path(ssh, sidecar_path)
sidecar_contents = get_file_contents(ssh, sidecar_lua_path)
return sidecar_contents
def get_file_contents(ssh, file_path):
sftp = ssh.open_sftp()
with sftp.open(file_path) as file_open:
file_contents = file_open.read()
return file_contents
def parse_bookmark(bookmark):
"""Different file formats have different bookmark information. Here I try to
standardize it into something I would want to have in Obsidian
:param bookmark:
:return:
"""
output = ''
if 'text' in bookmark:
text = re.sub(r'^Page \d+ ', '', bookmark.pop('text'), count=1)
text = text.rsplit(' @ ', 1)[0]
text = '- {}'.format(text)
elif 'notes' in bookmark:
text = '- {}'.format(bookmark.pop('notes'))
else:
return None
output += '\n - '.join(text.split('\\\n'))
output += ' <!-- datetime: {} -->\n'.format(bookmark['datetime'])
return output
def write_markdown(output_path, authors, title, bookmarks):
output_path.mkdir(parents=True, exist_ok=True)
if len(bookmarks) < 1:
warnings.warn('len(bookmarks) < 1')
return
# Get start and end dates from the bookmarks
bookmark_dates = [b['datetime'] for b in bookmarks]
start = datetime.strptime(
min(bookmark_dates), '%Y-%m-%d %H:%M:%S'
) if len(bookmark_dates) > 0 else None
end = datetime.strptime(
max(bookmark_dates), '%Y-%m-%d %H:%M:%S'
) if len(bookmark_dates) > 1 else None
prefix = ''
if end is not None:
prefix = end.strftime('%Y%m%d - ')
elif start is not None:
prefix = start.strftime('%Y%m%d - ')
# Get current datetime
modified = datetime.now().isoformat()
# Construct the output file path and empty it
md_file = output_path / '{}{}.md'.format(prefix, title)
md_file.write_text('')
# Add frontmatter and headers
with md_file.open(mode='a') as md:
md.write('---\n')
if start is not None:
md.write('created: {}\n'.format(start.isoformat()))
md.write(
'modified: {2}\n\n'
'---\n\n'
'# {0}\n\n'
'- Van [[{1}]]\n'.format(
title,
authors,
modified
)
)
if start is not None:
md.write(
'- Eerste highlight gezet op {}\n'
''.format(start.strftime('[[DAGBOEK/%Y/%Y-%m/%Y-%m-%d]]'))
)
if end is not None:
md.write(
'- Laatste highlight gezet op {}'
'\n'.format(end.strftime('[[DAGBOEK/%Y/%Y-%m/%Y-%m-%d]]'))
)
md.write('\n## Highlights\n')
# Write the actual bookmarks, per chapter
current_chapter = None
for bookmark in bookmarks:
bookmark_chapter = bookmark.pop('chapter')
if bookmark_chapter != current_chapter:
current_chapter = bookmark_chapter
md.write('\n### {}\n\n'.format(current_chapter))
md.write(parse_bookmark(bookmark))
def sort_bookmarks(bookmarks_dict):
def sort_key(item):
if type(item['page']) == int:
return [item['page']]
return [int(i) for i in re.findall(r'\[(\d+)\]', item['page'])]
bookmarks_list = [b for b in bookmarks_dict.values()]
bookmarks_list = sorted(bookmarks_list, key=sort_key)
return bookmarks_list
def main():
try:
ssh = get_ssh(
os.environ['SSH_HOST'],
os.environ['SSH_USER']
)
except paramiko.ssh_exception.PasswordRequiredException as e:
warnings.warn('{}: {}'.format(e.__class__.__name__, e.args[0]))
passphrase = inquirer.password("Enter your private key's passphrase:")
ssh = get_ssh(
os.environ['SSH_HOST'],
os.environ['SSH_USER'],
passphrase
)
sidecar_paths = get_sidecar_paths(
ssh, [
'/mnt/onboard/.adds/koreader/articles/',
'/mnt/onboard/.adds/koreader/books/',
'/mnt/onboard/.adds/koreader/comics/'
]
)
for sidecar_path in sidecar_paths:
if len(sidecar_path) == 0:
continue
print(sidecar_path)
try:
sidecar_contents = get_sidecar_contents(ssh, sidecar_path).decode()
# Clean that sidecar str, then decode into a dict
sidecar_contents = re.sub('^[^{]*', '', sidecar_contents).strip()
sidecar_lua = lua.decode(sidecar_contents)
# Get book authors, title and dates
authors = sidecar_lua['doc_props']['authors']
title = sidecar_lua['doc_props']['title']
# Sort bookmarks based on `page`
sorted_bookmarks = sort_bookmarks(sidecar_lua['bookmarks'])
# Write the bookmarks to the `output` folder
write_markdown(
Path('output').resolve(),
authors,
title,
sorted_bookmarks
)
except KeyError as e:
warnings.warn('{}: {}'.format(e.__class__.__name__, e.args[0]))
if __name__ == '__main__':
dotenv.load_dotenv(override=True)
main()