Twitter4J:从用户那里获取超过 1 条推文

发布于 2024-12-07 02:55:38 字数 337 浏览 0 评论 0原文

我正在尝试获取关注者列表中每个用户的最近状态列表。我有以下内容可以让用户...

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

我能从中得到的只是 getStatus() 这是他们的最新状态。 getHomeTimeline() 也不够,因为我需要每个用户最近的推文列表。无论如何,我可以使用 Twitter4J 实现这一目标吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首 2024-12-14 02:55:38

我只是想自己找到这个答案。我使用 getUserTimeline 方法取得了相当大的成功。看起来您正在尝试查找朋友 ID 列表,因此下面的方法应该采用 long[] 并吐出所有用户状态。如果您想以这种方式查找用户,lookupUsers 还接受屏幕名称的 String[]。

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

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();
    }
}
草莓酥 2024-12-14 02:55:38

Alex 的答案很接近,但每个用户只能获得 100 条推文。以下内容将为您提供所有信息(或至少是 API 的最大限制):

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

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]);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文