Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change keywords on twitter stream api using twitter4j?

I am using twitter4j to connect to Stream API.

I understand that from this post, Change Twitter stream filter keywords without re-opening stream, there is no way to change keywords while the connection is open. I have to disconnect and change the filter predicate and reconnect it.

I would like to know if there is any code sample that would allow me to disconnect it, change the keywords and reconnect it?

Currently, I tried to do this in the StatusListener under onStatus() where after an X amount of time has passed, it will change the keyword to "juice". But there is no method for me to close the connection and reconnect to Stream API.

if (diff>=timeLapse) {
   StatusListener listener = createStatusListener();
   track = "juice";
   twitterStream = new TwitterStreamFactory().getInstance();
   twitterStream.addListener(listener);
   FilterQuery fq = new FilterQuery();
   fq.track(new String[] {track});
   startTime=System.currentTimeMillis();
   twitterStream.filter(fq);
}
like image 568
ktlim Avatar asked Nov 26 '12 09:11

ktlim


People also ask

What is the difference between Twitter search API and streaming API?

With a specific keyword, you can typically only poll the last 5,000 tweets per keyword. Unlike Twitter's Search API where you are polling data from tweets that have already happened, Twitter's Streaming API is a push of data as tweets happen in near real-time.

How do you update twitter live?

Tap Live at the bottom selector. Fill in an optional description that will appear as a Tweet, and a location if desired. Tap Go live. Your live broadcast, with description and location (if added), will appear in a Tweet in your followers' timelines and on your profile.

How do I filter twitter data?

In order to filter Twitter data you need to specify a 'query' (if you use recent search endpoint) or create a 'rule' (if you use the filtered stream endpoint). These queries and rules are a combination of operators that help you specify the data you need. If you have used our v1.


2 Answers

You need to cleanUp() the stream first and then open a new stream with filter(FilterQuery) method to change track terms.

like image 58
YAMAMOTO Yusuke Avatar answered Oct 14 '22 18:10

YAMAMOTO Yusuke


You can do this by simply calling Filter(query) again, no need to call cleanUp() as calling filter(query) does this for you. Here's how I do this, and there is no need to stop/restart the stream !

private TwitterStream twitterStream;

private void filterTwitterStream() {

    if (conditionToStopStreaming) {
        if (null != twitterStream) {
            twitterStream.shutdown();
            twitterStream = null;
        }
    } else {
        if (twitterStream == null) {
            twitterStream = new TwitterStreamFactory(getConfiguration()).getInstance();
            twitterStream.addListener(getListener());
        }

        FilterQuery qry = new FilterQuery();

        String[] keywords = {......}
        qry.track(keywords);

        twitterStream.filter(qry);
    }
}

Where getConfiguration() returns my Configuration object and getListener() returns my defined StatusListener() object

like image 42
Marc Avatar answered Oct 14 '22 20:10

Marc