I'm using the twitter4j library in java to get a sample of random tweets from the public, however I'm only getting one tweet repeated over and over again.
public class FetchTweets {
private static String oAuthConsumerKey = "YOUR-CONSUMER-KEY-GOES-HERE";
private static String oAuthConsumerSecret = "YOUR-CONSUMER-SECRET-GOES-HERE";
private static String oAuthAccessToken = "YOUR-ACCESS-TOKEN-GOES-HERE";
private static String oAuthAccessTokenSecret = "YOUR-ACCESS-TOKEN-SECRET-GOES-HERE";
private static TwitterStream config() {
ConfigurationBuilder configBuilder = new ConfigurationBuilder();
configBuilder.setDebugEnabled(true)
.setOAuthConsumerKey(oAuthConsumerKey)
.setOAuthConsumerSecret(oAuthConsumerSecret)
.setOAuthAccessToken(oAuthAccessToken)
.setOAuthAccessTokenSecret(oAuthAccessTokenSecret);
return new TwitterStreamFactory(configBuilder.build()).getInstance();
}
private static void getTweets(){
TwitterStream twitterStream = config();
StatusListener listener = new StatusListener(){
@Override
public void onException(Exception ex) {
ex.printStackTrace();
}
@Override
public void onStatus(Status status) {
//System.out.println(status.getId() + " " + status.getText());
try {
writeTweetsToFile(status);
} catch (InterruptedException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Status deletion notice " + statusDeletionNotice.getStatusId());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice " + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId: " + userId + "upToStatusId:" + upToStatusId);
}
@Override
public void onStallWarning(StallWarning warning) {
System.out.println("Got stall warning " + warning);
}
};
//TwitterStream twitterStream = config();
twitterStream.addListener(listener);
twitterStream.sample();
}
}
Here is a snippet of my code that streams the tweets