-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportnotebook.py
96 lines (76 loc) · 4.19 KB
/
exportnotebook.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
#!/bin/python
# -*- coding: utf-8 -*-
__name__ = "NotePublisher"
import evernote.edam.notestore.NoteStore as NoteStore
import evernote.edam.error.ttypes as EvernoteTypes
from session import EvernoteSession
import helpers
from exportnote import NoteExport
try:
from html import escape # python 3.x
except ImportError:
from cgi import escape # python 2.x
class NotebooksExport(object):
def __init__(self, arguments=None):
self.arguments = arguments
self.client = EvernoteSession()
self.client.connect(**arguments)
# self.note_store = self.client.note_store
# self.oauth_token = getLastOAuthToken()
# self.user = self.client.get_user_store().getUser()
# self.userPublicInfo = self.client.get_user_store().getPublicUserInfo(self.user.username)
user = self.client.user_store.getPublicUserInfo(self.client.user.username)
self.resourceUriPrefix = user.webApiUrlPrefix + "res"
def filter_noteboooks_by_fact(self, fact, matchString):
print ("Filtering notebooks to %s matching '%s'..." % (fact, matchString))
matched = {getattr(n, fact): n for n in self.client.notebooks.values() if getattr(n, fact) and matchString in getattr(n, fact) }
return matched
def exportSearch(self):
try:
notebooks = {}
if "--matchstack" in self.arguments and self.arguments["--matchstack"] is not None:
notebooks = self.filter_noteboooks_by_fact('stack', self.arguments["--matchstack"])
if "--matchnotebook" in self.arguments and self.arguments["--matchnotebook"] is not None:
notebooks = self.filter_noteboooks_by_fact('name', self.arguments["--matchnotebook"])
for key in notebooks:
notebook = notebooks[key]
filter = NoteStore.NoteFilter()
if notebook is not None:
filter.notebookGuid = notebook.guid
else:
filter.notebookGuid = self.client.default_notebook.guid
# offset = 0;
# pageSize = 10;
# notes = [];
# resultSpec = { "includeTitle": True, "includeNotebookGuid":True, "includeAttributes":True, "includeContentLength" :True }
allNoteResults = self.client.note_store.findNotes(self.client.token, filter, 0, 9999)
for noteMeta in allNoteResults.notes:
noteexp = NoteExport(client=self.client, outputFolder=self.arguments["output"], noteMetadata=noteMeta, notebook=notebook, tags=self.client.tags, resourceUriPrefix=self.resourceUriPrefix)
noteexp.export()
# allNoteResults = self.note_store.findNotesMetadata(self.client.token, filter, offset, pageSize, spec = resultSpec)
# remaining = allNoteResults.totalNotes()
# while (remaining < allNoteResults.getTotalNotes()):
# nextNotes = allNoteResults.getNotes()
#
# for noteMeta in nextNotes:
# html = None
# noteexp = NoteExport(client=self.client, user=self.user, noteMetadata=noteMeta)
#
#
# remaining = allNoteResults.totalNotes - (allNoteResults.startIndex + notes.length)
# offset = offset + allNoteResults.getNotesSize()
except EvernoteTypes.EDAMSystemException as e:
if e.errorCode == EvernoteTypes.EDAMErrorCode.RATE_LIMIT_REACHED:
print ("Rate limit reached. Retrying the request in %d seconds." % e.rateLimitDuration)
import time
time.sleep(int(e.rateLimitDuration) + 1)
self.exportSearch()
else:
print ("!!!!! Error: Failed to access note via Evernote API. Message: %s" % e)
exit(-1)
except EvernoteTypes.EDAMUserException as e:
print ("Invalid Evernote API request. Please check the call parameters. Message: %s" % e)
exit(-1)
except Exception as e:
helpers.reRaiseException("!!!!! Error: Failed to access note via Evernote API. Message:", e)
exit(-1)