How to get list of all twitter followers using twitter4j?

后端 未结 4 784
情书的邮戳
情书的邮戳 2021-01-16 15:01

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 Li         


        
相关标签:
4条回答
  • 2021-01-16 15:40

    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();
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-16 15:56

    Try This...

     ConfigurationBuilder confbuilder = new ConfigurationBuilder();
        confbuilder.setOAuthAccessToken(accessToken)
                .setOAuthAccessTokenSecret(secretToken)
                .setOAuthConsumerKey(TwitterOAuthActivity.CONSUMER_KEY)
                .setOAuthConsumerSecret(TwitterOAuthActivity.CONSUMER_SECRET);
        Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance();
    
        PagableResponseList<User> followersList;
    
        ArrayList<String> list = new ArrayList<String>();
        try
        {
            followersList = twitter.getFollowersList(screenName, cursor);
    
            for (int i = 0; i < followersList.size(); i++)
            {
                User user = followersList.get(i);
                String name = user.getName();
                list.add(name);
                System.out.println("Name" + i + ":" + name);
            }
    
            listView.setAdapter(new ArrayAdapter<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);
        }
    
    0 讨论(0)
  • 2021-01-16 15:56

    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.

    0 讨论(0)
提交回复
热议问题