-
Notifications
You must be signed in to change notification settings - Fork 0
/
Evernote.py
395 lines (311 loc) · 12.2 KB
/
Evernote.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2016-08-20 18:25:17
# @Author : Tom Hu ([email protected])
# @Link : http://h1994st.com
# @Version : 1.0
import re
import time
import pytz
import binascii
import datetime
from pprint import pprint
import evernote.edam.type.ttypes as Types
import evernote.edam.error.ttypes as Errors
from evernote.edam.notestore import NoteStore
from evernote.api.client import EvernoteClient
import Config
from timeout import timeout
from auto_flow_leaker.auto_flow.post import Post
from auto_flow_leaker.auto_flow.channel import Channel
TIMEOUT = int(Config.Global('timeout'))
class Evernote(EvernoteClient, Channel):
'''
Default user: ctom357 ([email protected])
'''
def __init__(self, **kwargs):
print 'Initializing Evernote/Yinxiang:'
kwargs.setdefault('service_host', Config.Evernote('host'))
print ' Service host: %s' % Config.Evernote('host')
kwargs.setdefault('token', Config.Evernote('token'))
print ' Access token: %s' % Config.Evernote('token')
kwargs.setdefault('sandbox', False)
print ' Sandbox: False'
super(Evernote, self).__init__(**kwargs)
self._note_store = self.get_note_store()
self.default_notebook = kwargs.pop(
'default_notebook', self.note_store.getDefaultNotebook(self.token))
print ' Default notebook: %s' % self.default_notebook.name
def description(self):
return 'username={!r}, email={!r}, server_host={!r}, token={!r}, sandbox={!r}'.format(
Config.Evernote('username'), Config.Evernote('email'),
Config.Evernote('host'), Config.Evernote('token'), False)
def send(self, content, title=None):
'''
Default title: unix epoch
'''
try:
note = self.create_note(
(title or '%.6f' % time.time()), content)
except Exception:
return None
else:
return Post(
id=note.guid,
title=note.title,
create_time=datetime.datetime.fromtimestamp(
note.created / 1000, pytz.utc))
def receive_all(self):
def converter(note):
raw_content = self.get_note(note.guid).content
text_content = re.search(
'<en-note>(?P<content>.*)</en-note>',
raw_content, flags=re.DOTALL)
if text_content:
text_content = text_content.groups('content')[0]
return Post(
id=note.guid,
title=note.title,
content=text_content,
create_time=datetime.datetime.fromtimestamp(
note.created / 1000, pytz.utc))
return sorted(
map(converter, self.get_notes()),
key=lambda x: x.create_time, reverse=True)
def delete(self, item):
self.delete_note(guid=item.id)
def delete_all(self):
self.delete_notes(notebook=self.default_notebook)
@property
def default_notebook(self):
'''
Default notebook for Evernote channel.
:rtype: evernote.edam.type.ttypes.Notebook
'''
if self._default_notebook_value is not None:
return self._default_notebook_value
else:
raise AttributeError('missing required field \'default_notebook\'')
@default_notebook.setter
def default_notebook(self, val):
if isinstance(val, str):
self._default_notebook_value = self.get_notebook(name=val)
elif isinstance(val, Types.Notebook):
self._default_notebook_value = val
else:
raise AttributeError('attribute \'default_notebook\' type error')
@default_notebook.deleter
def default_notebook(self):
self._default_folder_value = self.note_store.getDefaultNotebook(
self.token)
@property
def note_store(self):
return self._note_store
@property
def notebooks(self):
return self.get_notebooks(readable=True)
def _notebook_to_dict(self, notebook):
assert isinstance(notebook, Types.Notebook), notebook
ret = {
'guid': notebook.guid,
'name': notebook.name,
'default': notebook.defaultNotebook
}
return ret
def _note_to_dict(self, note):
assert isinstance(note, (Types.Note, NoteStore.NoteMetadata)), note
ret = {
'guid': note.guid,
'title': note.title,
'length': note.contentLength,
'created': note.created, # Unix Epoch
'notebook_guid': note.notebookGuid,
}
if isinstance(note, Types.Note):
ret['content'] = note.content
ret['resources'] = note.resources
return ret
@timeout(TIMEOUT)
def get_notebooks(self, readable=False):
assert isinstance(readable, bool)
notebooks = self.note_store.listNotebooks(self.token)
if readable:
return map(self._notebook_to_dict, notebooks)
return notebooks
@timeout(TIMEOUT)
def get_notebook(self, guid=None, name=None, readable=False):
assert guid is None or isinstance(guid, (str, unicode)), guid
assert name is None or isinstance(name, (str, unicode)), name
assert isinstance(readable, bool), readable
if name is None and guid is None:
return None
if guid is not None:
# Using guid to retrieve notebook
notebook = self.note_store.getNotebook(self.token, guid)
else:
# name is not None
# Get notebooks at first
notebooks = self.get_notebooks()
candidate_notebooks = filter(
lambda notebook: notebook.name == name, notebooks)
assert len(candidate_notebooks) <= 1
if len(candidate_notebooks) == 0:
return None
notebook = candidate_notebooks[0]
assert isinstance(notebook, Types.Notebook), notebook
if readable:
return self._notebook_to_dict(notebook)
return notebook
@timeout(TIMEOUT)
def get_notes(self, notebook=None, notebook_guid=None, readable=False):
assert (notebook is None or
isinstance(notebook, Types.Notebook)), notebook
assert (notebook_guid is None or
isinstance(notebook_guid, (str, unicode))), notebook_guid
assert isinstance(readable, bool), readable
if notebook is None and notebook_guid is None:
# Set default notebook
notebook = self.note_store.getDefaultNotebook(self.token)
note_filter = NoteStore.NoteFilter()
note_filter.order = Types.NoteSortOrder.CREATED
note_filter.ascending = False
if notebook is not None:
note_filter.notebookGuid = notebook.guid
else:
# notebook_guid is not None
note_filter.notebookGuid = notebook_guid
spec = NoteStore.NotesMetadataResultSpec(
includeTitle=True,
includeCreated=True,
includeNotebookGuid=True,
includeContentLength=True)
notes_meta_list = self.note_store.findNotesMetadata(
self.token, note_filter, 0, 256, spec)
assert isinstance(
notes_meta_list, NoteStore.NotesMetadataList), notes_meta_list
if readable:
return map(self._note_to_dict, notes_meta_list.notes)
return notes_meta_list.notes
# Write
@timeout(TIMEOUT)
def create_note(self, title, body, resources=[], notebook=None):
'''
Create a Note instance with title and body
Send Note object to user's account
'''
assert isinstance(title, (str, unicode)), title
assert isinstance(body, (str, unicode)), body
ourNote = Types.Note()
ourNote.title = title
# Build body of note
nBody = '<?xml version="1.0" encoding="UTF-8"?>'
nBody += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
nBody += '<en-note>%s' % body
if resources:
# Add Resource objects to note body
nBody += '<br />' * 2
ourNote.resources = resources
for resource in resources:
hexhash = binascii.hexlify(resource.data.bodyHash)
nBody += 'Attachment with hash %s: <br /><en-media type="%s" hash="%s" /><br />' % (hexhash, resource.mime, hexhash)
nBody += '</en-note>'
ourNote.content = nBody
# parentNotebook is optional; if omitted, default notebook is used
if notebook and hasattr(notebook, 'guid'):
ourNote.notebookGuid = notebook.guid
# Attempt to create note in Evernote account
try:
note = self.note_store.createNote(self.token, ourNote)
except Errors.EDAMUserException, edue:
# Something was wrong with the note data
# See EDAMErrorCode enumeration for error code explanation
# http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode
print 'EDAMUserException:', edue
return None
except Errors.EDAMNotFoundException, ednfe:
# Parent Notebook GUID doesn't correspond to an actual notebook
print 'EDAMNotFoundException: Invalid parent notebook GUID'
print ednfe
return None
# Return created note object
return note
# Delete
@timeout(TIMEOUT)
def delete_note(self, note=None, guid=None):
assert (note is None or
isinstance(note, (Types.Note, NoteStore.NoteMetadata))), note
assert guid is None or isinstance(guid, (str, unicode)), guid
if note is None and guid is None:
return
if note is not None:
self.note_store.deleteNote(self.token, note.guid)
else:
# guid is not None
self.note_store.deleteNote(self.token, guid)
# Read
@timeout(TIMEOUT)
def get_note(self, guid,
withContent=True,
withResourcesData=False,
withResourcesRecognition=False,
withResourcesAlternateData=False,
readable=False):
assert isinstance(guid, (str, unicode)), guid
note = self.note_store.getNote(
self.token, guid,
withContent, withResourcesData,
withResourcesRecognition, withResourcesAlternateData)
assert isinstance(note, Types.Note)
if readable:
return self._note_to_dict(note)
return note
def delete_notes(self, notebook=None, notebook_guid=None):
'''
Delete all the posts
'''
assert (notebook is None or
isinstance(notebook, Types.Notebook)), notebook
assert (notebook_guid is None or
isinstance(notebook_guid, (str, unicode))), notebook_guid
if notebook is None and notebook_guid is None:
# Set default notebook
notebook = self.note_store.getDefaultNotebook(self.token)
# Retrieve all notes in the notebook
notes = self.get_notes(notebook=notebook, notebook_guid=notebook_guid)
for note in notes:
print 'Delete %s (%s)' % (note.title, note.guid)
self.delete_note(note=note)
def test_evernote():
e = Evernote()
print e
# Read all
pprint(e.receive_all())
print 'Input file: ./data/eva_time_data_2.in'
with open('data/eva_time_data_2.in', 'r') as fp:
content = fp.read()
print e.send(content)
# time.sleep(3)
pprint(e.receive_all())
# Delete all
e.delete_all()
# Read all
pprint(e.receive_all())
def test_evernote_parser():
e = Evernote()
print e
# Read all
pprint(e.receive_all())
print 'Input file: ./data/eva_time_data_2.in'
with open('data/eva_time_data_2.in', 'r') as fp:
content = fp.read()
print e.send(content)
note = e.receive()
pprint(note)
print note.content
import re
res = re.search('<en-note>(?P<content>.*)</en-note>', note.content, flags=re.DOTALL)
print res.groups(), res.group('content')
# Delete all
e.delete_all()
if __name__ == '__main__':
test_evernote_parser()