简体   繁体   中英

Search new twitter with a specific hashtag form a stream with twitter4j

I need my java app to be updated on all the new tweet with a specific hashtag (let's say #test ) with twitter4j. Due to avoiding continuous polling on twitter REST API I'll gonna use Twitter Stream API.

Running this code I'm expecting to:

  1. Run the code
  2. Open a browser, go to twitter and post the a tweet that contains #test
  3. See the print on my app only for tweet with a certain hashcode

But.. I do not see anything in my app.. (SEE THE EDIT)

Someone can give me some advice?

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

EDIT:

After some errors fixed I'm reading something in the sys.out but.. it seems random stuffs after some errors:

[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.

And then some random tweets like this:

@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??????

Access to twitter firehose is only given to selected companies via resellers. So unless you are one of those companies, you want to use sample() instead of firehose . You should see some logs though. Are you sure your loggers are set up correctly?

EDIT: Read this section of the documentation and add a valid twitter4j.properties file. Note that you have to sign up with twitter to get an access token. Once you have a token and filled the config file, you can just include it in your classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM