Skip to content Skip to sidebar Skip to footer

How To Get List Of All Twitter Followers Using Twitter4j?

I have been wondering if there is a way to access all the twitter followers list. We have tried using call to the REST API via twitter4j: public List getFriendList()

Solution 1:

You should definitely use getFollowersIDs. As the documentation says, this returns an array (list) of IDs objects. Note that it causes the list to be broken into pages of around 5000 IDs at a time. To begin paging provide a value of -1 as the cursor. The response from the API will include a previous_cursor and next_cursor to allow paging back and forth.

The tricky part is to handle the cursor. If you can do this, then you will not have the problem of getting only 20 followers.

The first call to getFollowersIDs will need to be given a cursor of -1. For subsequent calls, you need to update the cursor value, by getting the next cursor, as done in the while part of the loop.

long cursor =-1L;
        IDs ids;
        do {
            ids = twitter.getFollowersIDs(cursor);
            for(long userID : ids.getIDs()){
                friendList.add(userID);
            }
        } while((cursor = ids.getNextCursor())!=0 );

Here is a very good reference: https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/friendsandfollowers/GetFriendsIDs.java

Now, if the user has more than around 75000 followers, you will have to do some waiting (see Vishal's answer). The first 15 calls will yield you around 75000 IDs. Then you will have to sleep for 15 minutes. Then make another 15 calls, and so on till you get all the followers. This can be done using a simple Thread.sleep(time_in_milliseconds) outside the for loop.

Solution 2:

Just Change like this and try, this is working for me

try {
        Log.i("act twitter...........", "ModifiedCustomTabBarActivity.class");
        // final JSONArray twitterFriendsIDsJsonArray = new JSONArray();
        IDs ids = mTwitter.mTwitter.getFriendsIDs(-1);// ids// for (long id : ids.getIDs()) {do {
            for (long id : ids.getIDs()) {               


                String ID = "followers ID #" + id;
                String[] firstname = ID.split("#");
                String first_Name = firstname[0];
                String Id = firstname[1];

                Log.i("split...........", first_Name + Id);

                String Name = mTwitter.mTwitter.showUser(id).getName();
                String screenname = mTwitter.mTwitter.showUser(id).getScreenName();


  //            Log.i("id.......", "followers ID #" + id);//          Log.i("Name..", mTwitter.mTwitter.showUser(id).getName());//          Log.i("Screen_Name...", mTwitter.mTwitter.showUser(id).getScreenName());//          Log.i("image...", mTwitter.mTwitter.showUser(id).getProfileImageURL());


            }
        } while (ids.hasNext());

    } catch (Exception e) {
        e.printStackTrace();
    }

Solution 3:

Try This...

ConfigurationBuilderconfbuilder=newConfigurationBuilder();
    confbuilder.setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(secretToken)
            .setOAuthConsumerKey(TwitterOAuthActivity.CONSUMER_KEY)
            .setOAuthConsumerSecret(TwitterOAuthActivity.CONSUMER_SECRET);
    Twittertwitter=newTwitterFactory(confbuilder.build()).getInstance();

    PagableResponseList<User> followersList;

    ArrayList<String> list = newArrayList<String>();
    try
    {
        followersList = twitter.getFollowersList(screenName, cursor);

        for (inti=0; i < followersList.size(); i++)
        {
            Useruser= followersList.get(i);
            Stringname= user.getName();
            list.add(name);
            System.out.println("Name" + i + ":" + name);
        }

        listView.setAdapter(newArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , list));
        listView.setVisibility(View.VISIBLE);
        friend_list.setVisibility(View.INVISIBLE);
        post_feeds.setVisibility(View.INVISIBLE);
        twit.setVisibility(View.INVISIBLE);
    }

Solution 4:

This is a tricky one. You should specify whether you're using application or per user tokens and the number of users you're fetching followers_ids for.

You get just 15 calls per 15 minutes in case of an application token. You can fetch a maximum of 5000 followers_ids per call. That gives you a maximum of 75K followers_ids per 15 minutes. If any of the users you're fetching followers_ids for has over 75K followers, you'll get the rate_limit error immediately. If you're fetching for more than 1 user, you'll need to build strong rate_limit handling in your code with sleeps and be very patient.

The same applies for friends_ids.

I've not had to deal with fetching more than 75K followers/friends for a given user but come to think of it, I don't know if it's even possible anymore.

Post a Comment for "How To Get List Of All Twitter Followers Using Twitter4j?"