This repository has been archived by the owner on Dec 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreader.php
429 lines (374 loc) · 14.8 KB
/
greader.php
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
/**
* Class operates on google reader via its unofficial API on behalf of user.
* Or on behalf of an app, which itself operates on behalf on the user.
* Or an app, which operates on behalf of someone, who does this on behalf
* of some user.
*
* @copyright Copyright (c) 2011, Kaspars Foigts
* @license MIT
* @author Kaspars Foigts <[email protected]>
* @link https://github.com/laacz
* @
*/
class GReader {
// Friend types
const FRIEND_TYPE_FOLLOWER = 0; // this person is following the user
const FRIEND_TYPE_FOLLOWING = 1; // the user is following this person
const FRIEND_TYPE_CONTACT = 3; // this person is in the user's contacts list
const FRIEND_TYPE_PENDING_FOLLOWING = 4; // the user is attempting to follow this person
const FRIEND_TYPE_PENDING_FOLLOWER = 5; // this person is attempting to follow this user
const FRIEND_TYPE_ALLOWED_FOLLOWING = 6; // the user is allowed to follow this person
const FRIEND_TYPE_ALLOWED_COMMENTING = 7; // the user is allowed to comment on this person's shared items
// Friend flags
const FRIEND_FLAG_IS_ME = 1; // represents the current user
const FRIEND_FLAG_IS_HIDDEN = 2; // current user has hidden this person from the list of people with shared items that show up people that they follow
const FRIEND_FLAG_USES_READER = 4; // this person uses reader
const FRIEND_FLAG_IS_BLOCKED = 8; // the user has blocked this person
const FRIEND_FLAG_HAS_PROFILE = 16; // this person has created a Google Profile
const FRIEND_FLAG_IS_IGNORED = 32; // this person has requested to follow the user, but the use has ignored the request
const FRIEND_FLAG_IS_NEW_FOLLOWER = 64; // this person has just begun to follow the user
const FRIEND_FLAG_IS_ANONYMOUS = 128; // this person doesn't have a display name set
const FRIEND_FLAG_HAS_SHARED_ITEMS = 256; // this person has shared items in reader
// CURL instance
private $ch;
var $ch_info;
// Contains Google Reader user's info
var $userInfo;
// Auth and request tokens
private $authToken;
private $token;
/**
* Constructive constructor.
*
* @param string $email Google (apps) email.
* @param string $password Password and keys to the place, where your moneys are.
* @param string $debug False - no debug, true - yes debug.
*/
function __construct($email, $password, $debug = false) {
$this->debug = $debug;
$this->login($email, $password);
}
/**
* Umm. What's it called. De... destructor, ye!
*/
function __desctruct() {
curl_close($this->ch);
}
/**
* Utility function to output debugging info, if debugging is on or called statically.
*
* @param string $str Message to output.
*
* @return void
*/
function debug($str) {
if ((isset($this) && $this->debug) || !isset($this)) {
echo date('Y-m-d H:i:s ') . $str . "\n";
}
}
/**
* Heart. Performs HTTP request.
*
* @param string $url URL to request.
* @param string $post_fields If specified, indicates that this is POST request.
*
* @return mixed Returns anything that google returns.
*/
function request($url, $post_fields = false) {
$this->debug('Perform ' . ($post_fields ? 'POST' : 'GET') . ' request on ' . $url . ($post_fields ? $this->array_to_str($post_fields) : ''));
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);
if ($post_fields) {
if ($this->token) $post_fields['T'] = $this->token;
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($post_fields));
}
if ($this->authToken) {
curl_setopt($this->ch, CURLOPT_HTTPHEADER, Array('Authorization: GoogleLogin auth=' . $this->authToken));
}
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_HEADER, false);
$result = curl_exec($this->ch);
$this->ch_info = curl_getinfo($this->ch);
if ($this->ch_info['http_code'] != 200) {
$this->debug('Not HTTP 200 OK: ' . $this->ch_info['http_code']);
print_r($result);
}
return $result;
}
/**
* Utility method dumps array as string.
*
* @param array $array Array to convert.
*
* @return string Ex-array as a string.
*/
function array_to_str($array) {
$str = Array();
foreach ($array as $k=>$v) {
if ($k == 'Passwd') $v = '*****';
$str[] = "$k=$v";
}
return '{' . join(', ', $str) . '}';
}
/**
* Performs login, populates userinfo and retreives tokens.
*
* @param string $email Your google/google apps email.
* @param string $password Password for your account.
*
* @return void
*/
function login($email, $password) {
$post_fields = Array(
'accountType' => 'GOOGLE',
'Email' => $email,
'Passwd' => $password,
'source'=>'ReaderMigration',
'service'=>'reader',
);
$result = $this->request('https://www.google.com/accounts/ClientLogin', $post_fields);
$this->authToken = trim(substr(strstr($result, "Auth"), 5));
$this->debug('Got auth token: ' . $this->authToken);
$this->userInfo = $this->getUserInfo();
$this->debug('User info: ' . $this->array_to_str($this->userInfo));
$this->token = $this->getToken();
$this->debug('Got token: ' . $this->token);
}
/**
* Gets userinfo.
*
* @return object User info (who would think?)
*/
function getUserInfo() {
return json_decode($this->request('https://www.google.com/reader/api/0/user-info?output=json'));
}
/**
* Gets token for POST requests.
*
* @return string token
*/
function getToken() {
return trim($this->request('https://www.google.com/reader/api/0/token'));
}
/**
* Gets all subscriptions for current user.
*
* @return object something
*/
function getSubscriptions() {
return json_decode($this->request('https://www.google.com/reader/api/0/subscription/list?output=json'));
}
/**
* Gets all friends for current user.
*
* @return object something
*/
function getFriends() {
return json_decode($this->request('https://www.google.com/reader/api/0/friend/list?output=json'));
}
/**
* Gets friend groups for current user.
*
* @return object something
*/
function getFriendsGroups() {
return json_decode($this->request('https://www.google.com/reader/api/0/friend/groups?output=json'));
}
/**
* Gets friend access lists for current user.
*
* @return object something
*/
function getFriendsACL() {
return json_decode($this->request('https://www.google.com/reader/api/0/friend/acl?output=json'));
}
/**
* Gets all labels for current user.
*
* @return object list of labels.
*/
function getTags() {
// Disallowed characters in tag titles: "<>?&/\^
return json_decode($this->request('https://www.google.com/reader/api/0/tag/list?output=json'));
}
/**
* Edits item
*
* @param string $id Subscription id. Mostly 'feed/http...feed..addr'.
* @param string $title Title for this subscription.
* @param string $label Label name. For example, 'state/com.google/starred'.
* @param string $action 'subscribe', 'unsubscribe'.
*
* @return object something.
*/
function editSubscription($id, $title = false, $label = false, $action = 'subscribe') {
$post_fields = Array(
's' => $id,
'ac' => $action,
);
if ($title) $post_fields['t'] = $title;
if ($label) $post_fields['a'] = $label;
return json_decode($this->request('https://www.google.com/reader/api/0/subscription/edit?output=json', $post_fields));
}
/**
* Gets itemds for specified stream.
*
* @param string $what Stream name.
* @param number $limit How many items to fetch. Try not to specify 0. That can lead to many, many, many items :)
*
* @return object (something)
*/
function getItemIds($what, $limit = 1024) {
$this->debug('Fetching items tagged as "' . $what . '" (' . ($limit ? 'limiting to ' . $limit : 'no limit') . ')');
$result = json_decode($this->request('http://www.google.com/reader/api/0/stream/items/ids/?s=' . rawurldecode($what) . '&output=json&n=' . $limit . '&client=scroll'));
return $result;
}
/**
* Fetches items from a stream.
*
* @param string $what Stream name. For example 'user/-/state/com.google/starred' or 'user/-/state/com.google/broadcast'.
* @param number $limit Number How many items to fetch. If zero (or false [or an empty array:>]), fetches all (beware!).
*
* @return object (->items is an array with entries).
*/
function getItems($what, $limit = 1024, $exclude = Array()) {
$this->debug('Fetching items tagged as "' . $what . '" (' . ($limit ? 'limiting to ' . $limit : 'no limit') . ')');
$continuation = '';
$return = false;
$count = ($limit >= 100 || !$limit) ? 100 : $limit;
if ($exclude) {
if (!is_array($exclude)) $exclude = Array($exclude);
foreach ($exclude as $k=>$v) {
$exclude[$k] = "xt=" . rawurlencode($v);
}
}
while (true) {
$result = json_decode($this->request('http://www.google.com/reader/api/0/stream/contents/' . rawurlencode($what) . (strpos($what, '?') === false ? '?' : '&') . 'output=json&n=' . $count . '&c=' . $continuation . '&client=scroll' . ($exclude ? '&' . join('&', $exclude) : '')));
$this->debug('Got items: ' . count($result->items). ' pcs');
if (!$return) {
$return = $result;
} else {
foreach ($result->items as $item) {
$return->items[] = $item;
}
}
// Are we done?
$continuation = isset($result->continuation) ? $result->continuation : false;
if (!$continuation) {
break;
}
// Shall we decrease linmit in order not to fetch more than needed?
if ($limit && ($count + count($return->items) >= $limit)) {
$count = $limit - count($return->items);
}
if ($count == 0) break;
}
return $return;
}
/**
* Sets entry tag (category, label). Works for user's tags and com.google (system) states
*
* @param string $id Entry id in (for example, tag:google.com,2005:reader/item/71d15223cd83b85d)
* @param string $feed_id Feed's id, from which entry came (for example, feed/http://blog.martindoms.com/feed/)
* @param string $tag String Tag to assign to the entry (for example, user/-/state/com.google/starred or user/-/label/SuperLabel)
*
* @return void
*/
function setEntryTag($id, $feed_id, $tag) {
$post_fields = Array(
's' => $feed_id,
'a' => $tag,
'i' => $id
);
return json_decode($this->request('https://www.google.com/reader/api/0/edit-tag?output=json', $post_fields));
}
/**
* Removes entry tag (category, label). Works for user's tags and com.google (system) states
*
* @param string $id Entry id in (for example, tag:google.com,2005:reader/item/71d15223cd83b85d)
* @param string $feed_id Feed's id, from which entry came (for example, feed/http://blog.martindoms.com/feed/)
* @param string $tag String Tag to unassign from the entry (for example, user/-/state/com.google/starred or user/-/label/SuperLabel)
*
* @return void
*/
function removeEntryTag($id, $feed_id, $tag) {
$post_fields = Array(
's' => $feed_id,
'r' => $tag,
'i' => $id
);
return json_decode($this->request('https://www.google.com/reader/api/0/edit-tag?output=json', $post_fields));
}
/**
* Annotates (adds note) and shares (or not:) entry.
*
* @param string $url URL to share.
* @param string $annotation Note to add.
* @param string $title Title to assign to shared item.
* @param string $snippet Content (or portion of it) to show.
* @param bool $share If true, shares item to followers.
*
* @return object something
*/
function annotateEntry($url, $annotation, $title, $snippet, $share = false) {
$post_fields = Array(
'url' => $url,
'share' => $share ? 'true' : 'false',
'annotation' => $annotation,
'title' => $title,
'snippet' => $snippet,
);
return json_decode($this->request('https://www.google.com/reader/api/0/item/edit?output=json', $post_fields));
}
/**
* Mark all items in a stream as read. Defaults to mark-all-everywhere-whenever-as-read.
*
* @param string $what Stream ID
*
* @return object whatever
*/
function markAllAsRead($what = 'user/-/state/com.google/reading-list') {
$post_fields = Array(
's' => $what, // stream id
'ts' => number_format(microtime(true) * 1000000, 0, '.', ''), // Timestamp in microseconds
);
return json_decode($this->request('https://www.google.com/reader/api/0/mark-all-as-read?output=json', $post_fields));
}
/**
* Edit friend.
*
* @param string $userId User ID
* @param string $profileUrl Link to user's profile
* @param string $action 'addfollowing' to follow, 'removefollowing' to unfollow
*
* TODO: implement blocking unblocking?
* Unblock:
* action:removefollowers
* u:07332626230056918067
* pu:http://www.google.com/profiles/112639243179322210394
* g:21
*
* Block:
* action:addfollowers
* u:07332626230056918067
* pu:http://www.google.com/profiles/112639243179322210394
* g:21
*
* @return smthn
*/
function editFriend($u, $pu, $action) {
$post_fields = Array(
'u' => $u,
'pu' => $pu,
'action' => $action,
);
return json_decode($this->request('https://www.google.com/reader/api/0/friend/edit?output=json', $post_fields));
}
function peopleSearch($q) {
return json_decode($this->request('https://www.google.com/reader/api/0/people/search?output=json&q=' . rawurlencode($q)));
}
}