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