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 django.utils.log 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['th_evernote'] |
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
|
|
|
tag_id.append(EvernoteMgr.create_tag(note_store, new_tag)) |
86
|
|
|
elif my_tags: |
87
|
|
|
new_tag.name = my_tags |
88
|
|
|
tag_id.append(EvernoteMgr.create_tag(note_store, new_tag)) |
89
|
|
|
|
90
|
|
|
return tag_id |
91
|
|
|
|
92
|
|
|
@staticmethod |
93
|
|
|
def create_note(note_store, note, trigger_id, data): |
94
|
|
|
""" |
95
|
|
|
create a note |
96
|
|
|
:param note_store Evernote instance |
97
|
|
|
:param note |
98
|
|
|
:param trigger_id id of the trigger |
99
|
|
|
:param data to save or to put in cache |
100
|
|
|
:type note_store: Evernote Instance |
101
|
|
|
:type note: Note instance |
102
|
|
|
:type trigger_id: int |
103
|
|
|
:type data: dict |
104
|
|
|
:return boolean |
105
|
|
|
:rtype boolean |
106
|
|
|
""" |
107
|
|
|
# create the note ! |
108
|
|
|
try: |
109
|
|
|
created_note = note_store.createNote(note) |
110
|
|
|
sentence = str('note %s created') % created_note.guid |
111
|
|
|
logger.debug(sentence) |
112
|
|
|
return True |
113
|
|
|
except EDAMSystemException as e: |
114
|
|
|
if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED: |
115
|
|
|
sentence = "Rate limit reached {code} " \ |
116
|
|
|
"Retry your request in {msg} seconds".format( |
117
|
|
|
code=e.errorCode, msg=e.rateLimitDuration) |
118
|
|
|
logger.warn(sentence) |
119
|
|
|
# put again in cache the data that could not be |
120
|
|
|
# published in Evernote yet |
121
|
|
|
cache.set('th_evernote_' + str(trigger_id), data, version=2) |
122
|
|
|
update_result(trigger_id, msg=sentence) |
123
|
|
|
return True |
124
|
|
|
else: |
125
|
|
|
logger.critical(e) |
126
|
|
|
return False |
127
|
|
|
except EDAMUserException as e: |
128
|
|
|
if e.errorCode == EDAMErrorCode.ENML_VALIDATION: |
129
|
|
|
sentence = "Data ignored due to validation" \ |
130
|
|
|
" error : err {code} {msg}".format( |
131
|
|
|
code=e.errorCode, msg=e.parameter) |
132
|
|
|
logger.warn(sentence) |
133
|
|
|
update_result(trigger_id, msg=sentence) |
134
|
|
|
return True |
135
|
|
|
except Exception as e: |
136
|
|
|
logger.critical(e) |
137
|
|
|
update_result(trigger_id, msg=e) |
138
|
|
|
return False |
139
|
|
|
|
140
|
|
|
@staticmethod |
141
|
|
|
def create_tag(note_store, new_tag): |
142
|
|
|
""" |
143
|
|
|
:param note_store Evernote instance |
144
|
|
|
:param new_tag: create this new tag |
145
|
|
|
:return: new tag id |
146
|
|
|
""" |
147
|
|
|
try: |
148
|
|
|
return note_store.createTag(new_tag).guid |
149
|
|
|
except EDAMUserException as e: |
150
|
|
|
if e.errorCode == EDAMErrorCode.DATA_CONFLICT: |
151
|
|
|
logger.info("Evernote Data Conflict Err {0}".format(e)) |
152
|
|
|
elif e.errorCode == EDAMErrorCode.BAD_DATA_FORMAT: |
153
|
|
|
logger.critical("Evernote Err {0}".format(e)) |
154
|
|
|
|
155
|
|
|
@staticmethod |
156
|
|
|
def set_header(): |
157
|
|
|
""" |
158
|
|
|
preparing the hearder of Evernote |
159
|
|
|
""" |
160
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>' \ |
161
|
|
|
'<!DOCTYPE en-note SYSTEM ' \ |
162
|
|
|
'"http://xml.evernote.com/pub/enml2.dtd">\n' |
163
|
|
|
|
164
|
|
|
@staticmethod |
165
|
|
|
def set_note_attribute(data): |
166
|
|
|
""" |
167
|
|
|
add the link of the 'source' in the note |
168
|
|
|
""" |
169
|
|
|
na = False |
170
|
|
|
if data.get('link'): |
171
|
|
|
na = Types.NoteAttributes() |
172
|
|
|
# add the url |
173
|
|
|
na.sourceURL = data.get('link') |
174
|
|
|
# add the object to the note |
175
|
|
|
return na |
176
|
|
|
|
177
|
|
|
@staticmethod |
178
|
|
|
def set_note_footer(data, trigger): |
179
|
|
|
""" |
180
|
|
|
handle the footer of the note |
181
|
|
|
""" |
182
|
|
|
footer = '' |
183
|
|
|
if data.get('link'): |
184
|
|
|
provided_by = _('Provided by') |
185
|
|
|
provided_from = _('from') |
186
|
|
|
footer_from = "<br/><br/>{} <em>{}</em> {} <a href='{}'>{}</a>" |
187
|
|
|
|
188
|
|
|
footer = footer_from.format( |
189
|
|
|
provided_by, trigger.trigger.description, provided_from, |
190
|
|
|
data.get('link'), data.get('link')) |
191
|
|
|
|
192
|
|
|
return footer |
193
|
|
|
|
194
|
|
|
@staticmethod |
195
|
|
|
def set_note_filter(filter_string): |
196
|
|
|
""" |
197
|
|
|
set the filter of the notes |
198
|
|
|
""" |
199
|
|
|
my_filter = NoteStore.NoteFilter() |
200
|
|
|
my_filter.words = filter_string |
201
|
|
|
return my_filter |
202
|
|
|
|
203
|
|
|
@staticmethod |
204
|
|
|
def set_evernote_spec(): |
205
|
|
|
spec = NoteStore.NotesMetadataResultSpec() |
206
|
|
|
spec.includeTitle = True |
207
|
|
|
spec.includeAttributes = True |
208
|
|
|
return spec |
209
|
|
|
|