Completed
Push — master ( 8e2052...0f0f78 )
by Fox
01:20
created

ServiceEvernote.get_notebook()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
1
# coding: utf-8
2
import sys
3
import arrow
4
5
# evernote API
6
from evernote.api.client import EvernoteClient
7
from evernote.edam.notestore import NoteStore
8
import evernote.edam.type.ttypes as Types
9
from evernote.edam.error.ttypes import EDAMSystemException, EDAMUserException
10
from evernote.edam.error.ttypes import EDAMErrorCode
11
12
# django classes
13
from django.utils.translation import ugettext as _
14
from django.conf import settings
15
from django.utils.log import getLogger
16
from django.core.cache import caches
17
18
# django_th classes
19
from django_th.services.services import ServicesMgr
20
from django_th.models import UserService, ServicesActivated
21
from th_evernote.models import Evernote
22
from th_evernote.sanitize import sanitize
23
24
25
"""
26
    handle process with evernote
27
    put the following in settings.py
28
29
    TH_EVERNOTE = {
30
        'sandbox': True,
31
        'consumer_key': 'abcdefghijklmnopqrstuvwxyz',
32
        'consumer_secret': 'abcdefghijklmnopqrstuvwxyz',
33
    }
34
    sanbox set to True to make your test and False for production purpose
35
36
    TH_SERVICES = (
37
        ...
38
        'th_evernote.my_evernote.ServiceEvernote',
39
        ...
40
    )
41
42
"""
43
44
logger = getLogger('django_th.trigger_happy')
45
46
cache = caches['th_evernote']
47
48
49
class ServiceEvernote(ServicesMgr):
50
51
    def __init__(self, token=None):
52
        super(ServiceEvernote, self).__init__(token)
53
        self.sandbox = settings.TH_EVERNOTE['sandbox']
54
        self.consumer_key = settings.TH_EVERNOTE['consumer_key']
55
        self.consumer_secret = settings.TH_EVERNOTE['consumer_secret']
56
        self.token = token
57
58
        kwargs = {'consumer_key': self.consumer_key,
59
                  'consumer_secret': self.consumer_secret,
60
                  'sandbox': self.sandbox}
61
62
        if self.token:
63
            kwargs = {'token': token, 'sandbox': self.sandbox}
64
65
        self.client = EvernoteClient(**kwargs)
66
67
    def read_data(self, **kwargs):
68
        """
69
            get the data from the service
70
71
            :param kwargs: contain keyword args : trigger_id at least
72
            :type kwargs: dict
73
74
            :rtype: list
75
        """
76
        date_triggered = kwargs['date_triggered']
77
        trigger_id = kwargs['trigger_id']
78
79
        kwargs['model_name'] = 'Evernote'
80
81
        trigger = super(ServiceEvernote, self).read_data(**kwargs)
82
83
        data = []
84
        # get the data from the last time the trigger has been started
85
        # the filter will use the DateTime format in standard
86
        new_date_triggered = arrow.get(str(date_triggered)[:-6],
87
                                       'YYYY-MM-DD HH:mm:ss')
88
        new_date_triggered = str(new_date_triggered).replace(
89
            ':', '').replace('-', '').replace(' ', '')
90
        date_filter = "created:{} ".format(new_date_triggered[:-6])
91
92
        notebook_filter = ''
93
        if trigger.notebook:
94
            notebook_filter = "notebook:{} ".format(trigger.notebook)
95
        tag_filter = "tag:{} ".format(trigger.tag) if trigger.tag != '' else ''
96
97
        complet_filter = ''.join((notebook_filter, tag_filter, date_filter))
98
99
        # filter
100
        my_filter = NoteStore.NoteFilter()
101
        my_filter.words = complet_filter
102
103
        # result spec to tell to evernote
104
        # what information to include in the response
105
        spec = NoteStore.NotesMetadataResultSpec()
106
        spec.includeTitle = True
107
        spec.includeAttributes = True
108
109
        note_store = self.client.get_note_store()
