【问题标题】:Twitter4j: Streamig API about a hashtag or queryTwitter4j:关于主题标签或查询的 Streamig API
【发布时间】:2018-07-10 09:20:49
【问题描述】:

我正在尝试使用 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。

【问题讨论】:

    标签: twitter streaming twitter4j


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      // 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);  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-14
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多