ServicePushbullet   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 13.98 %

Importance

Changes 0
Metric Value
dl 13
loc 93
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
B save_data() 0 35 4
A __init__() 13 13 2
C read_data() 0 39 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# coding: utf-8
2
import arrow
3
# Pushbullet
4
from pushbullet import Pushbullet as Pushb
5
6
# django classes
7
from django.conf import settings
8
from logging import getLogger
9
from django.core.cache import caches
10
11
# django_th classes
12
from django_th.services.services import ServicesMgr
13
from django_th.models import update_result
14
from th_pushbullet.models import Pushbullet
15
16
17
"""
18
    handle process with pushbullet
19
    put the following in settings.py
20
21
    TH_PUSHBULLET = {
22
        'client_id': 'abcdefghijklmnopqrstuvwxyz',
23
        'client_secret': 'abcdefghijklmnopqrstuvwxyz',
24
    }
25
    TH_SERVICES = (
26
        ...
27
        'th_pushbullet.my_pushbullet.ServicePushbullet',
28
        ...
29
    )
30
"""
31
32
logger = getLogger('django_th.trigger_happy')
33
34
cache = caches['django_th']
35
36
37
class ServicePushbullet(ServicesMgr):
38
    """
39
        Service Pushbullet
40
    """
41 View Code Duplication
    def __init__(self, token=None, **kwargs):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
42
        super(ServicePushbullet, self).__init__(token, **kwargs)
43
        self.AUTH_URL = 'https://pushbullet.com/authorize'
44
        self.ACC_TOKEN = 'https://pushbullet.com/access_token'
45
        self.REQ_TOKEN = 'https://api.pushbullet.com/oauth2/token'
46
        self.consumer_key = settings.TH_PUSHBULLET_KEY['client_id']
47
        self.consumer_secret = settings.TH_PUSHBULLET_KEY['client_secret']
48
        self.scope = 'everything'
49
        self.service = 'ServicePushbullet'
50
        self.oauth = 'oauth2'
51
        if token:
52
            self.token = token
53
            self.pushb = Pushb(token)
54
55
    def read_data(self, **kwargs):
56
        """
57
            get the data from the service
58
            as the pushbullet service does not have any date
59
            in its API linked to the note,
60
            add the triggered date to the dict data
61
            thus the service will be triggered when data will be found
62
63
            :param kwargs: contain keyword args : trigger_id at least
64
            :type kwargs: dict
65
66
            :rtype: list
67
        """
68
        trigger_id = kwargs.get('trigger_id')
69
        trigger = Pushbullet.objects.get(trigger_id=trigger_id)
70
        date_triggered = kwargs.get('date_triggered')
71
        data = list()
72
        pushes = self.pushb.get_pushes()
73
        for p in pushes:
74
            title = 'From Pushbullet'
75
            created = arrow.get(p.get('created'))
76
            if created > date_triggered and p.get('type') == trigger.type and\
77
                (p.get('sender_email') == p.get('receiver_email') or
78
                 p.get('sender_email') is None):
79
                title = title + ' Channel' if p.get('channel_iden') and \
80
                                              p.get('title') is None else title
81
                # if sender_email and receiver_email are the same ;
82
                # that means that "I" made a note or something
83
                # if sender_email is None, then "an API" does the post
84
85
                body = p.get('body')
86
                data.append({'title': title, 'content': body})
87
                # digester
88
                self.send_digest_event(trigger_id,
89
                                       title,
90
                                       '')
91
92
        cache.set('th_pushbullet_' + str(trigger_id), data)
93
        return data
94
95
    def save_data(self, trigger_id, **data):
96
        """
97
            let's save the data
98
            :param trigger_id: trigger ID from which to save data
99
            :param data: the data to check to be used and save
100
            :type trigger_id: int
101
            :type data:  dict
102
            :return: the status of the save statement
103
            :rtype: boolean
104
        """
105
        title, content = super(ServicePushbullet, self).save_data(trigger_id,
106
                                                                  **data)
107
108
        if self.token:
109
            trigger = Pushbullet.objects.get(trigger_id=trigger_id)
110
            if trigger.type == 'note':
111
                status = self.pushb.push_note(title=title, body=content)
112
            elif trigger.type == 'link':
113
                status = self.pushb.push_link(title=title, body=content,
114
                                              url=data.get('link'))
115
                sentence = str('pushbullet {} created').format(title)
116
                logger.debug(sentence)
117
            else:
118
                # no valid type of pushbullet specified
119
                msg = "no valid type of pushbullet specified"
120
                logger.critical(msg)
121
                update_result(trigger_id, msg=msg, status=False)
122
                status = False
123
        else:
124
            msg = "no token or link provided for trigger " \
125
                  "ID {} ".format(trigger_id)
126
            logger.critical(msg)
127
            update_result(trigger_id, msg=msg, status=False)
128
            status = False
129
        return status
130