使用特定的标签搜索新的twitter,形成一个使用twitter4j的流

时间:2013-07-02 11:53:44

标签: java twitter twitter4j

我需要使用twitter4j在所有新推文上更新我的java应用程序,并使用特定的主题标签(比方说#test)。由于避免在Twitter REST API上进行连续轮询,我将使用Twitter Stream API。

运行此代码我期望:

  1. 运行代码
  2. 打开浏览器,转到Twitter并发布包含#test
  3. 的推文
  4. 仅针对具有特定哈希码的推文查看我的应用上的打印
  5. 但是..我的应用程序中没有看到任何内容..(参见编辑)

    有人可以给我一些建议吗?

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey(CONSUMER_KEY)
      .setOAuthConsumerSecret(CONSUMER_SECRET)
      .setOAuthAccessToken(ACCESS_TOKEN)
      .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    
    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getUser().getId() + " - " + status.getText());
        }
        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Post Deletado:" + statusDeletionNotice.getStatusId());
        }
        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.err.println("Limitação:" + numberOfLimitedStatuses);
        }
        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }
        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }
        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    
    List<String> queries = new ArrayList<String>();
    queries.add("#test");
    
    twitterStream.addListener(listener);
    twitterStream.firehose(20);
    
    String[] trackQueries = (String[]) queries.toArray(new String[queries.size()]);
    
    FilterQuery filterQuery = new FilterQuery();
    twitterStream.filter(filterQuery.track(trackQueries));
    

    编辑:

    修复了一些错误之后,我正在读取sys.out中的内容但是......在出现一些错误之后似乎是随机的东西:

    [Tue Jul 02 14:58:30 CEST 2013]Establishing connection.
    [Tue Jul 02 14:58:30 CEST 2013]Establishing connection.
    [Tue Jul 02 14:58:31 CEST 2013]Connection established.
    [Tue Jul 02 14:58:31 CEST 2013]Receiving status stream.
    [Tue Jul 02 14:58:31 CEST 2013]Connection established.
    [Tue Jul 02 14:58:31 CEST 2013]Receiving status stream.
    [Tue Jul 02 14:58:31 CEST 2013]Stream closed.
    [Tue Jul 02 14:58:31 CEST 2013]Stream closed.
    [Tue Jul 02 14:58:31 CEST 2013]Waiting for 250 milliseconds
    Stream closed.
    Relevant discussions can be found on the Internet at:
        http://www.google.co.jp/search?q=a8fd061d or
        http://www.google.co.jp/search?q=00070a0c
    TwitterException{exceptionCode=[a8fd061d-00070a0c a8fd061d-0007099d], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
        at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:199)
        at twitter4j.StatusStreamImpl.next(StatusStreamImpl.java:57)
        at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.run(TwitterStreamImpl.java:478)
    Caused by: java.io.IOException: the end of the stream has been reached
        at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:88)
        ... 2 more
    Stream closed.
    Relevant discussions can be found on the Internet at:
        http://www.google.co.jp/search?q=a8fd061d or
        http://www.google.co.jp/search?q=00070a0c
    TwitterException{exceptionCode=[a8fd061d-00070a0c a8fd061d-0007099d], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
        at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:199)
        at twitter4j.StatusStreamImpl.next(StatusStreamImpl.java:57)
        at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.run(TwitterStreamImpl.java:478)
    Caused by: java.io.IOException: the end of the stream has been reached
        at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:88)
        ... 2 more
    [Tue Jul 02 14:58:31 CEST 2013]Unhandled event: {"disconnect":{"code":7,"stream_name":"urbanspirit5-statuses1734930","reason":"admin logout"}}
    [Tue Jul 02 14:58:32 CEST 2013]Establishing connection.
    [Tue Jul 02 14:58:33 CEST 2013]Connection established.
    [Tue Jul 02 14:58:33 CEST 2013]Receiving status stream.
    

    然后是一些像这样的随机推文:

    @markinhos_jm - 300044892 - CRAZY PARTY AGUARDEM!!!
    @bindibba7 - 511548205 - ??????? .. ??????? = ?? ??? ????
    @Tamirmdty - 1521605059 - Salah ngetik
    @PAOLOves_you - 222755754 - Hirap ah. Text, Chat FB at Tweet? SIYET! #MedyoAZARitu
    @agokichi_mmmm - 793252368 - ???DVD??????
    

1 个答案:

答案 0 :(得分:0)

仅通过经销商向选定的公司提供对twitter firehose的访问权限。因此,除非您是其中一家公司,否则您希望使用sample()代替firehose。你应该看到一些日志。您确定您的记录仪设置正确吗?

编辑:阅读文档的this section并添加有效的twitter4j.properties文件。请注意,您必须使用twitter注册才能获得访问令牌。获得令牌并填写配置文件后,您可以将其包含在类路径中。