|
1
|
|
|
# coding: utf-8 |
|
2
|
|
|
# add here the call of any native lib of python like datetime etc. |
|
3
|
|
|
|
|
4
|
|
|
# add the python API here if needed |
|
5
|
|
|
from wallabag_api.wallabag import Wallabag as Wall |
|
6
|
|
|
# django classes |
|
7
|
|
|
from django.core.urlresolvers import reverse |
|
8
|
|
|
from django.utils.log 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.html_entities import HtmlEntities |
|
14
|
|
|
from django_th.models import UserService, ServicesActivated |
|
15
|
|
|
|
|
16
|
|
|
""" |
|
17
|
|
|
handle process with wallabag |
|
18
|
|
|
put the following in settings.py |
|
19
|
|
|
|
|
20
|
|
|
TH_SERVICES = ( |
|
21
|
|
|
... |
|
22
|
|
|
'th_wallabag.my_wallabag.ServiceWallabag', |
|
23
|
|
|
... |
|
24
|
|
|
) |
|
25
|
|
|
""" |
|
26
|
|
|
|
|
27
|
|
|
logger = getLogger('django_th.trigger_happy') |
|
28
|
|
|
|
|
29
|
|
|
cache = caches['th_wallabag'] |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
class ServiceWallabag(ServicesMgr): |
|
33
|
|
|
|
|
34
|
|
|
def __init__(self, token=None): |
|
35
|
|
|
super(ServiceWallabag, self).__init__(token) |
|
36
|
|
|
self.token = token |
|
37
|
|
|
if token: |
|
38
|
|
|
try: |
|
39
|
|
|
us = UserService.objects.get(token=token, name='ServiceWallabag') |
|
40
|
|
|
|
|
41
|
|
|
self.service_username = us.username |
|
42
|
|
|
self.service_password = us.password |
|
43
|
|
|
self.service_host = us.host |
|
44
|
|
|
self.service_client_secret = us.client_secret |
|
45
|
|
|
self.service_client_id = us.client_id |
|
46
|
|
|
|
|
47
|
|
|
self.wall = Wall(host=self.service_host, |
|
48
|
|
|
client_secret=self.service_client_secret, |
|
49
|
|
|
client_id=self.service_client_id, |
|
50
|
|
|
token=self.token) |
|
51
|
|
|
except UserService.DoesNotExist: |
|
52
|
|
|
pass |
|
53
|
|
|
|
|
54
|
|
|
def new_wall(self, token): |
|
55
|
|
|
|
|
56
|
|
|
return Wall(host=self.service_host, |
|
57
|
|
|
client_secret=self.service_client_secret, |
|
58
|
|
|
client_id=self.service_client_id, |
|
59
|
|
|
token=token) |
|
60
|
|
|
|
|
61
|
|
|
def read_data(self, **kwargs): |
|
62
|
|
|
""" |
|
63
|
|
|
get the data from the service |
|
64
|
|
|
as the pocket service does not have any date |
|
65
|
|
|
in its API linked to the note, |
|
66
|
|
|
add the triggered date to the dict data |
|
67
|
|
|
thus the service will be triggered when data will be found |
|
68
|
|
|
|
|
69
|
|
|
:param kwargs: contain keyword args : trigger_id at least |
|
70
|
|
|
:type kwargs: dict |
|
71
|
|
|
|
|
72
|
|
|
:rtype: list |
|
73
|
|
|
""" |
|
74
|
|
|
data = list() |
|
75
|
|
|
trigger_id = kwargs['trigger_id'] |
|
76
|
|
|
cache.set('th_wallabag_' + str(trigger_id), data) |
|
77
|
|
|
|
|
78
|
|
|
def save_data(self, trigger_id, **data): |
|
79
|
|
|
""" |
|
80
|
|
|
let's save the data |
|
81
|
|
|
|
|
82
|
|
|
:param trigger_id: trigger ID from which to save data |
|
83
|
|
|
:param data: the data to check to be used and save |
|
84
|
|
|
:type trigger_id: int |
|
85
|
|
|
:type data: dict |
|
86
|
|
|
:return: the status of the save statement |
|
87
|
|
|
:rtype: boolean |
|
88
|
|
|
""" |
|
89
|
|
|
from th_wallabag.models import Wallabag |
|
90
|
|
|
|
|
91
|
|
|
if self.token and 'link' in data and data['link'] is not None\ |
|
92
|
|
|
and len(data['link']) > 0: |
|
93
|
|
|
# get the data of this trigger |
|
94
|
|
|
trigger = Wallabag.objects.get(trigger_id=trigger_id) |
|
95
|
|
|
|
|
96
|
|
|
title = self.set_title(data) |
|
97
|
|
|
# convert htmlentities |
|
98
|
|
|
title = HtmlEntities(title).html_entity_decode |
|
99
|
|
|
|
|
100
|
|
|
try: |
|
101
|
|
|
self.wall.post_entries(url=data['link'], |
|
102
|
|
|
title=title, |
|
103
|
|
|
tags=(trigger.tag.lower())) |
|
104
|
|
|
|
|
105
|
|
|
sentence = str('wallabag {} created').format(data['link']) |
|
106
|
|
|
logger.debug(sentence) |
|
107
|
|
|
status = True |
|
108
|
|
|
except Exception as e: |
|
109
|
|
|
if e.errno == 401: |
|
110
|
|
|
new_token = self.refresh_token() |
|
111
|
|
|
UserService.objects.filter( |
|
112
|
|
|
id=data['userservice_id']).update( |
|
113
|
|
|
token=new_token) |
|
114
|
|
|
|
|
115
|
|
|
# new wallabag session with new token |
|
116
|
|
|
new_wall = self.new_wall(new_token) |
|
117
|
|
|
try: |
|
118
|
|
|
return new_wall.post_entries(url=data['link'], |
|
119
|
|
|
title=title, |
|
120
|
|
|
tags=(trigger.tag.lower()) |
|
121
|
|
|
) |
|
122
|
|
|
except Exception as e: |
|
123
|
|
|
logger.critical(e.errno, e.strerror) |
|
124
|
|
|
status = False |
|
125
|
|
|
else: |
|
126
|
|
|
logger.critical(e.errno, e.strerror) |
|
127
|
|
|
status = False |
|
128
|
|
|
|
|
129
|
|
|
elif self.token and 'link' in data and data['link'] is not None\ |
|
130
|
|
|
and len(data['link']) == 0: |
|
131
|
|
|
logger.warning( |
|
132
|
|
|
"no link provided for trigger ID {}, so we ignore it".format(trigger_id)) |
|
133
|
|
|
status = True |
|
134
|
|
|
else: |
|
135
|
|
|
logger.critical( |
|
136
|
|
|
"no token provided for trigger ID {}".format(trigger_id)) |
|
137
|
|
|
status = False |
|
138
|
|
|
return status |
|
139
|
|
|
|
|
140
|
|
|
def auth(self, request): |
|
141
|
|
|
""" |
|
142
|
|
|
let's auth the user to the Service |
|
143
|
|
|
:param request: request object |
|
144
|
|
|
:return: callback url |
|
145
|
|
|
:rtype: string that contains the url to redirect after auth |
|
146
|
|
|
|
|
147
|
|
|
""" |
|
148
|
|
|
service = UserService.objects.get(user=request.user, name='ServiceWallabag') |
|
149
|
|
|
callback_url = 'http://%s%s' % ( |
|
150
|
|
|
request.get_host(), reverse('wallabag_callback')) |
|
151
|
|
|
params = {'username': service.username, |
|
152
|
|
|
'password': service.password, |
|
153
|
|
|
'client_id': service.client_id, |
|
154
|
|
|
'client_secret': service.client_secret} |
|
155
|
|
|
access_token = Wall.get_token(host=service.host, **params) |
|
156
|
|
|
request.session['oauth_token'] = access_token |
|
157
|
|
|
return callback_url |
|
158
|
|
|
|
|
159
|
|
|
def callback(self, request, **kwargs): |
|
160
|
|
|
""" |
|
161
|
|
|
Called from the Service when the user accept to activate it |
|
162
|
|
|
:param request: request object |
|
163
|
|
|
:return: callback url |
|
164
|
|
|
:rtype: string , path to the template |
|
165
|
|
|
""" |
|
166
|
|
|
|
|
167
|
|
|
try: |
|
168
|
|
|
UserService.objects.filter( |
|
169
|
|
|
user=request.user, |
|
170
|
|
|
name=ServicesActivated.objects.get(name='ServiceWallabag') |
|
171
|
|
|
) |
|
172
|
|
|
except KeyError: |
|
173
|
|
|
return '/' |
|
174
|
|
|
|
|
175
|
|
|
return 'wallabag/callback.html' |
|
176
|
|
|
|
|
177
|
|
|
def refresh_token(self): |
|
178
|
|
|
""" |
|
179
|
|
|
refresh the expired token |
|
180
|
|
|
:return: boolean |
|
181
|
|
|
""" |
|
182
|
|
|
params = {'username': self.service_username, |
|
183
|
|
|
'password': self.service_password, |
|
184
|
|
|
'client_id': self.service_client_id, |
|
185
|
|
|
'client_secret': self.service_client_secret} |
|
186
|
|
|
return Wall.get_token(host=self.service_host, **params) |
|
187
|
|
|
|