1
|
|
|
# coding: utf-8 |
2
|
|
|
# readability API |
3
|
|
|
from readability import ReaderClient |
4
|
|
|
|
5
|
|
|
# django classes |
6
|
|
|
from django.conf import settings |
7
|
|
|
from django.utils.log import getLogger |
8
|
|
|
from django.core.cache import caches |
9
|
|
|
|
10
|
|
|
# django_th classes |
11
|
|
|
from django_th.services.services import ServicesMgr |
12
|
|
|
from th_readability.models import Readability |
13
|
|
|
|
14
|
|
|
""" |
15
|
|
|
handle process with readability |
16
|
|
|
put the following in settings.py |
17
|
|
|
|
18
|
|
|
TH_READABILITY = { |
19
|
|
|
'consumer_key': 'abcdefghijklmnopqrstuvwxyz', |
20
|
|
|
'consumer_secret': 'abcdefghijklmnopqrstuvwxyz', |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
TH_SERVICES = ( |
24
|
|
|
... |
25
|
|
|
'th_readability.my_readability.ServiceReadability', |
26
|
|
|
... |
27
|
|
|
) |
28
|
|
|
|
29
|
|
|
""" |
30
|
|
|
|
31
|
|
|
logger = getLogger('django_th.trigger_happy') |
32
|
|
|
|
33
|
|
|
cache = caches['th_readability'] |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
class ServiceReadability(ServicesMgr): |
37
|
|
|
|
38
|
|
View Code Duplication |
def __init__(self, token=None): |
|
|
|
|
39
|
|
|
super(ServiceReadability, self).__init__(token) |
40
|
|
|
base = 'https://www.readability.com' |
41
|
|
|
self.AUTH_URL = '{}/api/rest/v1/oauth/authorize/'.format(base) |
42
|
|
|
self.REQ_TOKEN = '{}/api/rest/v1/oauth/request_token/'.format(base) |
43
|
|
|
self.ACC_TOKEN = '{}/api/rest/v1/oauth/access_token/'.format(base) |
44
|
|
|
self.consumer_key = settings.TH_READABILITY['consumer_key'] |
45
|
|
|
self.consumer_secret = settings.TH_READABILITY['consumer_secret'] |
46
|
|
|
self.token = token |
47
|
|
|
kwargs = {'consumer_key': self.consumer_key, |
48
|
|
|
'consumer_secret': self.consumer_secret} |
49
|
|
|
if token: |
50
|
|
|
token_key, token_secret = self.token.split('#TH#') |
51
|
|
|
self.client = ReaderClient(token_key, token_secret, **kwargs) |
52
|
|
|
|
53
|
|
|
def read_data(self, **kwargs): |
54
|
|
|
""" |
55
|
|
|
get the data from the service |
56
|
|
|
|
57
|
|
|
:param kwargs: contain keyword args : trigger_id at least |
58
|
|
|
:type kwargs: dict |
59
|
|
|
|
60
|
|
|
:rtype: list |
61
|
|
|
""" |
62
|
|
|
date_triggered = kwargs['date_triggered'] |
63
|
|
|
trigger_id = kwargs['trigger_id'] |
64
|
|
|
data = [] |
65
|
|
|
|
66
|
|
|
if self.token is not None: |
67
|
|
|
|
68
|
|
|
bookmarks = self.client.get_bookmarks( |
69
|
|
|
added_since=date_triggered).content |
70
|
|
|
|
71
|
|
|
for bookmark in bookmarks.values(): |
72
|
|
|
|
73
|
|
|
for b in bookmark: |
74
|
|
|
if 'article' in b: |
75
|
|
|
title = '' |
76
|
|
|
if 'title' in b['article']: |
77
|
|
|
title = b['article']['title'] |
78
|
|
|
|
79
|
|
|
link = '' |
80
|
|
|
if 'url' in b['article']: |
81
|
|
|
link = b['article']['url'] |
82
|
|
|
|
83
|
|
|
content = '' |
84
|
|
|
if 'excerpt' in b['article']: |
85
|
|
|
content = b['article']['excerpt'] |
86
|
|
|
|
87
|
|
|
data.append( |
88
|
|
|
{'title': title, |
89
|
|
|
'link': link, |
90
|
|
|
'content': content}) |
91
|
|
|
|
92
|
|
|
cache.set('th_readability_' + str(trigger_id), data) |
93
|
|
|
|
94
|
|
|
return data |
95
|
|
|
|
96
|
|
|
def save_data(self, trigger_id, **data): |
97
|
|
|
""" |
98
|
|
|
let's save the data |
99
|
|
|
|
100
|
|
|
:param trigger_id: trigger ID from which to save data |
101
|
|
|
:param data: the data to check to be used and save |
102
|
|
|
:type trigger_id: int |
103
|
|
|
:type data: dict |
104
|
|
|
:return: the status of the save statement |
105
|
|
|
:rtype: boolean |
106
|
|
|
""" |
107
|
|
|
status = False |
108
|
|
|
if self.token and 'link' in data and data['link'] is not None \ |
109
|
|
|
and len(data['link']) > 0: |
110
|
|
|
# get the data of this trigger |
111
|
|
|
trigger = Readability.objects.get(trigger_id=trigger_id) |
112
|
|
|
|
113
|
|
|
bookmark_id = self.client.add_bookmark(url=data['link']) |
114
|
|
|
|
115
|
|
|
if trigger.tag is not None and len(trigger.tag) > 0: |
116
|
|
|
try: |
117
|
|
|
self.client.add_tags_to_bookmark( |
118
|
|
View Code Duplication |
bookmark_id, tags=(trigger.tag.lower())) |
|
|
|
|
119
|
|
|
sentence = str('readability {} created item id {}').format( |
120
|
|
|
data['link'], bookmark_id) |
121
|
|
|
logger.debug(sentence) |
122
|
|
|
status = True |
123
|
|
|
except Exception as e: |
124
|
|
|
logger.critical(e) |
125
|
|
|
status = False |
126
|
|
|
|
127
|
|
|
elif self.token and 'link' in data and data['link'] is not None\ |
128
|
|
|
and len(data['link']) == 0: |
129
|
|
|
logger.warning( |
130
|
|
|
"no link provided for trigger ID {}, so we ignore it".format(trigger_id)) |
131
|
|
|
status = True |
132
|
|
|
else: |
133
|
|
|
logger.critical( |
134
|
|
|
"no token provided for trigger ID {}".format(trigger_id)) |
135
|
|
|
status = False |
136
|
|
|
return status |
137
|
|
|
|
138
|
|
|
def auth(self, request): |
139
|
|
|
""" |
140
|
|
|
let's auth the user to the Service |
141
|
|
|
:param request: request object |
142
|
|
|
:return: callback url |
143
|
|
|
:rtype: string that contains the url to redirect after auth |
144
|
|
|
""" |
145
|
|
|
request_token = super(ServiceReadability, self).auth(request) |
146
|
|
|
callback_url = self.callback_url(request, 'readability') |
147
|
|
|
|
148
|
|
|
# URL to redirect user to, to authorize your app |
149
|
|
|
auth_url_str = '%s?oauth_token=%s&oauth_callback=%s' |
150
|
|
|
auth_url = auth_url_str % (self.AUTH_URL, |
151
|
|
|
request_token['oauth_token'], |
152
|
|
|
callback_url) |
153
|
|
|
|
154
|
|
|
return auth_url |
155
|
|
|
|
156
|
|
|
def callback(self, request, **kwargs): |
157
|
|
|
""" |
158
|
|
|
Called from the Service when the user accept to activate it |
159
|
|
|
:param request: request object |
160
|
|
|
:return: callback url |
161
|
|
|
:rtype: string , path to the template |
162
|
|
|
""" |
163
|
|
|
kwargs = {'access_token': '', 'service': 'ServiceReadability', |
164
|
|
|
'return': 'readability'} |
165
|
|
|
return super(ServiceReadability, self).callback(request, **kwargs) |
166
|
|
|
|