Sino Thomas
Sino Thomas

Reputation: 141

How to get full list of Twitter followers using new API 1.1

I am using this https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=sitestreams&count=5000 to list the Twitter followers list, But I got only list of 200 followers. How to increase the list of Twitter followers using the new API 1.1?

Upvotes: 4

Views: 23103

Answers (3)

Mike Barwick
Mike Barwick

Reputation: 5367

Here's how I run/update full list of follower ids on my platform. I'd avoid using sleep() like @aphoe script. Really bad to keep a connection open that long - and what happens if your user has 1MILL followers? You going to keep that connection open for a week? lol If you must, run cron or save to redis/memcache. Rinse and repeat until you get all the followers.

Note, my code below is a class that's run through a cron command every minute. I'm using Laravel 5.1. So you can probably ignore a lot of this code, as it's unique to my platform. Such as the TwitterOAuth (which gets all oAuths I have on db), TwitterFollowerList is another table and I check if an entry already exists, TwitterFollowersDaily is another table where I store/update total amount for the day for the user, and TwitterApi is the Abraham\TwitterOAuth package. You can use whatever library though.

This might give you a good sense of what you might do the same or even figure out a better way. I won't explain all the code, as there's a lot happening, but you should be able to guide through it. Let me know if you have any questions.

/**
 * Update follower list for each oAuth
 *
 * @return response
 */
public function updateFollowers()
{
    TwitterOAuth::chunk(200, function ($oauths) 
    {
        foreach ($oauths as $oauth)
        {
            $page_id = $oauth->page_id;
            $follower_list = TwitterFollowerList::where('page_id', $page_id)->first();

            if (!$follower_list || $follower_list->updated_at < Carbon::now()->subMinutes(15))
            {
                $next_cursor = isset($follower_list->next_cursor) ? $follower_list->next_cursor : -1;
                $ids = isset($follower_list->follower_ids) ?  $follower_list->follower_ids : [];

                $twitter = new TwitterApi($oauth->oauth_token, $oauth->oauth_token_secret);
                $results = $twitter->get("followers/ids", ["user_id" => $page_id, "cursor" => $next_cursor]);

                if (isset($results->errors)) continue;

                $ids = $results->ids; 

                if ($results->next_cursor !== 0)
                {
                    $ticks = 0;

                    do 
                    {
                        if ($ticks === 13) 
                        {
                            $ticks = 0;
                            break;
                        }

                        $ticks++;

                        $results = $twitter->get("followers/ids", ["user_id" => $page_id, "cursor" => $results->next_cursor]);
                        if (!$results) break;

                        $more_ids = $results->ids;
                        $ids = array_merge($ids, $more_ids);
                    }
                    while ($results->next_cursor > 0);
                }

                $stats = [
                    'page_id' => $page_id,
                    'follower_count' => count($ids),
                    'follower_ids' => $ids,
                    'next_cursor' => ($results->next_cursor > 0) ? $results->next_cursor : null,
                    'updated_at' => Carbon::now()
                ];

                TwitterFollowerList::updateOrCreate(['page_id' => $page_id], $stats);

                TwitterFollowersDaily::updateOrCreate([
                        'page_id' => $page_id, 
                        'date' => Carbon::now()->toDateString()
                    ],
                    [
                        'page_id' => $page_id,
                        'date' => Carbon::now()->toDateString(),
                        'follower_count' => count($ids),
                    ]
                );
                continue;
            }
        }
    });
}

Upvotes: 0

eNeF
eNeF

Reputation: 3280

You can't fetch more than 200 at once... It was clearly stated on the documentation where count:

The number of users to return per page, up to a maximum of 200. Defaults to 20.

you can somehow make it via pagination using

"cursor=-1" #means page 1, "If no cursor is provided, a value of -1 will be assumed, which is the first “page."

Upvotes: 0

Pedro
Pedro

Reputation: 691

You must first setup you application

<?php
$consumerKey = 'Consumer-Key';
$consumerSecret = 'Consumer-Secret';
$oAuthToken = 'OAuthToken';
$oAuthSecret = 'OAuth Secret';

# API OAuth
require_once('twitteroauth.php');

$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);

You can download the twitteroauth.php from here: https://github.com/elpeter/pv-auto-tweets/blob/master/twitteroauth.php

Then

You can retrieve your followers like this:

$tweet->get('followers/ids', array('screen_name' => 'YOUR-SCREEN-NAME-USER'));

If you want to retrieve the next group of 5000 followers you must add the cursor value from first call.

$tweet->get('followers/ids', array('screen_name' => 'YOUR-SCREEN-NAME-USER', 'cursor' => 9999999999));

You can read about: Using cursors to navigate collections in this link: https://dev.twitter.com/docs/misc/cursoring

Upvotes: 6

Related Questions