1
|
|
|
# coding: utf-8 |
2
|
|
|
|
3
|
|
|
# Oauth2Session |
4
|
|
|
from requests_oauthlib import OAuth2Session |
5
|
|
|
|
6
|
|
|
# Pushbullet |
7
|
|
|
from pushbullet import Pushbullet as Pushb |
8
|
|
|
|
9
|
|
|
# django classes |
10
|
|
|
from django.conf import settings |
11
|
|
|
from django.utils.log import getLogger |
12
|
|
|
from django.core.cache import caches |
13
|
|
|
|
14
|
|
|
# django_th classes |
15
|
|
|
from django_th.models import UserService, ServicesActivated |
16
|
|
|
from django_th.services.services import ServicesMgr |
17
|
|
|
from th_pushbullet.models import Pushbullet |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
""" |
21
|
|
|
handle process with pushbullet |
22
|
|
|
put the following in settings.py |
23
|
|
|
|
24
|
|
|
TH_PUSHBULLET = { |
25
|
|
|
'client_id': 'abcdefghijklmnopqrstuvwxyz', |
26
|
|
|
'client_secret': 'abcdefghijklmnopqrstuvwxyz', |
27
|
|
|
} |
28
|
|
|
TH_SERVICES = ( |
29
|
|
|
... |
30
|
|
|
'th_pushbullet.my_pushbullet.ServicePushbullet', |
31
|
|
|
... |
32
|
|
|
) |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
logger = getLogger('django_th.trigger_happy') |
36
|
|
|
|
37
|
|
|
cache = caches['th_pushbullet'] |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
class ServicePushbullet(ServicesMgr): |
41
|
|
|
|
42
|
|
|
def __init__(self, token=None, **kwargs): |
43
|
|
|
super(ServicePushbullet, self).__init__(token, **kwargs) |
44
|
|
|
self.AUTH_URL = 'https://pushbullet.com/authorize' |
45
|
|
|
self.ACC_TOKEN = 'https://pushbullet.com/access_token' |
46
|
|
|
self.REQ_TOKEN = 'https://api.pushbullet.com/oauth2/token' |
47
|
|
|
self.consumer_key = settings.TH_PUSHBULLET['client_id'] |
48
|
|
|
self.consumer_secret = settings.TH_PUSHBULLET['client_secret'] |
49
|
|
|
self.scope = 'everything' |
50
|
|
|
if token: |
51
|
|
|
self.token = token |
52
|
|
|
self.pushb = Pushb(token) |
53
|
|
|
|
54
|
|
|
def read_data(self, **kwargs): |
55
|
|
|
""" |
56
|
|
|
get the data from the service |
57
|
|
|
as the pushbullet service does not have any date |
58
|
|
|
in its API linked to the note, |
59
|
|
|
add the triggered date to the dict data |
60
|
|
|
thus the service will be triggered when data will be found |
61
|
|
|
|
62
|
|
|
:param kwargs: contain keyword args : trigger_id at least |
63
|
|
|
:type kwargs: dict |
64
|
|
|
|
65
|
|
|
:rtype: list |
66
|
|
|
""" |
67
|
|
|
trigger_id = kwargs['trigger_id'] |
68
|
|
|
data = list() |
69
|
|
|
cache.set('th_pushbullet_' + str(trigger_id), data) |
70
|
|
|
|
71
|
|
|
def save_data(self, trigger_id, **data): |
72
|
|
|
""" |
73
|
|
|
let's save the data |
74
|
|
|
:param trigger_id: trigger ID from which to save data |
75
|
|
|
:param data: the data to check to be used and save |
76
|
|
|
:type trigger_id: int |
77
|
|
|
:type data: dict |
78
|
|
|
:return: the status of the save statement |
79
|
|
|
:rtype: boolean |
80
|
|
|
""" |
81
|
|
|
kwargs = {} |
82
|
|
|
|
83
|
|
|
title, content = super(ServicePushbullet, self).save_data(trigger_id, |
84
|
|
|
data, |
85
|
|
|
**kwargs) |
86
|
|
|
|
87
|
|
|
if self.token: |
88
|
|
|
trigger = Pushbullet.objects.get(trigger_id=trigger_id) |
89
|
|
|
if trigger.type == 'note': |
90
|
|
|
status = self.pushb.push_note(title=title, body=content) |
91
|
|
|
elif trigger.type == 'link': |
92
|
|
|
status = self.pushb.push_link(title=title, body=content, |
93
|
|
|
url=data.get('link')) |
94
|
|
|
sentence = str('pushbullet {} created').format(title) |
95
|
|
|
logger.debug(sentence) |
96
|
|
|
else: |
97
|
|
|
# no valid type of pushbullet specified |
98
|
|
|
logger.critical("no valid type of pushbullet specified") |
99
|
|
|
status = False |
100
|
|
|
else: |
101
|
|
|
logger.critical("no token or link provided for " |
102
|
|
|
"trigger ID {} ".format(trigger_id)) |
103
|
|
|
status = False |
104
|
|
|
return status |
105
|
|
|
|
106
|
|
|
def auth(self, request): |
107
|
|
|
""" |
108
|
|
|
let's auth the user to the Service |
109
|
|
|
:param request: request object |
110
|
|
|
:return: callback url |
111
|
|
|
:rtype: string that contains the url to redirect after auth |
112
|
|
|
""" |
113
|
|
|
callback_url = self.callback_url(request, 'pushbullet') |
114
|
|
|
oauth = OAuth2Session(client_id=self.consumer_key, |
115
|
|
|
redirect_uri=callback_url) |
116
|
|
|
authorization_url, state = oauth.authorization_url(self.AUTH_URL) |
117
|
|
|
|
118
|
|
|
return authorization_url |
119
|
|
|
|
120
|
|
|
def callback(self, request, **kwargs): |
121
|
|
|
""" |
122
|
|
|
Called from the Service when the user accept to activate it |
123
|
|
|
""" |
124
|
|
|
callback_url = self.callback_url(request, 'pushbullet') |
125
|
|
|
oauth = OAuth2Session(client_id=self.consumer_key, |
126
|
|
|
scope=self.scope, |
127
|
|
|
redirect_uri=callback_url) |
128
|
|
|
response = oauth.fetch_token(self.REQ_TOKEN, |
129
|
|
|
code=request.GET.get('code', ''), |
130
|
|
|
authorization_response=callback_url, |
131
|
|
|
client_secret=self.consumer_secret) |
132
|
|
|
|
133
|
|
|
token = response.get('access_token') |
134
|
|
|
service_name = ServicesActivated.objects.get(name='ServicePushbullet') |
135
|
|
|
UserService.objects.filter(user=request.user, |
136
|
|
|
name=service_name |
137
|
|
|
).update(token=token) |
138
|
|
|
|
139
|
|
|
return 'pushbullet/callback.html' |
140
|
|
|
|