6

How can I obtain the friends list of a friend or follower using Twitter4J?

Using getFriendsId(), I'm only able to retrieve the friend's/follower's list of that current user which is authenticated. What I want is to obtain the list of friends of a follower or friend of the authenticated user.

Jasper
  • 2,166
  • 4
  • 30
  • 50
sandeepsharat
  • 97
  • 1
  • 3
  • 10

7 Answers7

6

This will show the name of your friend's followers.

      User u1 = null ;
      long cursor = -1;
      IDs ids;
      System.out.println("Listing followers's ids.");
      do {
              ids = twitter.getFollowersIDs("username", cursor);
          for (long id : ids.getIDs()) {
              System.out.println(id);
              User user = twitter.showUser(id);
              System.out.println(user.getName());
          }
      } while ((cursor = ids.getNextCursor()) != 0);
vikiiii
  • 9,246
  • 9
  • 49
  • 68
  • Warning: there is a good chance that this will get you rate limited the first time you try it. – dranxo Nov 12 '12 at 23:48
  • I don't understand why are you keeping on calling the `getFollowersIds` method. Is it for the paging of the responses? So the previous answer by sandeepsharat would return only a subset of all friends? – 5agado Jul 13 '13 at 09:15
  • 1
    @rcompton Can't we overcome this limit issue? – TheLittleNaruto Feb 10 '14 at 13:25
3

You only need to do this:

Twitter twitter = mTwitterApp.getTwitterInstance();
long cursor = -1;
List<User> users=twitter.getFriendsList(mTwitterApp.getUserID(), cursor);

Here users is a list users who are your friends(you are following them). mTwitterApp.getUserID() is your login useris which is a long value.

Mohit Verma
  • 3,025
  • 1
  • 20
  • 36
2
long lCursor = -1;
IDs friendsIDs = twitter.getFriendsIDs(userID, lCursor);
System.out.println(twitter.showUser(userID).getName());
System.out.println("==========================");
do
{
  for (long i : friendsIDs.getIDs())
   {
       System.out.println("follower ID #" + i);
       System.out.println(twitter.showUser(i).getName());
   }
}while(friendsIDs.hasNext());
kapex
  • 28,903
  • 6
  • 107
  • 121
sandeepsharat
  • 97
  • 1
  • 3
  • 10
  • 4
    Didn't this code attack repeatedly to the API and you may be banned by exceed rate limits? – rubdottocom Feb 23 '13 at 13:23
  • This will exceed rate limits very easily (dev.twitter.com/rest/public/rate-limiting). Use getFriendsList instead of getFriendsIDs. Check my answer on this page. – abhijit.mitkar May 19 '15 at 09:07
1
         PagableResponseList<User> friendlist= twitter.getFriendsList(user.getScreenName(), -1);

         int sizeoffreindlist= friendlist.size();

         for(int i=0;i<sizeoffreindlist;i++)
         {
             System.out.println(friendlist.get(i));
         }

It will provide you a list of 20 friends as the default limit is 20

Raj
  • 22,346
  • 14
  • 99
  • 142
1

This code works! (without exceeding rate limits). Referred twitter4j documentation and other answers on StackOverflow.

    try {
            // get friends
            long cursor = -1;
            PagableResponseList<User> pagableFollowings;
            do {
                pagableFollowings = twitter.getFriendsList(twitter.getId(), cursor);
                for (User user : pagableFollowings) {
                    listFriends.add(user); // ArrayList<User>
                }
            } while ((cursor = pagableFollowings.getNextCursor()) != 0);

            // get followers
            cursor = -1;
            PagableResponseList<User> pagableFollowers;
            do {
                pagableFollowers = twitter.getFollowersList(twitter.getId(), cursor);
                for (User user : pagableFollowers) {
                    listFollowers.add(user); // ArrayList<User>
                }
            } while ((cursor = pagableFollowers.getNextCursor()) != 0);

        } catch (TwitterException e) {
            printError(e);
        }
abhijit.mitkar
  • 246
  • 2
  • 7
1

You can use

twitter.getFollowersIDs("username", cursor);

http://twitter4j.org/javadoc/twitter4j/api/FriendsFollowersResources.html#getFollowersIDs-java.lang.String-long- which returns only 5000 user not all users. Also it is limited 15 times in 15 minutes.(https://dev.twitter.com/rest/reference/get/friends/ids)

Also, you can use,

twitter.getFollowersList("username", cursor);

http://twitter4j.org/javadoc/twitter4j/api/FriendsFollowersResources.html#getFollowersList-java.lang.String-long- which is also limited with 20 user. Also it is limited 15 times in 15 minutes for user auth, 30 times in 15 minutes for app auth (https://dev.twitter.com/rest/reference/get/friends/list)

For unlimited access, you can look at https://gnip.com/ or whitelisted user access of twitter.

hakansel
  • 33
  • 8
0

What about something like get Friends List? https://dev.twitter.com/docs/api/1.1/get/friends/list

According to the docs:

Returns a cursored collection of user objects for every user the specified user is following (otherwise known as their "friends").

There is an interface for this in twitter4j.api, but I can't figure out how to use it:

PagableResponseList<User> getFriendsList(String screenName, long cursor) throws TwitterException;
Brian Porter
  • 529
  • 3
  • 19