-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKanedo_Readability.php
More file actions
323 lines (292 loc) · 9.12 KB
/
Kanedo_Readability.php
File metadata and controls
323 lines (292 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* A API Wrapper for the Readability API TEST
* @see http://www.readability.com/publishers/api
* @author Gabriel Bretschner <info@kanedo.net>
* @package Kanedo_Readability
**/
require('OAuth.php');
require('curl.php');
/**
* Kanedo_Readability_Exception
**/
class Kanedo_Readability_Exception extends Exception {
protected $body;
protected $headers;
public function __construct($msq = "", $code = 0){
$msq = $msq;
if($msq instanceof CurlResponse){
$code = $msq->headers['Status-Code'];
$pre = "Error: ";
switch($code){
case 401:
$pre = "Authorization Required";
break;
case 404:
$pre = "Not Found";
break;
case 500:
$pre = "Internal Server Error";
break;
case 400:
$pre = "Bad Request";
break;
case 409:
$pre = "Conflict";
break;
case 403:
$pre = "Forbidden";
break;
}
$this->body = json_decode($msq->body);
$this->headers = $msq->headers;
$msg = $pre.": ".print_r($this->body ,true)." HEADERS: ".print_r($this->headers, true);
}
parent::__construct($msg, $code);
}
public function getBody(){
return $this->body;
}
public function getHeaders(){
return $this->headers;
}
}
/**
* The Wrapper Class Kanedo_Readability
* This class encapsulate all important API features
* @uses OAuth
* @package Kanedo_Readability
**/
class Kanedo_Readability {
public $error = "";
public $errnr = -1;
private $c_key;
private $c_secret;
private $oauth_consumer;
private $access_token = NULL;
private $e_authorize = "https://www.readability.com/api/rest/v1/oauth/authorize/";
private $e_request = "https://www.readability.com/api/rest/v1/oauth/request_token/";
private $e_access = "https://www.readability.com/api/rest/v1/oauth/access_token/";
private $api_base = "https://www.readability.com/api/rest/v1/";
public function __construct($consumer_key, $consumer_secret) {
$this->c_key = $consumer_key;
$this->c_secret = $consumer_secret;
$this->oauth_consumer = new OAuthConsumer($consumer_key, $consumer_secret, NULL);
}
protected function parseUrlQuery($query) {
$parsed_result = array();
$tmp = explode("&",$query);
foreach($tmp as $part){
$tmp2 = explode("=", $part);
if(count($tmp2) == 2){
$parsed_result[$tmp2[0]] = $tmp2[1];
}
}
return $parsed_result;
}
/**
* This method makes an HTTP Request with help of the curl wrapper
* It throws an exception if the status code is not 200 or 202
* @see https://github.com/shuber/curl
* @author Gabriel Bretschner
* @package Readability API
* @since 4301344192
**/
protected function makeHTTPRequest($url, $param = array(), $method="GET"){
$curl = new Curl();
switch($method){
case "GET":
$result = $curl->get($url, $param);
break;
case "POST":
$result = $curl->post($url, $param);
break;
}
if(!($result->headers['Status-Code'] == 202 || $result->headers['Status-Code'] == 200)){
throw new Kanedo_Readability_Exception($result);
}
return $result;
}
/**
* Make an API Request
* This method takes an url with parameters and signs it.
* It makes an HTTP Request and returns the result without interpreting it.
*
* @author Gabriel Bretschner
* @package Readability API
* @since 4301344192
**/
public function makeAPIRequest($url, array $params = NULL, OAuthToken $aToken = NULL, $method="GET"){
if($aToken == NULL && $this->access_token == NULL){
throw new Kanedo_Readability_Exception('access token required');
}
$token = ($aToken == NULL)?$this->access_token:$aToken;
$req = OAuthRequest::from_consumer_and_token($this->oauth_consumer, $token, $method, $url, $params);
$req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->oauth_consumer, $token);
$result = $this->makeHTTPRequest($req->to_url(),$params, $method);
return $result;
}
/**
* Generates the authorize URL
* @param $callback string The callback URL
* @return string The authorization URL
**/
public function authorize($callback){
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
NULL,
"GET",
$this->e_authorize,
array(
"oauth_callback" => $callback
)
);
$req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->oauth_consumer, NULL);
return $req->to_url();
}
public function request_token($oauth_verifier, $oauth_token, $oauth_callback_confirmed) {
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
NULL,
"GET",
$this->e_request,
array(
'oauth_verifier' => $oauth_verifier,
'oauth_token' => $oauth_token,
'oauth_callback_confirmed' => $oauth_callback_confirmed,
)
);
$req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->oauth_consumer, NULL);
$result = $this->makeHTTPRequest($req->to_url(), 'GET');
return ($this->parseUrlQuery($result));
}
/**
* Requests an access token
* @param array $results The results of Kanedo_Readability::request_token
* @return OAuthToken | false
**/
public function access_token(array $results) {
if(!array_key_exists("oauth_token", $results) ||
!array_key_exists("oauth_token_secret", $results) ||
!array_key_exists("oauth_verifier", $results) )
{
throw new Kanedo_Readability_Exception('wrong parameter');
}
$token = new OAuthToken($results['oauth_token'], $results['oauth_token_secret']);
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
$token,
'GET',
$this->e_access,
array('oauth_verifier' => $results['oauth_verifier'])
);
$req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->oauth_consumer, $token);
try{
$result = $this->makeHTTPRequest($req->to_url());
}catch (Kanedo_Readability_Exception $e) {
$this->error = $e->getMessage();
$this->errnr = $e->getCode();
return false;
}
$token_credencials = ($this->parseUrlQuery($result));
return $this->access_token = new OAuthToken($token_credencials['oauth_token'], $token_credencials['oauth_token_secret']);
}
/**
* Retrieves information about the current logged in user
* @author Gabriel Bretschner
* @package Readability API
* @since 4301344192
**/
public function getCurrentUser(OAuthToken $aToken = NULL) {
if($aToken == NULL && $this->access_token == NULL){
throw new Kanedo_Readability_Exception('access token required');
}
$token = ($aToken == NULL)?$this->access_token:$aToken;
$url = $this->api_base."users/_current";
$result = $this->makeAPIRequest($url, NULL, $token);
$result = json_decode($result->body);
if($result == NULL){
throw new Kanedo_Readability_Exception("bad json");
}
return $result;
}
/**
* Retrieves all Bookmarks available
*
* @param OAuthToken $aToken the optional token
* @author Gabriel Bretschner
* @package Readability API
* @since 4301344192
**/
public function getBookmarks(OAuthToken $aToken = NULL){
if($aToken == NULL && $this->access_token == NULL){
throw new Kanedo_Readability_Exception('access token required');
}
$token = ($aToken == NULL)?$this->access_token:$aToken;
$url = $this->api_base."bookmarks";
$result = $this->makeAPIRequest($url, NULL, $token);
return json_decode($result->body);
}
/**
* retrieves every favorite bookmark between an optional given time frame
* @param $start timestamp time since favorited
* @param $end timestamp time until favorited
* @param $aToken OAuthToken access token
* @return array List of Bookmark objects
**/
public function getFavorites($start=NULL, $end=NULL, OAuthToken $aToken = NULL){
if($aToken == NULL && $this->access_token == NULL){
throw new Kanedo_Readability_Exception('access token required');
}
$token = ($aToken == NULL)?$this->access_token:$aToken;
$url = $this->api_base."bookmarks";
$fend= date("Y-m-d", $end);
$params = array(
'favorite' => 1,
);
if($start != NULL){
$params['favorited_since'] = date("Y-m-d", $start);
}
if($end != NULL){
$params['favorited_until'] = date("Y-m-d", $end);
}
$results = $this->makeAPIRequest($url, $params, $token);
$json = json_decode($results);
if($json == NULL){
throw new Kanedo_Readability_Exception("bad json");
}
return $json->bookmarks;
}
/**
* Adds a bookmark to reading list
* @author Gabriel Bretschner
* @package Readability API
* @since 4301344192
**/
public function addBookmark($url, $fav=0, $archive = 0, OAuthToken $aToken = NULL){
if($aToken == NULL && $this->access_token == NULL){
throw new Kanedo_Readability_Exception('access token required');
}
$token = ($aToken == NULL)?$this->access_token:$aToken;
$aurl = $this->api_base."bookmarks";
$params = array(
'url' => $url,
'favorite' => $fav,
'archive' => $archive,
); /**/
try{
$result = $this->makeAPIRequest($aurl, $params, $token, 'POST');
if($result->headers['Status-Code'] == 202){
return true;
}
}catch(Kanedo_Readability_Exception $e){
if($e->getCode() != 409){
error_log($e->getMessage());
return false;
}
$result = $e->getMessage();
}
return $result;
}
}
?>