如何在特定时间间隔(Twitter4J)获取特定人的推文?

时间:2016-03-12 11:34:59

标签: filter twitter4j

我想在最近一年收集一些特定人的推文。我正在使用Twitter4J,就像这样:

Paging paging = new Paging(i, 200);
    try {
        statuses = twitter.getUserTimeline("martinsuchan",paging);
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但是如何在特定时间间隔内过滤该用户的推文?

任何回答赞赏

1 个答案:

答案 0 :(得分:0)

您可以根据status.getCreatedAt()在本地过滤状态。例如:

try {
    int statusesPerPage = 200;
    int page = 1;
    String username = "username";
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, -1);

    Twitter twitter = new TwitterFactory().getInstance();
    Paging paging = new Paging(page, statusesPerPage);
    List<Status> statuses = twitter.getUserTimeline(username, paging);

    page_loop:
    while (statuses.size() > 0) {
        System.out.println("Showing @" + username + "'s home timeline, page " + page);
        for (Status status : statuses) {
            if (status.getCreatedAt().before(cal.getTime())) {
                break page_loop;
            }
            System.out.println(status.getCreatedAt() + " - " + status.getText());
        }
        paging = new Paging(++page, statusesPerPage);
        statuses = twitter.getUserTimeline(username, paging);
    }
} catch (TwitterException te) {
    te.printStackTrace();
}