forked from vishalchaudhary/acTwitterConversation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacTwitterConversation.php
357 lines (235 loc) · 7.54 KB
/
acTwitterConversation.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
<?php
/*
* Returns an array of Tweets for a Twitter conversation.
*
* This was written due to the official Twitter API endpoint "related_results"
* being depreciated in REST v1.1
*
* Written by Adrian Crepaz
* http://adriancrepaz.com/twitter_conversions_api
* https://github.com/adriancrepaz/acTwitterConversation
*
* Version: 0.1.1
*/
/*
* Define the types of replies to return.
*/
define('CONVERSATE_BEFORE', 1);
define('CONVERSATE_AFTER', 2);
define('CONVERSATE_BOTH', 3);
class acTwitterConversation {
/*
* The tweetId of the conversion to fetch.
*/
private $tweetId;
/*
* The array of tweets pulled from the conversation.
*/
private $tweets = array();
/*
* DOMNodeList list of HTML replies.
*/
private $replies;
/*
* Store an instance of the DomDocument extension.
*/
private $dom;
/*
* Stores the raw HTML contents from the Twitter Conversation page.
*/
private $html;
/*
* Stores an error message, occured when parsing/retriveing data.
*/
private $error;
/*
* Stores the type of data to be returned.
*/
private $methodType;
/*
* Current reply HTML, updated on each iteration.
*/
private $currentReply;
/*
* Sets up the DomDocument/libXML class.
*/
public function __construct(){
// Omit libxml errors, we don't have much control over HTML/formatting from Twitter.
libxml_use_internal_errors(true);
$this->dom = new DomDocument;
}
/*
* Attempt to fetch an array of a Tweets conversation.
*/
public function fetchConversion($tweetId, $method = 'data', $conversate = CONVERSATE_BOTH){
$this->html = null;
$this->tweetId = $tweetId;
$this->methodType = (is_string($method)) ? $method : get_class($method);
/*
* We cannot continue if there was a problem retrieving or parsing Twitter data.
*/
if($this->fetchDocument() === false){
return array('error' => true, 'message' => $this->error, 'tweets' => array());
}
/*
* We have a valid response (HTML), continue to parse what we need.
*/
if($this->replies->length > 0){
foreach($this->replies AS $node){
// First off, fetch the basic details. TweetID and "State".
$details = $this->fetchDetails($node);
if($details === false) continue;
/*
* Before parsing data, check if we need the tweet.
* $conversate can filter out certain types.
*/
if($conversate == CONVERSATE_BEFORE AND $details['state'] == 'after'){
// It's safe to break, tweets are in date/time order.
// No-more tweets are required.
break;
}
if($conversate == CONVERSATE_AFTER AND $details['state'] == 'before'){
// Not required, skip to the next reply.
continue;
}
switch($this->methodType){
case 'data':
// Tidy up the HTML node for parseData.
$this->currentReply = $this->fetchInnerHtml($node);
$details = array_merge($details, $this->parseData());
break;
/*
* Returns a list of tweets including data returned from the
* Twitter GET: statuses/show endpoint.
*
* Data retrieved via the TwitterOAuth Class, by abraham.
* https://github.com/abraham/twitteroauth
*/
case 'TwitterOAuth':
$details['tweet'] = $method->get('statuses/show', array('id' => $details['id']));
break;
}
$this->tweets[] = $details;
}
}
return array('error' => false, 'tweets' => $this->tweets);
}
/*
* Attempt to fetch the HTML and load a DomDocument.
*/
private function fetchDocument(){
// The tweetId of the post must be must numeric.
if(empty($this->tweetId) OR !is_numeric($this->tweetId)){
$this->error = 'Invalid tweetId specified.';
return false;
}
/*
* Attempt to fetch the HTML from Twitter, using the IE6 user agent.
*
* I've tested this with blank, and really rare/obscure UA's and
* still receive the same markup. I image this is Twitters "fallback"
* markup for non-modern browsers.
*/
$stream = stream_context_create(array(
'http' => array(
'header' => "Accept-language: en\r\nUser-Agent: MSIE 6.0\r\n"
)));
$this->html = @file_get_contents('https://mobile.twitter.com/string/status/' . $this->tweetId, false, $stream);
if($this->html === false){
// If we pass an invalid tweetId, Twitter gives us a 404 header.
if(strpos($http_response_header[0], '404 Not Found') !== false){
$this->error = 'Tweet not found. It may have been deleted.';
} else {
$this->error = 'Unable to fetch conversation from Twitter.';
}
return false;
}
// Load the HTML into the DomDocument, and verify it's somewhat valid.
if($this->dom->loadHTML($this->html) === false){
$this->error = 'Unable to parse HTML response.';
return false;
}
// Store the replies, if any, into our $replies variable.
$xpath = new DomXPath($this->dom);
$this->replies = $xpath->query("//table[@class='tweet ']");
$this->tweetId = intval($this->tweetId);
return true;
}
/*
* Fetches the tweetId and state of a tweet.
* A valid DOMElement must be specified.
*/
private function fetchDetails(DOMElement $node){
$href = $node->getAttribute('href');
if(!empty($href)){
$tweetId = null;
$href = explode('/', trim($href, '/'));
// Now using explode() as PHP < 5.3.0 doesn't support the $before_needle on strstr.
if(!empty($href[2])){
list($tweetId) = explode('?', $href[2]);
}
if(empty($tweetId) OR (!empty($tweetId) AND !is_numeric($tweetId))){
return false;
}
return array(
'id' => $tweetId,
'state' => ((intval($tweetId) < $this->tweetId) ? 'before' : 'after')
);
}
return false;
}
/*
* Build an array of retreived elements.
*/
public function parseData(){
$thumbnail = $this->retrieve('image');
return array(
'username' => $this->retrieve('username'),
'name' => $this->retrieve('name'),
'content' => $this->retrieve('content'),
'date' => $this->retrieve('date'),
'images' => array(
'thumbnail' => $thumbnail,
'large' => str_replace('_normal.', '.', $thumbnail)
)
);
}
/*
* Retrieves and tidies up a node's HTML content.
*/
private function fetchInnerHtml($node){
$innerHTML = '';
$children = $node->childNodes;
foreach($children AS $child){
$innerHTML .= $child->ownerDocument->saveXML($child);
}
$innerHTML = preg_replace('/[ \t]+/', ' ', preg_replace('/[\r\n]+/', "\n", $innerHTML));
return str_replace("\n", '', $innerHTML);
}
/*
* Fetches an element from the reply HTML using regular expressions.
*/
private function retrieve($field){
$expressions = array(
'name' => '/(\<strong class\="fullname"\>)(.*?)(\<\/strong\>)/i',
'username' => '/(\<span class\="username"\> \<span\>@\<\/span\>)(.*?)(\<\/span\>)/i',
'content' => '/(\<div class\="dir-ltr" dir\="ltr"\>)(.*?)(\<\/div\>)/i',
'date' => '/(\<td class\="timestamp"\>)(.*?)(\<\/td\>)/i',
'image' => '/(src\=")(.*?)("\/\>)/i'
);
if(!isset($expressions[$field])){
return false;
}
// Attempt to match our required element.
preg_match($expressions[$field], $this->currentReply, $matches, 0);
return (!empty($matches[2])) ? trim(strip_tags($matches[2])) : false;
}
/*
* Returns a string of the HTML to be parsed.
* This is untouched by the class, and is direct from Twitter.
*/
public function returnHTML(){
return $this->html;
}
}
?>