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

Asked

Viewed 911 times

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 collecting the api returns me tweets from all over the world. I’m using the twitter4j library. In the code below I’m collecting all 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);  

    }
}

1 answer

2

The Streaming API accepts the parameter language, which can be assigned with "pt" or "pt-BR". In the library twitter4j he is present in the class Filterquery, then 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 because besides it depends on geolocated tweets, which are not so frequent, it does not restrict the marriage of the Keywords that you pass in the parameter track, that is, tweets that have the word "car" and that are not inside the Location box that you passed, will also be returned. And This does not happen with the parameter language.

  • Humm, I understand, thank you very much Luis Cipriani! Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.