-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgk2qp.py
executable file
·216 lines (155 loc) · 4.88 KB
/
gk2qp.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
#!/usr/bin/python3
import argparse
import json
from pathlib import Path
import re
import shutil
import sys
import tempfile
colors = {
'GREEN': 'Green',
'PINK': 'Pink',
'BLUE': 'Blue',
'RED': 'Red',
'ORANGE': 'Orange',
'YELLOW': 'Yellow',
}
extra_colors = {
'CERULEAN': 'Blue',
# 'GRAY': 'Default',
'BROWN': 'Orange',
'PURPLE': 'Pink',
'TEAL': 'Blue',
}
dtre = re.compile(r'^\d\d\d\d-\d\d-\d\dT\d\d_\d\d_\d\d.\d\d\d\+\d\d_\d\d$')
def is_untitled(gk_name):
return dtre.match(gk_name) is not None
def convert_note(id, filepath):
with open(filepath) as f:
gk = json.load(f)
qp = {
'id': id,
'creationDate': gk['createdTimestampUsec'] // 1_000_000,
'modifiedDate': gk['userEditedTimestampUsec'] // 1_000_000,
'notebookId': 1,
}
if not is_untitled(filepath.stem):
qp['title'] = filepath.stem
if gk['isArchived']:
qp['isArchived'] = True
if gk['isPinned']:
qp['isPinned'] = True
if gk['isTrashed']:
qp['isDeleted'] = True
if gk['color'] in colors:
qp['color'] = colors[gk['color']]
if 'textContent' in gk:
qp['content'] = gk['textContent']
if 'listContent' in gk:
qp['isList'] = True
qp['taskList'] = []
for j, gkt in enumerate(gk['listContent']):
qpt = {
'id': j,
'isDone': gkt['isChecked'],
'content': gkt['text'],
}
qp['taskList'].append(qpt)
tags = gk.get('labels', [])
attachments = []
if 'attachments' in gk:
qp['attachments'] = []
for gka in gk['attachments']:
qpa = {
'description': gka['filePath'],
'fileName': gka['filePath'],
}
qp['attachments'].append(qpa)
attachments.append(gka['filePath'])
return qp, tags, attachments
def convert_notes(filepaths, tags):
qp_notes = {
'version': 26,
'notes': [],
'notebooks': [
{
'name': "Imported from Google Keep",
'id': 1,
}
],
'tags': tags,
'joins': [],
}
attachments = []
for i, filepath in enumerate(filepaths, start=1):
qp_note, note_tags, note_attachments = convert_note(i, filepath)
qp_notes['notes'].append(qp_note)
join_entries = create_join_entries(i, note_tags, tags)
qp_notes['joins'].extend(join_entries)
attachments.extend(note_attachments)
return qp_notes, attachments
def tag_id_from_name(tags, name):
for tag in tags:
if tag['name'] == name:
return tag['id']
print(f'Failed to find tag: {name}')
sys.exit(-1)
return 0
def create_join_entries(note_id, note_tags, tags):
entries = []
for note_tag in note_tags:
entry = {
'tagId': tag_id_from_name(tags, note_tag['name']),
'noteId': note_id,
}
entries.append(entry)
return entries
def extract_tags(filepath):
if not filepath.is_file():
return []
tags = []
with open(filepath) as f:
for i, line in enumerate(f.readlines(), start=1):
tag = {
'id': i,
'name': line.strip(),
}
tags.append(tag)
return tags
def main(args):
src = Path(args.takeout)
dst = f'quillpad-{src.stem}'
srcdir = Path(tempfile.mkdtemp())
dstdir = Path(tempfile.mkdtemp())
shutil.unpack_archive(src, srcdir)
keepdir = srcdir / 'Takeout' / 'Keep'
if args.use_extra_colors:
colors.update(extra_colors)
tags = extract_tags(keepdir / 'Labels.txt')
qp_notes, attachments = convert_notes(keepdir.glob('*.json'), tags)
print(f'Converted {len(qp_notes['notes'])} notes with {len(tags)} tags and {len(attachments)} attachments')
with open(dstdir / 'backup.json', 'w') as f:
f.write(json.dumps(qp_notes))
mediadir = dstdir / 'media'
mediadir.mkdir()
for attachment in attachments:
shutil.copy(keepdir / attachment, mediadir)
archive = shutil.make_archive(dst, 'zip', root_dir=dstdir)
shutil.rmtree(srcdir)
shutil.rmtree(dstdir)
print(f'Created Quillpad backup: {archive}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='gk2gp',
description='Converts Google Keep notes to Quillpad notes',
)
parser.add_argument('takeout',
help='Path to takeout archive containing Google Keep notes',
)
parser.add_argument('-c', '--disable-extra-colors',
dest='use_extra_colors',
action='store_false',
help='Map colors in Google Keep not supported in Quillpad to colorless, otherwise use the closest match',
)
args = parser.parse_args()
main(args)