110
        our_note_list = note_store.findNotesMetadata(self.token,
111
                                                     my_filter,
112
                                                     0,
113
                                                     100,
114
                                                     spec)
115
116
        for note in our_note_list.notes:
117
            whole_note = note_store.getNote(self.token,
118
                                            note.guid,
119
                                            True,
120
                                            True,
121
                                            False,
122
                                            False)
123
            content = self.cleaning_content(whole_note.content)
124
            data.append(
125
                {'title': note.title,
126
                 'my_date': arrow.get(note.created),
127
                 'link': whole_note.attributes.sourceURL,
128
                 'content': content})
129
130
        cache.set('th_evernote_' + str(trigger_id), data)
131
132
        return data
133
134
    def _create_note(self, note, trigger_id, data):
135
        """
136
            create a note
137
            :param note
138
            :param trigger_id id of the trigger
139
            :param data to save or to put in cache
140
            :type note:
141
            :type trigger_id: int
142
            :type data: dict
143
            :return boolean
144
            :rtype boolean
145
        """
146
        # create the note !
147
        try:
148
            created_note = self.note_store.createNote(note)
149
            sentance = str('note %s created') % created_note.guid
150
            logger.debug(sentance)
151
            return True
152
        except EDAMSystemException as e:
153
            if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED:
154
                sentance = "Rate limit reached {code}"
155
                sentance += "Retry your request in {msg} seconds"
156
                logger.warn(sentance.format(
157
                    code=e.errorCode,
158
                    msg=e.rateLimitDuration))
159
                # put again in cache the data that could not be
160
                # published in Evernote yet
161
                cache.set('th_evernote_' + str(trigger_id),
162
                          data,
163
                          version=2)
164
                return True
165
            else:
166
                logger.critical(e)
167
                return False
168
        except EDAMUserException as e:
169
            if e.errorCode == EDAMErrorCode.ENML_VALIDATION:
170
                sentance = "Data ignored due to validation"
171
                sentance += " error : err {code} {msg}"
172
                logger.warn(sentance.format(
173
                    code=e.errorCode,
174
                    msg=e.parameter))
175
                return True
176
        except Exception as e:
177
            logger.critical(e)
178
            return False
179
180
    def save_data(self, trigger_id, **data):
181
        """
182
            let's save the data
183
            don't want to handle empty title nor content
184
            otherwise this will produce an Exception by
185
            the Evernote's API
186
187
            :param trigger_id: trigger ID from which to save data
188
            :param data: the data to check to be used and save
189
            :type trigger_id: int
190
            :type data:  dict
191
            :return: the status of the save statement
192
            :rtype: boolean
193
        """
194
        kwargs = {}
195
        # set the title and content of the data
196
        title, content = super(ServiceEvernote, self).save_data(trigger_id,
197
                                                                data,
198
                                                                **kwargs)
199
200
        if len(title):
201
            # get the evernote data of this trigger
202
            trigger = Evernote.objects.get(trigger_id=trigger_id)
203
204
            try:
205
                self.note_store = self.client.get_note_store()
206
            except EDAMSystemException as e:
207
                # rate limite reach have to wait 1 hour !
208
                if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED:
209
                    sentance = "Rate limit reached {code}"
210
                    sentance += "Retry your request in {msg} seconds"
211
                    sentance += " - date set to cache again until"
212
                    sentance += " limit reached"
213
                    logger.warn(sentance.format(
214
                        code=e.errorCode,
215
                        msg=e.rateLimitDuration))
216
                    # put again in cache the data that could not be
217
                    # published in Evernote yet
218
                    cache.set('th_evernote_' + str(trigger_id),
219
                              data,
220
                              version=2)
221
                    return True
222
                else:
223
                    logger.critical(e)
224
                    return False
225
            except Exception as e:
226
                logger.critical(e)
227
                return False
228
229
            # note object
230
            note = Types.Note()
231
            if trigger.notebook:
232
                # get the notebookGUID ...
