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