Twitter4j:关于主题标签或查询的 Streamig API

Twitter4j: Streamig API about a hashtag or query

我正在尝试使用 twitter4j API 获取特定主题的推文流。

这是我的代码:

TwitterStream twitterStream = inizialize();

    StatusListener listener = new StatusListener(){
        public void onStatus(Status status) {
            System.out.println(status.getUser().getName() + " ====> " + status.getText());
        }
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onStallWarning(StallWarning warning) {
            // TODO Auto-generated method stub

        }
    };


    FilterQuery filterQuery = new FilterQuery("GAME");
    twitterStream.addListener(listener);
    twitterStream.filter(filterQuery);
    twitterStream.sample(); // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.

}

推文流运行良好,但我无法设置任何查询。所以,我想知道,是否有可能做我想做的事? 当然,inizialize() returns 一个配置了有效 oauth 令牌的 twitterStream。

您需要手动过滤来自流的状态。例如,如果您只想显示包含 'vanilla' 的推文,那么您可以在 onStatus:

中这样处理
public void onStatus(Status status) {
    String statusText = status.getText();
    if (statusText.toLowerCase().contains("vanilla")) {
        System.out.println(status.getUser().getName() + " ====> " + statusText);
    }
}
// Expecting a String[] of topics to track:
filterQuery.track(keywords);

// Within a bounding box of geo-coordinates:
filterQuery.locations(new double[][] {{lng1, lat1}, {lng2, lat2}});  

// Specifies a language to track:
filterQuery.language("en");

// Number of previous statuses to stream before transitioning to the live stream:
filterQuery.count(10);