forked from rPawel/Tomboy2Evernote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskwarrior2Evernote.py
executable file
·94 lines (74 loc) · 2.78 KB
/
Taskwarrior2Evernote.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import re
import sys, getopt
import glob
import os
import json
def process_files(inputfile, outputdir):
enex_notes = []
output_filename = 'json2Evernote.enex'
with open(inputfile) as f:
data = json.load(f)
for entry in data:
print(entry);
title = entry["description"]
html_note_body = entry["description"]
created_date = entry["entry"]
updated_date = entry["modified"]
enex_notes.append(make_enex(title, html_note_body, created_date, updated_date))
multi_enex_body = make_multi_enex(enex_notes)
save_to_file(outputdir, output_filename, multi_enex_body)
print ("Evernote file location: " + outputdir + "/" + output_filename)
def make_enex(title, body, created_date, updated_date):
return '''<note><title>''' + title + '''</title><content><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
''' + body + '''
</en-note>]]></content><created>''' + created_date + '''</created><updated>''' + updated_date + '''</updated></note>'''
def make_multi_enex(multi_enex_body):
return '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export2.dtd">
<en-export export-date="20150412T153431Z" application="Evernote/Windows" version="5.x">
''' + ''.join(multi_enex_body) + '''</en-export>'''
def save_to_file(outputdir, filename, body):
if not os.path.exists(outputdir):
os.makedirs(outputdir)
text_file = open(outputdir + '/' + filename, "w")
text_file.write(body)
text_file.close()
def get_help_line():
print ('Usage: ', sys.argv[0], ' -i <inputfile> -o <outputdir>')
def get_input_params(argv):
inputfile = ''
outputdir = ''
printhelpline = 0
try:
opts, args = getopt.getopt(argv, "hi:o:", ["ifile=", "odir="])
except getopt.GetoptError:
exit_with_error()
for opt, arg in opts:
if opt == '-h':
get_help_line()
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--odir"):
outputdir = arg
if (inputfile == ""):
print ("Error: Missing input file")
printhelpline = 1
if (outputdir == ""):
print ("Error: Missing output folder")
printhelpline = 1
if printhelpline == 1:
exit_with_error()
return (inputfile, outputdir)
def exit_with_error():
get_help_line()
sys.exit(2)
def main(argv):
inputfile, outputdir = get_input_params(argv)
process_files(inputfile, outputdir)
if __name__ == "__main__":
main(sys.argv[1:])