-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterAggregator.php
330 lines (221 loc) · 10.8 KB
/
twitterAggregator.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
<?php
/************************************
twitter aggregator
***********************************/
class twitterAggregator {
// our options array
private $option = array(
// twitter API consumer key, secret, and oath token and oauth secret
'consumer_key' => "",
'consumer_secret' => "",
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
// comma separated list of twitter handles to pull
'usernames' => "",
// set the number of tweets to show
'count' => 10,
// set an update interval (minutes)
'update_interval' => 10,
// set the cache directory name/path
'cache_dir' => 'cache',
// boolean, exclude replies, default true
'exclude_replies' => true,
// boolean, include retweets, default true
'include_rts' => true
);
// debug mode will display errors and stop script processing
public $debug = false;
// this property will hold the error array if one occurs
public $error = false;
// data property to contain entire fetched timelines
public $data = array();
// data array trimmed down to the requested count
public $data_trimmed = array();
// the main constructor function, loads settings if included
public function __construct( $options ) {
// if settings were passed to the constructor
if ( !empty( $options ) ) {
// merge the options passed to the constructor, into the object options property
$this->option = array_merge( $this->option, $options );
}
// check if we're in WordPress
if ( function_exists( 'wp_upload_dir' ) ) {
// get upload directory info
$upload_info = wp_upload_dir();
// set up some variables to store cache urls
$this->option['cache_dir'] = $upload_info['basedir'] . '/cache';
}
}
// get twitter timelines and return them
public function fetch() {
// if we've already got the data stored in a property (the request has already taken place)
if ( !empty( $this->data ) ) {
// return it
return $this->data;
} else {
// put together the cache file url for this set of usernames
$cache_file = $this->option['cache_dir'] . '/twitter-' . md5( $this->option['usernames'] ) . '.txt';
// if cache folder doesn't exist
if ( !file_exists( $this->option['cache_dir'] ) ) {
// make the cache directory
if ( !mkdir( $this->option['cache_dir'], '777', 1 ) ) {
// register an error array if there's trouble making the directory
$this->error( 'Failed to create cache directory.' );
}
}
// check if we have a cache file
if ( file_exists( $cache_file ) ) {
// refresh the cache if the cache file is older than our update_interval value ago
if ( filemtime( $cache_file ) < ( time() - ( $this->option['update_interval'] * 60 ) ) ) {
$cached = false;
} else {
$cached = true;
}
} else {
$cached = false;
}
// if we don't have the timeline cached, grab it again.
if ( $cached === false ) {
// split apart usernames
$usernames = explode( ",", $this->option['usernames'] );
// empty array to place results into
$response_final = array();
// loop through usernames
if ( !empty( $usernames ) ) {
foreach ( $usernames as $username ) {
// pull some statuses
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
// parameters to pass to twitter api
$query_args = array(
// the twitter handle we're looking up
'screen_name' => trim( $username ),
// don't include replies.
'exclude_replies' => $this->option['exclude_replies'],
// don't include replies.
'include_rts' => $this->option['include_rts'],
// request 5 times the number of records we need, since the api counts tweets before excluding replies and retrweets (if those are set)
'count' => ( $this->option['count'] * 5 )
);
// build the query string
$query = '?' . http_build_query( $query_args );
// use the get method
$method = 'GET';
// open up a twitter API object for us
$twitter = new TwitterAPIExchange( $this->option );
// execute response
$response = $twitter->setGetfield( $query )->buildOauth( $url, $method )->performRequest();
// convert the response from json to an array
$response_array = json_decode( $response );
// loop through response, and set up the array by date
if ( isset( $response_array->errors ) ) {
// output errors returned by the API if applicable.
return $this->error( $response_array->errors[0]->message );
} else {
// otherwise, loop through the response array, and build our own array keyed by timestamp
foreach ( $response_array as $result ) {
$date = strtotime( $result->created_at );
$response_final[ $date ] = $result;
}
}
}
// sort response array in reverse order by the key (tweet timestamp)
krsort( $response_final );
// write the cache file
if ( !file_put_contents( $cache_file, serialize( $response_final ) ) ) {
// return a write error
return $this->error( "Could not write cache file." );
}
} else {
// output 'no username' error if we didn't get any twitter handles to fetch.
return $this->error( "No usernames specified." );
}
} else {
// grab cache file if we have it
$response_final = unserialize( file_get_contents( $cache_file ) );
}
// set the data into an object property
$this->data = $response_final;
// trim the array down to the count requested in the 'count' option
$this->data_trimmed = array_slice( $response_final, 0, $this->option['count'] );
// return the tweets
return $this->data_trimmed;
}
}
// get the widget code
public function widget() {
$html = '<style>' . file_get_contents( __DIR__ . '/widget.css' ) . '</style>';
// get the timelines
$html .= $this->widget_unstyled();
// return widget with styles
return $html;
}
// get the widget code
public function widget_unstyled() {
// get the timelines
$this->fetch();
// output timeline
if ( !empty( $this->data_trimmed ) ) {
$html = '<div class="twitter-aggregator">';
// set up a counter to output a tweet number for each tweet as we display thme.
$tweet_count = 1;
// loop through the trimmed data
foreach ( $this->data_trimmed as $tweet ) {
// generate the code for each tweet
$html .= '<div class="twitter-aggregator-tweet tweet-' . $tweet_count . '">';
$html .= '<div class="twitter-aggregator-tweet-profile-pic"><img src="' . str_replace( 'http://', 'https://', $tweet->user->profile_image_url ) . '" alt="Tweet by @' . $tweet->user->screen_name . '"></div>';
$html .= '<h3 class="twitter-aggregator-tweet-profile-name"><a href="https://twitter.com/' . $tweet->user->screen_name . '">' . $tweet->user->screen_name . '</a></h3>';
$html .= '<div class="twitter-aggregator-tweet-time">' . $this->ago( $tweet->created_at ) . ' ago</div>';
$html .= '<div class="twitter-aggregator-tweet-text">' . $this->make_clickable( $tweet->text ) . '</div>';
$html .= '</div>';
// increment counter
$tweet_count++;
}
$html .= '</div>';
}
return $html;
}
// display the widget
public function display() {
print $this->widget();
}
// display the widget
public function display_unstyled() {
print $this->widget_unstyled();
}
// helper function to get a string representing the difference between two times in a human readible format
public function ago( $tm, $rcs = 0 ) {
if ( is_string( $tm ) ) $tm = strtotime( $tm );
$cur_tm = time();
$dif = $cur_tm - $tm;
$pds = array( 'second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade' );
$lngh = array( 1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600 );
for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
$no = floor( $no ); if( $no <> 1 ) $pds[$v] .='s';
$x=sprintf("%d %s ",$no,$pds[$v]);
if ( ( $rcs == 1 ) && ( $v >= 1 ) && ( ( $cur_tm - $_tm ) > 0 ) ) $x .= time_ago($_tm);
return $x;
}
// helper function to parse and make urls clickable.
public function make_clickable( $string ) {
$url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
$string = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string);
return $string;
}
// error function, will return an error, or
public function error( $message ) {
// set the error array into an object property
$this->error = array(
'error' => true,
'error_message' => $message
);
// check our error display setting to see if we need to display them
if ( $this->debug ) {
// display error array dump and end processing
print_r( $this->error );
die;
} else {
// otherwise return the error array
return $this->error;
}
}
}