How to collect tweets through the Streaming API, restricting a geographic location?

2

I would like to collect tweets through a certain keyword in real time, however I would like tweets only posted in brazil, because when doing the collection the api returns me tweets from all over the world. I'm using the twitter4j library. In the code below I am collecting all the tweets referring to the keywords "car".

package ToCollect;
import twitter4j.FilterQuery;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;

public class Main {
    public static void main(String[] args) {
        Connect.join();
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("X");
        cb.setOAuthConsumerSecret("X");
        cb.setOAuthAccessToken("X");
        cb.setOAuthAccessTokenSecret("X");


        /*
        TwitterFactory tf = new TwitterFactory(cb.build());  
        Twitter twitter = tf.getInstance();  

        try {
            twitter.updateStatus("It's Free!");
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        */
        TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

        StatusListener listener = new StatusListener() {

            @Override
            public void onException(Exception arg0) {
                // TODO Auto-generated method stub

            }

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

            }

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

            }

            @Override
            public void onStatus(Status status) {
                User user = status.getUser();

                // gets Username
                String username = status.getUser().getScreenName();
                System.out.println(username);
                String profileLocation = user.getLocation();
                System.out.println(profileLocation);
                long tweetId = status.getUser().getId(); 
                System.out.println(tweetId);
                String content = status.getText();
                System.out.println(content +"\n");
                if (content.contains("RT @") == false)
                Connect.insere(username, tweetId, content=Replace.trata(content));

            }

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

            }

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

            }

        };
        FilterQuery fq = new FilterQuery();

       String keywords[] = {"chuteira da nike"};
       fq.track(keywords);

        twitterStream.addListener(listener);
        twitterStream.filter(fq);  

    }
}
    
asked by anonymous 11.04.2014 / 20:25

1 answer

2

The Streaming API supports the parameter language , which can be assigned with "pt "or" en-GB ". In the twitter4j library it is present in the FilterQuery class , so you have to change your code to:

String keywords[] = {"chuteira da nike"};
fq.track(keywords);

twitterStream.addListener(listener);
twitterStream.filter(fq);
twitterStream.language("pt-BR");

I do not recommend using the parameter locations since it depends on geolocated tweets, which are not so frequent, it does not restrict the matching of the keywords you pass in the track parameter, that is, tweets that have the word "car" and are not within the location box you passed, will also be returned. E This does not happen with the language parameter.

    
12.04.2014 / 01:39