-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportnote.py
561 lines (455 loc) · 19.3 KB
/
exportnote.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/bin/python
# -*- coding: utf-8 -*-
__name__ = "NotePublisher"
import os
import helpers
import datetime
import bs4 as BeautifulSoup
import mimetypes
from base64 import b64encode
try:
from html import escape # python 3.x
except ImportError:
from cgi import escape # python 2.x
import hashlib
def scrubPathPart(pathpart, lowercase=False):
assert (pathpart)
for c in r'[]/\;,><&*:%=+@!#^()|?^':
pathpart = pathpart.replace(c, u'')
pathpart = pathpart.replace(u' ', u'_')
if lowercase:
pathpart = pathpart.lower()
return pathpart
PAGE_BASE_LAYOUT = """
<HTML>
<HEAD>
<!-- exported by Note Publisher - https://github.com/selner/NotePublisher --></BODY>
<TITLE></TITLE>
<STYLE>
body {
/* Font & Text */
font-family: Times;
font-size: 16px;
font-style: normal;
font-variant: normal;
font-weight: normal;
letter-spacing: normal;
line-height: 16px;
text-decoration: none;
text-align: start;
text-indent: 0px;
text-transform: none;
vertical-align: baseline;
white-space: normal;
word-spacing: 0px;
/* Color & Background */
background-attachment: scroll;
background-color: rgb(255, 255, 255);
background-image: none;
background-position: 0% 0%;
background-repeat: repeat;
color: rgb(0, 0, 0);
}
h1 {
font-family: caecilia, times, serif;
font-size: 28px;
font-stretch: normal;
font-style: normal;
font-variant: normal;
font-weight: 300;
line-height: 32px;
vertical-align: baseline;
color: #2DBE60;
}
#note-content {
color: #383838;
font-family: gotham, helvetica, arial, sans-serif;
font-size: 14px;
-webkit-font-smoothing: antialiased;
}
.document-heading{
color: #4a4a4a;
font-family: gotham, helvetica, arial, sans-serif;
font-size: 14px;
-webkit-font-smoothing: antialiased;
}
.metafact {
white-space: no-wrap;
display: inline-flex;
margin-right: 15px;
}
.metafact .heading {
margin-right: 8px;
color: #878787;
-webkit-font-smoothing: antialiased;
font-family: gotham, helvetica, arial, sans-serif;
font-size: 11px;
font-weight: 400;
text-transform: uppercase;
letter-spacing: 1px;
}
.metafact .value {
color: #4a4a4a;
-webkit-font-smoothing: antialiased;
font-family: gotham, helvetica, arial, sans-serif;
font-size: 13px;
font-weight: 400;
-webkit-font-smoothing: antialiased;
font-family: gotham, helvetica, arial, sans-serif;
font-size: 11px;
font-weight: 400;
text-transform: uppercase;
letter-spacing: 1px;
}
body {
padding: 3em;
}
.document-item {
display: block;
margin: 1.5em 0 2.5em 0;
position: relative;
padding-left: 45px;
color: #4a4a4a;
}
.document-item::before {
position: absolute;
width: 29px;
height: 34px;
left: 0;
top: -7px;
content: '';
border: solid 1px #878787;
}
.document-item::after {
content: 'file';
content: attr(filetype);
left: -4px;
padding: 0px 2px;
text-align: right;
line-height: 1.3;
position: absolute;
background-color: #28a956;
color: #fff;
font-size: 11px;
text-transform: uppercase;
letter-spacing: 1px;
top: 10px;
}
.document-item .fileCorner {
width: 0;
height: 0;
border-style: solid;
border-width: 12px 0 0 11px;
border-color: white transparent transparent #878787;
position: absolute;
top: -8px;
left: 22px;
}
</STYLE>
</HEAD>
<BODY>
<H1 id="note-heading" style="margin-bottom: 5px;"></H1>
<HR/><DIV id="note-content">%s</DIV>
</HTML>
"""
import requests
def getDirectory(dir):
if not os.path.exists(dir):
os.makedirs(dir)
return dir
def _getNotebookExportDirectory(output_folder, notebook, typeSubfolder = ""):
if typeSubfolder:
output_folder = os.path.join(output_folder, typeSubfolder)
notebookDirectory = output_folder
if notebook.stack:
notebookstack = scrubPathPart(notebook.stack, lowercase=False)
notebookDirectory = os.path.join(notebookDirectory, notebookstack)
notebookname = scrubPathPart(notebook.name, lowercase=False)
notebookDirectory = os.path.join(notebookDirectory, notebookname)
return getDirectory(notebookDirectory)
class NoteExport(object):
_note = None
_notebook = None
_client = None
def __init__(self, client, outputFolder=None, resourceUriPrefix=None, noteMetadata=None, note=None, notebook=None,
tags=None, formats=[]):
self._client = client
self.resourceUriPrefix = resourceUriPrefix
self._note = note
self.oauth_token = client.token
self.noteMetadata = noteMetadata
self._notebook = notebook
self.tags = tags
self.output_folder = outputFolder
self.note_resources_by_hash = {}
self.formats = formats
@property
def client(self):
return self._client
@property
def notestore(self):
return self._client.note_store
@property
def notebook(self):
return self._notebook
@property
def title(self):
return self.noteMetadata.title
@property
def filename(self):
return scrubPathPart(self.noteMetadata.title, lowercase=True)
@property
def note(self):
if not self._note and self.noteMetadata:
try:
# self.note = self.note_store.getNote(guid=noteMetadata.guid, withContent=1, withResourcesData=1, withResourcesRecognition=0, withResourcesAlternateData=0)
self._note = self.client.note_store.getNote(self.oauth_token, self.noteMetadata.guid, True, True, False, False)
except Exception as e:
helpers.reRaiseException(f'!!!!!! ERROR: Failed to export note {self.noteMetadata.title} due to error: {e}')
return self._note
@note.setter
def note(self, value):
self._note = value
def _getFilePathForResource(self, resource, output_parent_folder):
# To display the Resource as part of the note's content, include an <en-media>
# tag in the note's ENML content. The en-media tag identifies the corresponding
# Resource using the MD5 hash.
if "fileName" in resource.attributes.__dict__ and resource.attributes.__dict__["fileName"]:
filename = resource.attributes.__dict__["fileName"]
else:
fileext = mimetypes.guess_extension(resource.mime, strict=False)
if fileext is None:
fileext = "." + resource.mime.split("/")[1]
if fileext.startswith(u'.jpe'):
fileext = ".jpg"
filename = "%s%s" % (resource.guid, fileext)
noteResourceDir = getDirectory(os.path.join(output_parent_folder, (self.filename + "-resources/")))
file_path = os.path.join(noteResourceDir, filename)
if len(file_path) > os.pathconf(self.output_folder, "PC_NAME_MAX"):
file_path = os.path.join(noteResourceDir,
os.path.splitext(filename)[0][0:8] + os.path.splitext(filename)[1])
return file_path
def export(self):
try:
output_folder = _getNotebookExportDirectory(self.output_folder, self.notebook, "enex")
if self.formats == [] or "enex" in self.formats:
self._export_enex(self.note)
if self.formats == [] or "html" in self.formats:
self.xmltransform()
self._export_html(self.note)
except Exception as e:
print(f'Unable to save note {self.note.title} due to error: {e}')
pass
def _addAttributeDivTag(self, soup, parentTag, heading, value):
newdiv = soup.new_tag("div")
newdiv["class"] = "metafact"
parentTag.append(newdiv)
newSpan = soup.new_tag("span")
newSpan["class"] = "metafact heading"
newSpan.string = heading
newdiv.append(newSpan)
newSpanVal = soup.new_tag("span")
newSpanVal["class"] = "metafact value"
newSpanVal.string = value
newdiv.append(newSpanVal)
return parentTag
def _insertMetadataFactsToHTML(self, soupNote):
attr = self.note.attributes.__dict__
if not soupNote.head:
soupNote.body.insert_before(soupNote.new_tag(u'head'))
parentDiv = soupNote.new_tag(u'div')
parentDiv.attrs["id"] = "note-metadata"
soupNote.body.append(parentDiv)
# strFactsHTML = ""
try:
for key in attr:
if attr[key] is not None and attr[key] != 0:
key = escape(key)
value = escape(attr[key])
metatag = soupNote.new_tag(u'meta')
metatag.attrs['content'] = value
metatag.attrs['name'] = key
soupNote.head.append(metatag)
parentDiv = self._addAttributeDivTag(soup=soupNote, parentTag=parentDiv, heading=key, value=value)
except Exception as e:
helpers.reRaiseException(
"!!!!!! ERROR: Failed to export metadata for note '$s' due to error: " % self.noteMetadata.title, e)
try:
tagNames = []
for t in self.note.tags:
tagNames.append(self.tags[t])
if tagNames and tagNames.count > 0:
parentDiv = self._addAttributeDivTag(soup=soupNote, parentTag=parentDiv, heading="Tags",
value=", ".join(tagNames))
except Exception:
pass
now = datetime.datetime.now()
nowstr = now.strftime("%x %X")
parentDiv = self._addAttributeDivTag(soup=soupNote, parentTag=parentDiv, heading="Exported", value=nowstr)
# divNote.insert(0, divAttr)
parentDiv = self._addAttributeDivTag(soup=soupNote, parentTag=parentDiv, heading="Notebook",
value=self.notebook.name)
# divNote.insert(0, divAttr)
# parentDiv.insert_before(soupNote.body)
#
soupNote.body.append(parentDiv)
return soupNote
def _wrap_outer_enex(self, title, body):
return f'<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-export SYSTEM \"http://xml.evernote.com/pub/evernote-export3.dtd\">\n<en-export export-date=\"20161017T021119Z\" application=\"NotePublisher\"><note>\n<title>{title}</title>\n<content><![CDATA[{body}]]></content>\n</note>\n</en-export>'
def _export_enex(self, note):
output_folder = _getNotebookExportDirectory(self.output_folder, self.notebook, "enex")
try:
enex_content = self.client.note_store.getNoteContent(self.oauth_token, self.noteMetadata.guid)
# get resources of the note
noteExport = self.note
enexContent = f'<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-export SYSTEM \"http://xml.evernote.com/pub/evernote-export3.dtd\">\n<en-export export-date=\"20161017T021119Z\" application=\"NotePublisher\">\n' \
'<note>\n<title>{self._note.title}</title>\n<content><![CDATA[{noteExport.content}]]></content>\n'
import re
if noteExport.resources:
for r in noteExport.resources:
resource = self.notestore.getResource(r.guid, True, False, True, False)
filepath = os.path.join(self.output_folder, f'resource_{r.guid}')
with open(filepath, 'wb') as out_file:
from shutil import copyfileobj
out_file.write(resource.data.body)
resbody = b64encode(resource.data.body)
xml = '\n<resource>\n<data encoding="base64">' + resbody.decode() + '</data>\n<mime>' + resource.mime + '</mime>\n</resource>'
enexContent = enexContent + xml
enexContent = enexContent + "\n</note>\n</en-export>"
outfile = helpers.save_text_file(path=output_folder, ext='enex', basename=self.filename,
textdata=enex_content, encoding='utf-8')
print("Published " + note.title + " to " + outfile)
except Exception as e:
helpers.reRaiseException(f'!!!!!! ERROR: Failed to export note \'{ note.title }\' due to error: {e}')
def _downloadResource(self, resource, filepath=None):
url = f'{self.resourceUriPrefix}/{resource.guid}'
if not filepath:
filepath = os.path.join(self.output_folder, f'resource{resource.guid}')
if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
try:
if resource.data.body:
data = resource.data.body
except:
res_url = self.resourceUriPrefix + resource.guid
try:
response = requests.post(url=res_url, auth=self.oauth_token)
data = response.content
except Exception as e:
print(e)
from helpers import reRaiseException
reRaiseException(e)
# print(f'!!!!! Error: Failed download resource from Evernote. URL: {res_url} Message: {e}')
response = requests.post(url=url, auth=self.oauth_token)
if response.code == 200:
data = response.content
with open(filepath, 'wb') as out_file:
from shutil import copyfileobj
copyfileobj(data, out_file)
else:
raise Exception(f'Unable to download {url} to {filepath}: {response}')
def _exportResourcesForHTML(self, note, output_parent_folder):
if note is None:
note = self.note
note_resources_by_hash = {}
try:
if note.resources and len(note.resources) > 0:
for i in range(0, len(note.resources)):
resource = note.resources[i]
data = None
try:
if resource.data.body:
data = resource.data.body
except:
res_url = self.resourceUriPrefix + resource.guid
try:
response = requests.post(url=res_url, auth=self.oauth_token)
data = response.content
except Exception as e:
print (f'!!!!! Error: Failed download resource from Evernote. URL: {res_url} Message: {e}')
continue
# calculate hashcode for media
md5 = hashlib.md5()
md5.update(data)
hashcode = md5.hexdigest()
file_path = self._getFilePathForResource(resource, output_parent_folder)
f = open(file_path, "w")
f.write(data)
f.close()
note_resources_by_hash[hashcode] = resource
return note_resources_by_hash
except Exception as e:
helpers.reRaiseException("Failed to export resource files for note %s. Error: " % self.note.title, e)
def _export_html(self, note):
try:
output_folder = _getNotebookExportDirectory(self.output_folder, self.notebook, "html")
resourcesByHash = {}
if note.resources:
try:
resourcesByHash = self._exportResourcesForHTML(note, output_folder)
except Exception as e:
helpers.reRaiseException("Unable to export resources for note due to error:", e)
soupExport = BeautifulSoup.BeautifulSoup(PAGE_BASE_LAYOUT, "lxml")
soupExport.title.string = self.noteMetadata.title
soupExport.h1.string = self.noteMetadata.title
try:
soupNoteHTML = BeautifulSoup.BeautifulSoup(note.content, "lxml")
soupNoteHTML = self._insertMetadataFactsToHTML(soupNote=soupNoteHTML)
# resources = set(self.note.resources.all())
# print unicode(soupNoteHTML)
# for mediatag in soupNoteHTML.findAll('en-media'):
# for res in resources:
# if res.hash == mediatag['hash']:
# mime_type = mediatag['type']
# if mime_type.startswith('image/'):
# img = BeautifulSoup.Tag(soupNoteHTML, 'img')
# img['height'] = mediatag.get('height', 450)
# img['width'] = mediatag.get('width', 600)
# img['src'] = res.file.url
# mediatag.replaceWith(img)
#
all_media = soupNoteHTML.find_all(u'en-media')
for mediatag in all_media:
media_type = mediatag['type'].split("/")[0]
if media_type.lower() == "image":
new_tag = soupNoteHTML.new_tag(u'img')
new_tag['src'] = self._getFilePathForResource(resourcesByHash[mediatag["hash"]], output_folder)
mediatag.replace_with(new_tag)
# elif media_type.lower() != "image":
# new_tag = soupNoteHTML.new_tag(u'img')
# new_tag['src'] = self._getFilePathForResource(resourcesByHash[mediatag["hash"]])
# mediatag.replace_with(new_tag)
#
# if media_type.lower() != "image":
else:
new_tag = soupNoteHTML.new_tag(u'div')
new_tag['class'] = "document-heading"
new_tag.string = "Attached document: "
link_tag = soupNoteHTML.new_tag(u'a')
filepath = self._getFilePathForResource(resourcesByHash[mediatag["hash"]], output_folder)
link_tag['href'] = filepath
link_tag['class'] = "document-item"
link_tag['filetype'] = os.path.splitext(filepath)[1]
childspan = soupNoteHTML.new_tag(u'span')
childspan['class'] = "fileCorner"
link_tag.string = "%s" % os.path.basename(filepath)
link_tag.insert(0, new_child=childspan)
new_tag.insert(1, new_child=link_tag)
mediatag.replace_with(new_tag)
tagContent = soupExport.find(id="note-content")
import copy
tagContent.replace_with(copy.copy(soupNoteHTML.body))
try:
exportHTML = soupExport.prettify(formatter="html").encode(u'utf-8')
outfile = helpers.export_html_file(path=output_folder, basename=self.filename,
html=exportHTML, encoding='utf-8')
print("Published " + self.note.title + " to " + outfile)
except UnicodeEncodeError as e:
helpers.reRaiseException("!!!!!! ERROR: Failed to export note '%s' due to error: " % self.noteMetadata.title, e)
except KeyError as e:
helpers.reRaiseException(
"!!!!!! ERROR: Unable to find attachment %s to export for note:" % self.noteMetadata.title, e)
pass
except Exception as e:
helpers.reRaiseException(
"!!!!!! ERROR: Failed to export note '%s' due to error: " % self.noteMetadata.title, e)