|
1
|
|
|
# coding: utf-8 |
|
2
|
|
|
import evernote.edam.type.ttypes as Types |
|
3
|
|
|
from evernote.edam.error.ttypes import EDAMSystemException, EDAMUserException |
|
4
|
|
|
from evernote.edam.error.ttypes import EDAMErrorCode |
|
5
|
|
|
from evernote.edam.notestore import NoteStore |
|
6
|
|
|
|
|
7
|
|
|
from django.utils.translation import ugettext as _ |
|
8
|
|
|
from logging import getLogger |
|
9
|
|
|
from django.core.cache import caches |
|
10
|
|
|
|
|
11
|
|
|
from django_th.models import update_result |
|
12
|
|
|
|
|
13
|
|
|
logger = getLogger('django_th.trigger_happy') |
|
14
|
|
|
cache = caches['django_th'] |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class EvernoteMgr(object): |
|
18
|
|
|
@staticmethod |
|
19
|
|
|
def get_notebook(note_store, my_notebook): |
|
20
|
|
|
""" |
|
21
|
|
|
get the notebook from its name |
|
22
|
|
|
""" |
|
23
|
|
|
notebook_id = 0 |
|
24
|
|
|
notebooks = note_store.listNotebooks() |
|
25
|
|
|
# get the notebookGUID ... |
|
26
|
|
|
for notebook in notebooks: |
|
27
|
|
|
if notebook.name.lower() == my_notebook.lower(): |
|
28
|
|
|
notebook_id = notebook.guid |
|
29
|
|
|
break |
|
30
|
|
|
return notebook_id |
|
31
|
|
|
|
|
32
|
|
|
@staticmethod |
|
33
|
|
|
def set_notebook(note_store, my_notebook, notebook_id): |
|
34
|
|
|
""" |
|
35
|
|
|
create a notebook |
|
36
|
|
|
""" |
|
37
|
|
|
if notebook_id == 0: |
|
38
|
|
|
new_notebook = Types.Notebook() |
|
39
|
|
|
new_notebook.name = my_notebook |
|
40
|
|
|
new_notebook.defaultNotebook = False |
|
41
|
|
|
notebook_id = note_store.createNotebook(new_notebook).guid |
|
42
|
|
|
return notebook_id |
|
43
|
|
|
|
|
44
|
|
|
@staticmethod |
|
45
|
|
|
def get_tag(note_store, my_tags): |
|
46
|
|
|
""" |
|
47
|
|
|
get the tags from his Evernote account |
|
48
|
|
|
:param note_store Evernote Instance |
|
49
|
|
|
:param my_tags string |
|
50
|
|
|
:return: array of the tag to create |
|
51
|
|
|
""" |
|
52
|
|
|
tag_id = [] |
|
53
|
|
|
listtags = note_store.listTags() |
|
54
|
|
|
# cut the string by piece of tag with comma |
|
55
|
|
|
if ',' in my_tags: |
|
56
|
|
|
for my_tag in my_tags.split(','): |
|
57
|
|
|
for tag in listtags: |
|
58
|
|
|
# remove space before and after |
|
59
|
|
|
# thus we keep "foo bar" |
|
60
|
|
|
# but not " foo bar" nor "foo bar " |
|
61
|
|
|
if tag.name.lower() == my_tag.lower().lstrip().rstrip(): |
|
62
|
|
|
tag_id.append(tag.guid) |
|
63
|
|
|
break |
|
64
|
|
|
else: |
|
65
|
|
|
for tag in listtags: |
|
66
|
|
|
if tag.name.lower() == my_tags.lower(): |
|
67
|
|
|
tag_id.append(tag.guid) |
|
68
|
|
|
break |
|
69
|
|
|
|
|
70
|
|
|
return tag_id |
|
71
|
|
|
|
|
72
|
|
|
@staticmethod |
|
73
|
|
|
def set_tag(note_store, my_tags, tag_id): |
|
74
|
|
|
""" |
|
75
|
|
|
create a tag if not exists |
|
76
|
|
|
:param note_store evernote instance |
|
77
|
|
|
:param my_tags string |
|
78
|
|
|
:param tag_id id of the tag(s) to create |
|
79
|
|
|
:return: array of the tag to create |
|
80
|
|
|
""" |
|
81
|
|
|
new_tag = Types.Tag() |
|
82
|
|
|
if ',' in my_tags: |
|
83
|
|
|
for my_tag in my_tags.split(','): |
|
84
|
|
|
new_tag.name = my_tag |
|
85
|
|
|
note_tag_id = EvernoteMgr.create_tag(note_store, new_tag) |
|
86
|
|
|
if note_tag_id is not False: |
|
87
|
|
|
tag_id.append(note_tag_id) |
|
88
|
|
|
else: |
|
89
|
|
|
return False |
|
90
|
|
|
elif my_tags: |
|
91
|
|
|
new_tag.name = my_tags |
|
92
|
|
|
note_tag_id = EvernoteMgr.create_tag(note_store, new_tag) |
|
93
|
|
|
if note_tag_id is not False: |
|
94
|
|
|
tag_id.append(note_tag_id) |
|
95
|
|
|
else: |
|
96
|
|
|
return False |
|
97
|
|
|
return tag_id |
|
98
|
|
|
|
|
99
|
|
|
@staticmethod |
|
100
|
|
|
def create_note(note_store, note, trigger_id, data): |
|
101
|
|
|
""" |
|
102
|
|
|
create a note |
|
103
|
|
|
:param note_store Evernote instance |
|
104
|
|
|
:param note |
|
105
|
|
|
:param trigger_id id of the trigger |
|
106
|
|
|
:param data to save or to put in cache |
|
107
|
|
|
:type note_store: Evernote Instance |
|
108
|
|
|
:type note: Note instance |
|
109
|
|
|
:type trigger_id: int |
|
110
|
|
|
:type data: dict |
|
111
|
|
|
:return boolean |
|
112
|
|
|
:rtype boolean |
|
113
|
|
|
""" |
|
114
|
|
|
# create the note ! |
|
115
|
|
|
try: |
|
116
|
|
|
created_note = note_store.createNote(note) |
|
117
|
|
|
sentence = str('note %s created') % created_note.guid |
|
118
|
|
|
logger.debug(sentence) |
|
119
|
|
|
return True |
|
120
|
|
|
except EDAMSystemException as e: |
|
121
|
|
|
if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED: |
|
122
|
|
|
sentence = "Rate limit reached {code} " \ |
|
123
|
|
|
"Retry your request in {msg} seconds".format( |
|
124
|
|
|
code=e.errorCode, msg=e.rateLimitDuration) |
|
125
|
|
|
logger.warn(sentence) |
|
126
|
|
|
# put again in cache the data that could not be |
|
127
|
|
|
# published in Evernote yet |
|
128
|
|
|
cache.set('th_evernote_' + str(trigger_id), data, version=2) |
|
129
|
|
|
update_result(trigger_id, msg=sentence, status=True) |
|
130
|
|
|
return True |
|
131
|
|
|
else: |
|
132
|
|
|
logger.critical(e) |
|
133
|
|
|
return False |
|
134
|
|
|
except EDAMUserException as e: |
|
135
|
|
|
if e.errorCode == EDAMErrorCode.ENML_VALIDATION: |
|
136
|
|
|
sentence = "Data ignored due to validation" \ |
|
137
|
|
|
" error : err {code} {msg}".format( |
|
138
|
|
|
code=e.errorCode, msg=e.parameter) |
|
139
|
|
|
logger.warn(sentence) |
|
140
|
|
|
update_result(trigger_id, msg=sentence, status=True) |
|
141
|
|
|
return True |
|
142
|
|
|
except Exception as e: |
|
143
|
|
|
logger.critical(e) |
|
144
|
|
|
update_result(trigger_id, msg=e, status=False) |
|
145
|
|
|
return False |
|
146
|
|
|
|
|
147
|
|
|
@staticmethod |
|
148
|
|
|
def create_tag(note_store, new_tag): |
|
149
|
|
|
""" |
|
150
|
|
|
:param note_store Evernote instance |
|
151
|
|
|
:param new_tag: create this new tag |
|
152
|
|
|
:return: new tag id |
|
153
|
|
|
""" |
|
154
|
|
|
try: |
|
155
|
|
|
return note_store.createTag(new_tag).guid |
|
156
|
|
|
except EDAMUserException as e: |
|
157
|
|
|
if e.errorCode == EDAMErrorCode.DATA_CONFLICT: |
|
158
|
|
|
logger.info("Evernote Data Conflict Err {0}".format(e)) |
|
159
|
|
|
elif e.errorCode == EDAMErrorCode.BAD_DATA_FORMAT: |
|
160
|
|
|
logger.critical("Evernote Err {0}".format(e)) |
|
161
|
|
|
return False |
|
162
|
|
|
|
|
163
|
|
|
@staticmethod |
|
164
|
|
|
def set_header(): |
|
165
|
|
|
""" |
|
166
|
|
|
preparing the hearder of Evernote |
|
167
|
|
|
""" |
|
168
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>' \ |
|
169
|
|
|
'<!DOCTYPE en-note SYSTEM ' \ |
|
170
|
|
|
'"http://xml.evernote.com/pub/enml2.dtd">\n' |
|
171
|
|
|
|
|
172
|
|
|
@staticmethod |
|
173
|
|
|
def set_note_attribute(data): |
|
174
|
|
|
""" |
|
175
|
|
|
add the link of the 'source' in the note |
|
176
|
|
|
""" |
|
177
|
|
|
na = False |
|
178
|
|
|
if data.get('link'): |
|
179
|
|
|
na = Types.NoteAttributes() |
|
180
|
|
|
# add the url |
|
181
|
|
|
na.sourceURL = data.get('link') |
|
182
|
|
|
# add the object to the note |
|
183
|
|
|
return na |
|
184
|
|
|
|
|
185
|
|
|
@staticmethod |
|
186
|
|
|
def set_note_footer(data, trigger): |
|
187
|
|
|
""" |
|
188
|
|
|
handle the footer of the note |
|
189
|
|
|
""" |
|
190
|
|
|
footer = '' |
|
191
|
|
|
if data.get('link'): |
|
192
|
|
|
provided_by = _('Provided by') |
|
193
|
|
|
provided_from = _('from') |
|
194
|
|
|
footer_from = "<br/><br/>{} <em>{}</em> {} <a href='{}'>{}</a>" |
|
195
|
|
|
|
|
196
|
|
|
footer = footer_from.format( |
|
197
|
|
|
provided_by, trigger.trigger.description, provided_from, |
|
198
|
|
|
data.get('link'), data.get('link')) |
|
199
|
|
|
|
|
200
|
|
|
return footer |
|
201
|
|
|
|
|
202
|
|
|
@staticmethod |
|
203
|
|
|
def set_note_filter(filter_string): |
|
204
|
|
|
""" |
|
205
|
|
|
set the filter of the notes |
|
206
|
|
|
""" |
|
207
|
|
|
my_filter = NoteStore.NoteFilter() |
|
208
|
|
|
my_filter.words = filter_string |
|
209
|
|
|
return my_filter |
|
210
|
|
|
|
|
211
|
|
|
@staticmethod |
|
212
|
|
|
def set_evernote_spec(): |
|
213
|
|
|
spec = NoteStore.NotesMetadataResultSpec() |
|
214
|
|
|
spec.includeTitle = True |
|
215
|
|
|
spec.includeAttributes = True |
|
216
|
|
|
return spec |
|
217
|
|
|
|