Friends and Followers in Spring Social

2019-08-14 03:56发布

问题:

I'm using spring-integration-twitter 4.1.6.RELEASE in my project. Using TwitterTemplate I was trying to get all the friends for the authenticated user.

So im using this method for it ,

friendsList = twitterTemplate.friendOperations().getFriends();

But in this case I'm only getting 20 friends as a default count. But I have 33 friends and I want to fetch them all. How can I do this.Also I'm a authenticated user when I call this method. In the TwitterTemplate there is no way to pass the count as a parameter. But the API says it will return 5000 users.

/** * Retrieves a list of up to 5000 users that the authenticated user follows. * Note that this method make multiple calls to Twitter's REST API (one call to get a list of the friend IDs and one call for every 100 friends). * If all you need is the friend IDs, consider calling getFriendIds() instead. * Or if you need only a subset of the user's friends, call UserOperations.getUsers() passing in the list of friend IDs you need. * @return a list of TwitterProfiles * @throws ApiException if there is an error while communicating with Twitter. * @throws MissingAuthorizationException if TwitterTemplate was not created with OAuth credentials. */ CursoredList<TwitterProfile> getFriends();

TwitterTemplate calls the twitter API to fetch data. So request redirects to Twitter API's https://api.twitter.com/1.1/friends/list.json access URL.

Twitter API description

At this time, results are ordered with the most recent following first — however, this ordering is subject to unannounced change and eventual consistency issues. Results are given in groups of 20 users and multiple “pages” of results can be navigated through using the next_cursor value in subsequent requests. See Using cursors to navigate collections for more information.

How can I achieve this???

回答1:

That is a good solution, but it won't return more than the first 100 friends. The method twitterTemplate.userOperations.getUsers(userIdArray) can only be used to return 100 users at a time. https://dev.twitter.com/rest/reference/get/users/lookup

A better solution is:

List<TwitterProfile> getAllFollowers() {
    CursoredList<Long> cursoredList;
    cursoredList = twitter.friendOperations().getFollowerIds();
    List<TwitterProfile> followers = getFollowers(cursoredList);
    return followers;
}

List<TwitterProfile> getFollowers(CursoredList<Long> cursoredList) {
    List<TwitterProfile> followers = new ArrayList<>();
    for(int i = 0; i< cursoredList.size(); i+=100){
        followers.addAll(getProfiles(cursoredList, i));
    }
    return followers;
}

List<TwitterProfile> getProfiles(CursoredList<Long> cursoredList, int start){
    int end = Math.min(start+100, cursoredList.size());
    long[] ids = cursoredList.subList(start, end).toArray(new long[0]);
    List<TwitterProfile> profiles = twitter.userOperations().getUsers(ids);
    return profiles;
}

And then call getAllFollowers(). Similar code can be used to get all Friends as well. Just change the call:

twitter.friendOperations.getFollowerIds() 

to

twitter.friendOperations.getFriendIds();


回答2:

Finally found the solution,

1) First you have to get authenticated user's all friend Ids list using friendOperations.

2) Then get the all the friends for the particular id list using userOperations.

Here is the example snippet,

List<org.springframework.social.twitter.api.TwitterProfile> friendsList;
CursoredList<Long> friendIdList;
long[] userIdArray;

friendIdList =  twitterTemplate.friendOperations().getFriendIds();
userIdArray = new long[friendIdList.size()];
for(int i=0; i<friendIdList.size(); i++)
    userIdArray[i] = friendIdList.get(i);
friendsList = twitterTemplate.userOperations().getUsers(userIdArray);


回答3:

To answer the original question how to navigate through with cursors, please consider the following.

You have to iterate through all the cursors and collect the results as follows:

    // ...
    CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
    ArrayList<TwitterProfile> allFriends = friends;
    while (friends.hasNext()) {
        friends = twitter.friendOperations().getFriendsInCursor(friends.getNextCursor());
        allFriends.addAll(friends);
    }
    // process allFriends...