-
Notifications
You must be signed in to change notification settings - Fork 2
/
export.py
110 lines (75 loc) · 3.03 KB
/
export.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
"""
Mon 13 Jul 2020 19:08:05 CEST
Export Google Keep notes to a markdown file.
After obtaining your Google Keep data from https://takeout.google.com/
unzip the folder and cd into it.
Copy this file in that folder and run:
python export.py > exported.md
or, if you prefer a simple txt file:
python export.py > exported.txt
"""
import os
import json
import datetime
import pathlib
PRINT_RAW = False
PRINT_PRETTY_JSON = True
PRINT_DATE = True
RIGHT_ALIGN_DATE = True
class Note:
def __init__(self, filename, data):
self._name = filename.replace('.json', '')
self._title = data['title']
self._raw_date = data['userEditedTimestampUsec']
self._date = datetime.datetime.fromtimestamp(self._raw_date/1_000_000)
self._isTrashed = data['isTrashed']
self._isArchived = data['isArchived']
self._isList = True if 'listContent' in data else False
if self._isList:
tmp = ""
for i in data['listContent']:
tick = 'x' if i['isChecked'] else ' '
tmp += "- [{}] {}\n".format(tick, i['text'])
self._content = tmp; del tmp
else:
self._content = data['textContent']
def _format_date(self):
# return a date of this type: Tuesday November 03, 2015, 03:20:51 PM
return self._date.strftime("%A %B %d, %Y, %I:%M:%S %p") # https://strftime.org/
def isTrashed(self):
return self._isTrashed
def __repr__(self):
ans = "#### {} {}\n".format(self._name, "(Archived)" if self._isArchived else "")
if PRINT_DATE:
if RIGHT_ALIGN_DATE:
ans += "<div style='text-align: right'>"
ans += "Last edited: {}\n".format(self._format_date())
if RIGHT_ALIGN_DATE:
ans += "\b</div>\n"
ans += "\n"
if self._title != '':
ans += "Title: {}\n".format(self._title)
ans += self._content
return ans
if __name__ == '__main__':
root_folder = pathlib.Path('Keep')
notes = [filename for filename in os.listdir(root_folder) if '.json' in filename]
notes_list = []
for filename in notes:
with open(pathlib.Path("{}/{}".format(root_folder, filename)), 'r') as json_file:
data = json.load(json_file)
if PRINT_RAW:
if PRINT_PRETTY_JSON:
print(json.dumps(data, indent=4, sort_keys=True)); print()
else:
print("{}\n\n".format(data))
note = Note(filename, data)
if not note.isTrashed():
notes_list.append(note)
del notes
#sorted_notes = sorted(notes_list, key=lambda x: x._raw_date, reverse=True)
sorted_notes = sorted(sorted(notes_list, key=lambda x : x._isArchived), key=lambda x : x._raw_date, reverse = True)
print("Total #notes = {} – sorted by (Last update, archived/not archived)\n\n".format(len(sorted_notes)))
for i in sorted_notes:
print("{}\n".format(i))
print("---\n\n")