春天社交中的朋友和追随者

时间:2015-08-03 18:37:22

标签: spring twitter spring-integration spring-social-twitter

我在我的项目中使用了spring-integration-twitter 4.1.6.RELEASE。使用TwitterTemplate我试图让所有知道用户的朋友。

所以即时使用此方法,

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

但在这种情况下,我只有20个朋友作为默认计数。但我有33个朋友,我想把它们全部拿走。我怎么能这样做。当我调用这个方法时,我也是一个经过身份验证的用户。在TwitterTemplate中,无法将计数作为参数传递。但API表示它将返回5000个用户。

/** * 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调用twitter API来获取数据。因此,请求重定向到Twitter API的https://api.twitter.com/1.1/friends/list.json访问网址。

Twitter API description

  

目前,结果是按照最新的后续顺序排序的 - 但是,此顺序可能会受到突击更改和最终一致性问题的影响。结果以20个用户的组给出,并且可以通过在后续请求中使用next_cursor值来导航结果的多个“页面”。有关详细信息,请参阅使用游标导航集合。

我怎样才能实现这一点?

3 个答案:

答案 0 :(得分:1)

这是一个很好的解决方案,但它不会比前100位朋友回报更多。 twitterTemplate.userOperations.getUsers(userIdArray)方法一次只能用于返回100个用户。 https://dev.twitter.com/rest/reference/get/users/lookup

更好的解决方案是:

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;
}

然后调用getAllFollowers()。类似的代码也可用于获取所有朋友。只需更改通话:

twitter.friendOperations.getFollowerIds() 

twitter.friendOperations.getFriendIds();

答案 1 :(得分:0)

终于找到了解决方案,

1)首先,您必须使用friendOperations获取经过身份验证的用户的所有朋友ID列表。

2)然后使用userOperations获取特定id列表的所有朋友。

以下是示例代码段

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);

答案 2 :(得分:0)

要回答原始问题如何使用游标进行导航,请考虑以下内容。

您必须遍历所有游标并按如下方式收集结果:

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