233
                notebook_id = self.get_notebook(trigger)
234
                # create notebookGUID if it does not exist then return its id
235
                note.notebookGuid = self.set_notebook(trigger, notebook_id)
236
237
                # ... and get the tagGUID if a tag has been provided
238
                tag_id = self.get_tag(trigger)
239
                tag_id = self.set_tag(trigger, tag_id)
240
241
                if trigger.tag is not '':
242
                    # set the tag to the note if a tag has been provided
243
                    note.tagGuids = tag_id
244
245
                logger.debug("notebook that will be used %s", trigger.notebook)
246
247
            # attribute of the note: the link to the website
248
            note_attribute = self.set_note_attribute(data)
249
            if note_attribute:
250
                note.attributes = note_attribute
251
252
            # footer of the note
253
            footer = self.set_note_footer(data, trigger)
254
            content += footer
255
256
            note.title = title
257
            note.content = self.set_evernote_header()
258
            note.content += self.get_sanitize_content(content)
259
            # create a note
260
            return self._create_note(note, trigger_id, data)
261
262
        else:
263
            sentence = "no title provided for trigger ID {}"
264
            logger.critical(sentence.format(trigger_id))
265
            return False
266
267
    def get_notebook(self, trigger):
268
        """
269
            get the notebook from its name
270
        """
271
        notebook_id = 0
272
        notebooks = self.note_store.listNotebooks()
273
        # get the notebookGUID ...
274
        for notebook in notebooks:
275
            if notebook.name.lower() == trigger.notebook.lower():
276
                notebook_id = notebook.guid
277
                break
278
        return notebook_id
279
280
    def set_notebook(self, trigger, notebook_id):
281
        """
282
            create a notebook
283
        """
284
        if notebook_id == 0:
285
            new_notebook = Types.Notebook()
286
            new_notebook.name = trigger.notebook
287
            new_notebook.defaultNotebook = False
288
            notebook_id = self.note_store.createNotebook(
289
                new_notebook).guid
290
291
        return notebook_id
292
293
    def get_tag(self, trigger):
294
        """
295
            get the tags from his Evernote account
296
        """
297
        tag_id = []
298
        if trigger.tag is not '':
299
            listtags = self.note_store.listTags()
300
            # cut the string by piece of tag with comma
301
            if ',' in trigger.tag:
302
                for my_tag in trigger.tag.split(','):
303
                    for tag in listtags:
304
                        # remove space before and after
305
                        # thus we keep "foo bar"
306
                        # but not " foo bar" nor "foo bar "
307
                        if tag.name.lower() == my_tag.lower().lstrip().rstrip():
308
                            tag_id.append(tag.guid)
309
                            break
310
            else:
311
                for tag in listtags:
312
                    if tag.name.lower() == trigger.tag.lower():
313
                        tag_id.append(tag.guid)
314
                        break
315
        return tag_id
316
317
    def set_tag(self, trigger, tag_id):
318
        """
319
            create a tag if not exists
320
            :param trigger object
321
            :param tag_id id of the tag(s) to create
322
            :return: array of the tag to create
323
        """
324
        if len(tag_id) == 0:
325
            new_tag = Types.Tag()
326
            if ',' in trigger.tag:
327
                for my_tag in trigger.tag.split(','):
328
                    new_tag.name = my_tag
329
                    tag_id.append(self._create_tag(new_tag))
330
            else:
331
                new_tag.name = trigger.tag
332
                tag_id.append(self._create_tag(new_tag))
333
        return tag_id
334
335
    def _create_tag(self, new_tag):
336
        """
337
            :param new_tag: create this new tag
338
            :return: new tag id
339
        """
340
        try:
341
            return self.note_store.createTag(new_tag).guid
342
        except EDAMUserException as e:
343
            if e.errorCode == EDAMErrorCode.DATA_CONFLICT:
344
                logger.info("Evernote Data Conflict Err {0}".format(e))
345
            elif e.errorCode == EDAMErrorCode.BAD_DATA_FORMAT:
