filtering out users using TwitterUtils

1.4k Views Asked by At

Is there a way to extract only specific user who post a tweet when using TwitterUtils.createStream()? (JAVA) The "filters" argument specifies the strings that need to be contained in the tweets, but I am not sure how this can be used to specify the user who interest me. i have this :

String[] filters = { "#USA" };
    JavaDStream<Status> tweets = TwitterUtils.createStream(ssc,filters);

this give me tweets which contains "#USA" what i need is a way to listen to a specific user and see what he post .

thanks

2

There are 2 best solutions below

0
On BEST ANSWER
private void receive() {
    try {
        TwitterStream newTwitterStream = new TwitterStreamFactory()
                .getInstance(twitterAuth);
        newTwitterStream.addListener(new StatusListener() {

            public void onException(Exception e) {
                if (!arret) {
                    restart("Erreur pandant la reception des tweets", e);
                }

            }

            public void onTrackLimitationNotice(int arg0) {
                // TODO Auto-generated method stub

            }

            public void onStatus(Status status) {
                store(status);
            }

            public void onStallWarning(StallWarning arg0) {
                // TODO Auto-generated method stub
            }

            public void onScrubGeo(long arg0, long arg1) {
                // TODO Auto-generated method stub

            }

            public void onDeletionNotice(StatusDeletionNotice arg0) {
                // TODO Auto-generated method stub

            }
        });
        FilterQuery query = new FilterQuery();

        if (filter_mots.length > 0 || filter_utilisateurs.length >0) {
            if (filter_mots.length > 0){
                query.track(filter_mots);   
            }
            if (filter_utilisateurs.length >0){

                query.follow(filter_utilisateurs);  
            }
            newTwitterStream.filter(query);
        } else{
            newTwitterStream.sample();
        }
        setTwitterStream(newTwitterStream) ; 
        arret = false ; 

    } catch (Throwable t) {
        // En cas d'erreur on redémare le streaming 
        restart("Erreur pendant la reception des données ", t);
    }
}
2
On

To filter users with Twitter4j you should use TwitterStream, like the code below:

TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
    .getInstance(); // First you create the Stream

StatusListener listener = new StatusListener() {
    //... a lot of things
}

twitterStream.addListener(listener);
FilterQuery filtre = new FilterQuery();
filtre.follow(usuarios); //you could filter by users
twitterStream.filter(filtre);

In that way you can get the tweets by the users that you want.

You should check the examples of Twitter4j for the streaming api.

If you want to filters users AND get the tweets that say "USA" you should filter first the users, and then in the StatusListener listener = new StatusListener() { //... a lot of things } part you can create filters for the tweets that you are receiving.