Search code examples
javatwittertwitter4j

Get Second Level features of social network through Twitter Api


I am trying to get second level features of my twitter personal network using twitter4j library. When I refer to second level features I mean the followers of my followers, the followees of my followees etc.

I've started with my followees first using the following code:

try{
    twitter = tf.getInstance();
    long cursor = -1;
    IDs ids = twitter.getFriendsIDs(cursor);

    long[] id = ids.getIDs();
    ResponseList<User> users = twitter.lookupUsers(id); 
}
catch (Exception e) {
    logger.warn("Error {}",e.getLocalizedMessage());
    e.printStackTrace();
}

Using the above I am getting my friends. But I am struggling to get the friends of friends. I've read that giving my twitter keys I am authorized to get second level features but not further that that.

Any help is appreciated.


Solution

  • Let me write a sample below: First block is for finding followers of a particular tweet user.
    
        long lCursor = -1, nextCursor = -1;
        IDs friendsIDs = null;
    
        followerIDListOfOriginalTweeter = new ArrayList<>();
    
                do
                {
                    lCursor = nextCursor;
                    try
                    {
                        friendsIDs = twitterObj.getFollowersIDs(originalTweetUserId, lCursor);
                    }
                    catch (Exception ex)
                    {
                        logWriter.Write(ex);
                    }
    
                    nextCursor = friendsIDs.getNextCursor();
    
                 } while (nextCursor != 0);
    
    
    The second block is for finding followers of followers:
    
        IDs friendsIDs = null;
        long lCursor = -1, nextCursor = -1;
    
        try
            {
                do
                  {
                        followers = new Followers();
    
                        nextCursor = -1;
    
                        do
                        {
                            lCursor = nextCursor;
    
                            try
                            {
                    friendsIDs = twitterObj.getFollowersIDs(followers.currentUserId, lCursor);
                            }
                            catch (Exception ex)
                            {
                              logWriter.Write(ex);
    
                            }
    
                            for (long followerId : friendsIDs.getIDs())
                            {                            
                               system.out.println(followerId);
                            }
    
                            nextCursor = friendsIDs.getNextCursor();
    
                        } while (nextCursor != 0);
    
                } while (rsForSelectNotCompletedRetweetsQuery.next());