This repository was archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEvernoteClient.lua
216 lines (185 loc) · 5.87 KB
/
EvernoteClient.lua
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
require "thrift.TBinaryProtocol"
require "thrift.THttpClient"
require "evernote.userstore.UserStore"
require "evernote.notestore.NoteStore"
--local inspect = require("inspect")
local dtd = '<?xml version="1.0" encoding="UTF-8"?>' ..
'<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
EvernoteClient = __TObject:new{
__type = "EvernoteClient",
domain,
authToken,
userStore,
noteStore,
}
function EvernoteClient:getUserStore()
if self.userStore then return self.userStore end
local config = require('EvernoteConfig')
if self.domain then
self.userStoreUri = config["USER_STORE_URL_"..self.domain:upper()]
else
self.userStoreUri = config.USER_STORE_URL
end
local userStoreHttpClient = THttpClient:new{ uri = self.userStoreUri }
local userStoreProtocol = TBinaryProtocol:new{ trans = userStoreHttpClient }
self.userStore = UserStoreClient:new{ iprot = userStoreProtocol }
self:checkVersion()
return self.userStore
end
function EvernoteClient:getNoteStore()
if self.noteStore then return self.noteStore end
self.noteStoreUri = self:getUserStore():getNoteStoreUrl(self.authToken)
local noteStoreHttpClient = THttpClient:new{ uri = self.noteStoreUri }
local noteStoreProtocol = TBinaryProtocol:new{ trans = noteStoreHttpClient }
self.noteStore = NoteStoreClient:new{ iprot = noteStoreProtocol }
return self.noteStore
end
function EvernoteClient:checkVersion()
local versionOK = self:getUserStore():checkVersion("Lua EMDATest",
EDAM_VERSION_MAJOR, EDAM_VERSION_MINOR
)
if not versionOK then
error("Old EDAM version")
end
end
function EvernoteClient:getUserInfo()
return self:getUserStore():getUser(self.authToken)
end
function EvernoteClient:findNotebooks()
return self:getNoteStore():listNotebooks(self.authToken)
end
--[[
-- find notebook by title in all notebooks
-- return notebook guid if found, otherwise return nil
--]]
function EvernoteClient:findNotebookByTitle(title)
local notebooks = self:findNotebooks()
for _,notebook in ipairs(notebooks) do
if notebook.name == title then return notebook.guid end
end
end
function EvernoteClient:createNotebook(name)
local notebook = Notebook:new{ name = name }
return self:getNoteStore():createNotebook(self.authToken, notebook)
end
function EvernoteClient:updateNotebook(guid, name)
local notebook = Notebook:new{
name = name,
guid = guid
}
return self:getNoteStore():updateNotebook(self.authToken, notebook)
end
function EvernoteClient:removeNotebook(guid)
return self:getNoteStore():expungeNotebook(self.authToken, guid)
end
function EvernoteClient:findNotes(keywords, count, createOrder, offset)
local noteFilter = NoteFilter:new{
order = createOrder and NoteSortOrder.CREATED or NoteSortOrder.RELEVANCE,
words = keywords or '',
}
return self:getNoteStore():findNotes(self.authToken, noteFilter, offset, count)
end
function EvernoteClient:loadNoteContent(note)
note.content = self:getNoteStore():getNoteContent(self.authToken, note.guid)
if note.tagGuids and not note.tagNames then
note.tagNames = {}
for i=1, #note.tagGuids do
local tag = self:getNoteStore():getTag(self.authToken, note.tagGuids[i])
table.insert(note.tagNames, tag.name)
end
end
end
--[[
-- find note by title in notebook
-- return note guid if found, otherwise return nil
--]]
function EvernoteClient:findNoteByTitle(title, notebook)
local spec = NotesMetadataResultSpec:new{ includeTitle = true }
local filter = NoteFilter:new{
order = NoteSortOrder.UPDATED,
notebookGuid = notebook
}
local index = 0
while true do
local metadata = self:getNoteStore():findNotesMetadata(self.authToken,
filter, index, 20, spec)
for _,note in ipairs(metadata.notes) do
if note.title == title then return note.guid end
end
if metadata.totalNotes <= metadata.startIndex + #metadata.notes then
break
end
index = index + #metadata.notes
end
end
local function enmlify(content)
return dtd..'<en-note>'..content..'</en-note>'
end
local function hextobin(s)
return (s:gsub('(%x%x)', function(hex)
return string.char(tonumber(hex, 16))
end))
end
local function createImageResources(resources)
local res = {}
for _, resource in ipairs(resources or {}) do
local image = resource.image
if image then
local data = Data:new{
size = #image.png,
bodyhash = hextobin(image.hash),
body = image.png
}
local resource = Resource:new{
mime = "image/png",
data = data
}
table.insert(res, resource)
end
end
return res
end
function EvernoteClient:createNote(title, content, resources, tags, notebook, created)
local note = Note:new{
title = title,
content = enmlify(content),
resources = createImageResources(resources),
created = created,
tagNames = tags,
notebookGuid = notebook,
}
return self:getNoteStore():createNote(self.authToken, note)
end
function EvernoteClient:updateNote(guid, title, content, resources, tags, notebook)
local note = Note:new{
guid = guid,
title = title,
content = enmlify(content),
resources = createImageResources(resources),
tagNames = tags,
notebookGuid = notebook,
}
return self:getNoteStore():updateNote(self.authToken, note)
end
function EvernoteClient:removeNote(guid)
self:getNoteStore():deleteNote(self.authToken, guid)
return true
end
function EvernoteClient:findTags()
return self:getNoteStore():listTags(self.authToken)
end
function EvernoteClient:createTag(name)
local tag = Tag:new{ name = name }
return self:getNoteStore():createTag(self.authToken, tag)
end
function EvernoteClient:updateTag(guid, name)
local tag = Tag:new{
name = name,
guid = guid
}
return self:getNoteStore():updateTag(self.authToken, tag)
end
function EvernoteClient:removeTag(guid)
return self:getNoteStore():expungeTag(self.authToken, guid)
end
return EvernoteClient