2017-08-31 33 views
0

我有一个Twitter API的大问题。目前Twitter不支持使用相关回复提取Tweets。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); 
    } 
} 
} 

回答

0

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

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匹配,我将它们添加到线程中。

0

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

这是conversation/show/:ID - 所以让所有的答复鸣叫编号123你会打电话conversation/show/123

唯一的问题是,这个API被限制在Twitter的官方API密钥。

+0

我无法将其用于我的应用程序。是否有另一种可能性,至少得到推文的所有答复。为了我的目的,没有必要获得每个回复的全部关系。按照时间顺序列出所有回复就足够了。 –

+0

您可以使用此搜索。例如,执行搜索“@fabian”以获取已回复您的每个人。然后按'reply_id'过滤 –

+0

非常感谢您的帮助。我为这个问题发布了一个很好的解决方案。也许将来Twitter会改进他们的API。 –