346
                logger.critical("Evernote Err {0}".format(e))
347
348
    @staticmethod
349
    def set_evernote_header():
350
        """
351
            preparing the hearder of Evernote
352
        """
353
        prolog = '<?xml version="1.0" encoding="UTF-8"?>'
354
        prolog += '<!DOCTYPE en-note SYSTEM \
355
        "http://xml.evernote.com/pub/enml2.dtd">\n'
356
        return prolog
357
358
    @staticmethod
359
    def get_sanitize_content(content):
360
        """
361
            tidy and sanitize content
362
        """
363
        enml = sanitize(content)
364
        # python 2
365
        if sys.version_info.major == 2:
366
            return enml.encode('ascii', 'xmlcharrefreplace')
367
        else:
368
            return str(enml)
369
370
    @staticmethod
371
    def set_note_attribute(data):
372
        """
373
           add the link of the 'source' in the note
374
           get a NoteAttributes object
375
        """
376
        na = False
377
        if 'link' in data:
378
            na = Types.NoteAttributes()
379
            # add the url
380
            na.sourceURL = data['link']
381
            # add the object to the note
382
        return na
383
384
    @staticmethod
385
    def set_note_footer(data, trigger):
386
        """
387
            handle the footer of the note
388
        """
389
        footer = ''
390
        if 'link' in data:
391
            provided_by = _('Provided by')
392
            provided_from = _('from')
393
            footer_from = "<br/><br/>{} <em>{}</em> {} <a href='{}'>{}</a>"
394
395
            # python 2
396
            if sys.version_info.major == 2:
397
                description = trigger.trigger.description.encode(
398
                    'ascii', 'xmlcharrefreplace')
399
            else:
400
                description = trigger.trigger.description
401
            footer = footer_from.format(
402
                provided_by, description, provided_from,
403
                data['link'], data['link'])
404
405
        return footer
406
407
    def get_evernote_client(self, token=None):
408
        """
409
            get the token from evernote
410
        """
411
        if token:
412
            return EvernoteClient(
413
                token=token,
414
                sandbox=self.sandbox)
415
        else:
416
            return EvernoteClient(
417
                consumer_key=self.consumer_key,
418
                consumer_secret=self.consumer_secret,
419
                sandbox=self.sandbox)
420
421
    def auth(self, request):
422
        """
423
            let's auth the user to the Service
424
        """
425
        client = self.get_evernote_client()
426
        request_token = client.get_request_token(
427
            self.callback_url(request, 'evernote'))
428
429
        # Save the request token information for later
430
        request.session['oauth_token'] = request_token['oauth_token']
431
        request.session['oauth_token_secret'] = request_token[
432
            'oauth_token_secret']
433
434
        # Redirect the user to the Evernote authorization URL
435
        # return the URL string which will be used by redirect()
436
        # from the calling func
437
        return client.get_authorize_url(request_token)
438
439
    def callback(self, request, **kwargs):
440
        """
441
            Called from the Service when the user accept to activate it
442
        """
443
        try:
444
            client = self.get_evernote_client()
445
            # finally we save the user auth token
446
            # As we already stored the object ServicesActivated
447
            # from the UserServiceCreateView now we update the same
448
            # object to the database so :
449
            # 1) we get the previous objet
450
            us = UserService.objects.get(
451
                user=request.user,
452
                name=ServicesActivated.objects.get(name='ServiceEvernote'))
453
            # 2) then get the token
454
            us.token = client.get_access_token(
455
                request.session['oauth_token'],
456
                request.session['oauth_token_secret'],
457
                request.GET.get('oauth_verifier', '')
458
            )
459
            # 3) and save everything
460
            us.save()
461
        except KeyError:
462
            return '/'
463
464
        return 'evernote/callback.html'
465
466
    @staticmethod
467
    def cleaning_content(data):
468
469
        data = data.replace('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">\n<en-note>', '')
470
        data = data.replace('</en-note>', '')
471
472
        return data
473