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

时间:2017-05-27 15:46:26

标签: twitter streaming twitter4j

我尝试使用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()返回一个配置了有效oauth标记的twitterStream。

2 个答案:

答案 0 :(得分:1)

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

public void onStatus(Status status) {
    String statusText = status.getText();
    if (statusText.toLowerCase().contains("vanilla")) {
        System.out.println(status.getUser().getName() + " ====> " + statusText);
    }
}

答案 1 :(得分:1)

// 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);