开发者

Twitter4J: Get more than 1 tweet from a user

开发者 https://www.devze.com 2023-04-08 00:46 出处:网络
I\'m trying to get a list of recent statuses from each user on a persons list of followers. I\'ve got the following to get the users...

I'm trying to get a list of recent statuses from each user on a persons list of followers. I've got the following to get the users...

 IDs list = twitter.getFriendsIDs(0);
    for(long ID : list.getIDs()){
        twitter4j.User TW_user = twitter.showUser(ID);
}

All I can get from this is getStatus() which is their most recent status. getHomeTimeline() is also insufficient as I need a list of recent tweets from each开发者_如何学编程 user. Is there anyway I can achieve this using Twitter4J?


I was just trying to find this answer myself. I had decent success using the getUserTimeline method. Looks like you're trying to look up a list of friend IDs, so this method below should take the long[] and spit out all the user statuses. lookupUsers also accepts a String[] of screen names if you want to look users up that way instead.

public static void lookupUsers(long[] usersList) {
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        ResponseList<User> users = twitter.lookupUsers(usersList);
        Paging paging = new Paging(1, 100);
        List<Status> statuses;

        for (User user : users) {
            statuses = twitter.getUserTimeline(user.getScreenName(), paging);
            System.out.println("\nUser: @" + user.getScreenName());
            for (Status s : statuses) {
                System.out.println(s.getText());
            }

        }
    } catch (TwitterException e) {
        e.printStackTrace();
    }
}


Alex's answer is close, but will only get you 100 tweets per user. The following will get you all (or at least the API's max limit):

 IDs list = twitter.getFriendsIDs(0);
 for(long ID : list.getIDs()) {

    Status[] tweets = getAllTweets(twitter, ID);
    System.out.println(ID + ": " + tweets.length);
 }

 Status[] getAllTweets(Twitter twitter, long userId)
 {
    int pageno = 1;
    List statuses = new ArrayList();

    while (true)
    {
      try
      {

        int size = statuses.size();
        Paging page = new Paging(pageno++, 100);
        statuses.addAll(twitter.getUserTimeline(userId, page));
        if (statuses.size() == size)
          break;
      }
      catch (TwitterException e)
      {

        e.printStackTrace();
      }
    }

    return (Status[]) statuses.toArray(new Status[0]);
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消