|
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.get('date_triggered') |
|
63
|
|
|
trigger_id = kwargs.get('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 data.get('link'): |
|
109
|
|
|
if len(data.get('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.get('link')) |
|
114
|
|
|
|
|
115
|
|
|
if trigger.tag is not None and len(trigger.tag) > 0: |
|
116
|
|
|
try: |
|
117
|
|
|
self.client.add_tags_to_bookmark( |
|
118
|
|
|
bookmark_id, tags=(trigger.tag.lower())) |
|
119
|
|
|
sentence = str('readability {} created item id {}').format( |
|
120
|
|
|
data.get('link'), bookmark_id) |
|
121
|
|
|
logger.debug(sentence) |
|
122
|
|
|
status = True |
|
123
|
|
|
except Exception as e: |
|
124
|
|
|
logger.critical(e) |
|
125
|
|
|
status = False |
|
126
|
|
|
|
|
127
|
|
|
else: |
|
128
|
|
|
logger.warning( |
|
129
|
|
|
"no link provided for trigger ID {}, so we ignore it".format(trigger_id)) |
|
130
|
|
|
status = True |
|
131
|
|
|
else: |
|
132
|
|
|
logger.critical( |
|
133
|
|
|
"no token provided for trigger ID {}".format(trigger_id)) |
|
134
|
|
|
status = False |
|
135
|
|
|
return status |
|
136
|
|
|
|
|
137
|
|
|
def auth(self, request): |
|
138
|
|
|
""" |
|
139
|
|
|
let's auth the user to the Service |
|
140
|
|
|
:param request: request object |
|
141
|
|
|
:return: callback url |
|
142
|
|
|
:rtype: string that contains the url to redirect after auth |
|
143
|
|
|
""" |
|
144
|
|
|
request_token = super(ServiceReadability, self).auth(request) |
|
145
|
|
|
callback_url = self.callback_url(request, 'readability') |
|
146
|
|
|
|
|
147
|
|
|
# URL to redirect user to, to authorize your app |
|
148
|
|
|
auth_url_str = '%s?oauth_token=%s&oauth_callback=%s' |
|
149
|
|
|
auth_url = auth_url_str % (self.AUTH_URL, |
|
150
|
|
|
request_token['oauth_token'], |
|
151
|
|
|
callback_url) |
|
152
|
|
|
|
|
153
|
|
|
return auth_url |
|
154
|
|
|
|
|
155
|
|
|
def callback(self, request, **kwargs): |
|
156
|
|
|
""" |
|
157
|
|
|
Called from the Service when the user accept to activate it |
|
158
|
|
|
:param request: request object |
|
159
|
|
|
:return: callback url |
|
160
|
|
|
:rtype: string , path to the template |
|
161
|
|
|
""" |
|
162
|
|
|
kwargs = {'access_token': '', 'service': 'ServiceReadability', |
|
163
|
|
|
'return': 'readability'} |
|
164
|
|
|
return super(ServiceReadability, self).callback(request, **kwargs) |
|
165
|
|
|
|