Reputation: 479
I'm currently writing a web application which traverses a users time line via the Twitter API. I have no problems getting the data or manipulating it. The issue I have is with speed. The Twitter API limits the number of tweets you can retrieve to 200 per page. Pagination is done via ID's by passing a param in (max_id) which was the last tweet you read on the previous page. Is there anyway anybody can think of to increase the speed at which I get these tweets? I am using the abraham oauth lib. My Code is below:
$twitteroauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth['oauth_token'], $oauth['oauth_token_secret']);
$tweets = $twitteroauth->get('statuses/user_timeline', array ( 'screen_name' => 'user_name', 'count' => 200));
// get first batch of tweets from api
foreach($tweets as $t)
{
$tweets_to_process[] = $t;
}
// get last id of tweet and set prev_id to 0
$last_id = $tweets_to_process[count($tweets_to_process)-1]->id_str;
$prev_id = 0;
$loop_num = 0;
// loop through pages whilst last page returned of api result does not equal last of last result
while($last_id != $prev_id && $loop_num < 4)
{
// get tweets
$tweets = $twitteroauth->get('statuses/user_timeline', array ( 'screen_name' => 'user_name', 'count' => 200, 'max_id' => $last_id));
// loop through tweets and add to array
foreach($tweets as $t)
{
$tweets_to_process[] = $t;
}
// set prev and last id
$prev_id = $last_id;
$last_id = $tweets_to_process[count($tweets_to_process)-1]->id_str;
$loop_num ++;
}
As you can see at the moment I have put a break counter in the while loop because looping through the max 3200 tweets is taking too long from a UX point of view.
Upvotes: 1
Views: 1427
Reputation: 479
Doesnt appear to be a more streamlined solution in this instance. Closing
Upvotes: 0
Reputation: 1263
To extend BAwebimax's suggestion.. You can download and locally cache tweets at a regular interval and then make calls to fetch new tweets beyond the 'since_id/max_id' at user logon. Old tweets don't change so you can pre-process those in advance. This would lead to far fewer calls and much less processing for the fewer new tweets when the user logs into your app.
....
Just noticed your comment.. if the scenario involves onetime use and there are no repeat users then the above would not be useful. You don't have many choices in that case.
Upvotes: 1
Reputation: 2677
The latest incarnation of the Twitter API seems to have been specifically made to reduce the constant strain of pulling that stuff every time from their servers. I'd recommend you extend your code to pull the twitter feeds on a time basis (via a cron/scheduled task) and cache the timeline entries locally. That way the manipulation you perform can be done much faster.
Upvotes: 2