Skip to content

Instantly share code, notes, and snippets.

@ABeltramo
Created January 12, 2017 12:44
Show Gist options
  • Save ABeltramo/412da46290395e1b4edd9f9ea7b8db9e to your computer and use it in GitHub Desktop.
Save ABeltramo/412da46290395e1b4edd9f9ea7b8db9e to your computer and use it in GitHub Desktop.
Twitter4J Friends
private ArrayList<Long> getAllFriendsIDs(String user) throws TwitterException{
ArrayList<Long> tmp = new ArrayList<>();
ArrayList<Long> friends = new ArrayList<>();
Long cursor = -1L;
IDs ids = null;
checkAPICall("/users/show/:id");
User curUser = _tw.showUser(user);
// Take the min number of user
if(curUser.getFollowersCount() < curUser.getFriendsCount()){
do {
checkAPICall("/followers/ids");
ids = _tw.getFollowersIDs(user, cursor, maxUserPerPage);
for(Long id : ids.getIDs()){
tmp.add(id);
}
cursor = ids.getNextCursor();
} while(ids.hasNext());
}
else{
do {
checkAPICall("/friends/ids");
ids = _tw.getFriendsIDs(user, cursor, maxUserPerPage);
for(Long id : ids.getIDs()){
tmp.add(id);
}
cursor = ids.getNextCursor();
} while(ids.hasNext());
} // Finish this I have a list of follower or friends
// I have to check if they are also on the other list
for(Long id : tmp){
checkAPICall("/friendships/show");
Relationship rel = _tw.showFriendship(curUser.getId(),id);
if(rel.isSourceFollowedByTarget() && rel.isSourceFollowingTarget()){
friends.add(id);
}
}
return friends;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment