推特回复之间的关系

Relation between Twitter replies

我对 Twitter API 有很大的疑问。目前 Twitter 不支持获取推文及其相关回复。

我想抓取时间线的提及并将它们与他们的回复相关联。

直到这一步一切都很好。现在我的问题。 我还想添加回复的子回复,以获得提及和回复之间的完整关系。

目前我获取时间轴并将结果拆分为提及和回复。

public void fetchTwitterTimeline(long sinceId) {
 try {
   Paging timelinePaging = new Paging();

   if (sinceId > 0) {
     timelinePaging.setSinceId(sinceId);
   }
   LOG.debug("Fetching Twitter Timeline");
   ResponseList<Status> statusResponseList = twitterClient.getMentionsTimeline(timelinePaging);
   assignTwitterStatusResponse(statusResponseList);
 } catch(TwitterException e){
   e.getStackTrace();
   System.out.println(e);
   LOG.error("Could not fetch Twitter Timeline: {}", e);
  }
}

private void assignTwitterStatusResponse(ResponseList<Status> statusResponseList) {
 for (Status status : statusResponseList) {
   if (status.isRetweet()) {
     continue;
   }

   if (status.getInReplyToStatusId() > 0) {
     replies.add(status);
   } else {
     mentions.add(status);
   }
 } 
}

有一个 API 调用来获取对推文的回复。

它是 conversation/show/:ID - 所以要获得对推文 ID 123 的所有回复,您需要调用 conversation/show/123

唯一的问题是这个 API 仅限于 Twitter 的官方 API 密钥。

非常感谢您的回复。 现在我也有一个很好的解决方案给那些有同样问题的人。

public List<Status> fetchTwitterThread(long tweetId) throws TwitterException {
   Paging timelinePaging = new Paging();
   timelinePaging.setSinceId(tweetId);

   ResponseList<Status> statusResponseList = twitterClient.getMentionsTimeline(timelinePaging);
   statusResponseList.addAll(twitterClient.getHomeTimeline(timelinePaging));

   List<Status> thread = new ArrayList<>();
   thread.add(getStatusById(tweetId)); // Add root status

   fetchTwitterThread(tweetId, statusResponseList, thread);

   return thread;
}

private void fetchTwitterThread(long parentId, ResponseList<Status> statusResponseList, List<Status> thread) {
  for (Status status : statusResponseList) {
    if (status.getInReplyToStatusId() == parentId) {
      thread.add(status);
      fetchTwitterThread(status.getId(), statusResponseList, thread);
    }
  }
}

我有两种方法。如果您想节省一些 API 次通话,这将是必需的。 在第一步中,我从请求的 ID 中获取了 mentionsTimeline 和 hometimeline。这对于您自己的推文和回复是必要的。

之后我实现了第二种递归方法。我遍历 responseList,如果一个状态 (inReplyToStatusId) 与 parentId 匹配,我将它们添加到线程中。