i've been working on a small program where I want to read in trending twitter topics and store them in a database. Currently I am using the twitter4j getDailyTrends() method, but getting strange results.
The code I currently have is:
Twitter twitter = new TwitterFactory().getInstance();
ResponseList<Trends> dailyTrends;
dailyTrends = twitter.getDailyTrends();
System.out.println();
// Print the trends.
for (Trends trends : dailyTrends) {
System.out.println("As of : " + trends.getAsOf());
for (Trend trend : trends.getTrends()) {
System.out.println(" " + trend.getName());
}
}
However, when the program runs, it shows the same list of trends 24 times. I have tried running the program on different days, however the list is always identical, no matter what day I run the program on.
I have also tried passing the getDailyTrends() method the current date and achieved the same results.
Would appreciate any help with this, it's driving me crazy. :)
EDIT: The result set I keep getting is displaying the twitter trends from 25.04.2012. And no matter when I run the program, or what date I give it - I get these same results.
EDIT2: OK so this has been bugging me all day, I eventually found the example code provided by twitter4j themselves for reading trends. I ran their code instead of mine, and I am having the same issue. The trends are a few weeks old, and never change. Has anyone actually managed to get this method working before?
getPlaceTrends(int woeid)
Returns the top 10 trending topics for a specific WOEID, if trending information is available for it.
woeid - The Yahoo! Where On Earth ID of the location to return trending information for. Global information is available by using 1 as the WOEID.
You can get the WOEID's of different locations with the code below
Twitter twitter = new TwitterFactory().getInstance();
ResponseList<Location> locations;
locations = twitter.getAvailableTrends();
System.out.println("Showing available trends");
for (Location location : locations) {
System.out.println(location.getName() + " (woeid:" + location.getWoeid() + ")");
}
And then, you can get the present trends of a specific location using it's WOEID like below
Trends trends = twitter.getPlaceTrends(2295414);
for (int i = 0; i < trends.getTrends().length; i++) {
System.out.println(trends.getTrends()[i].getName());
}
I use twitter4j and this is my class to get trendings topics from location that you get as parameter, in this example "spain"
import twitter4j.Location;
import twitter4j.ResponseList;
import twitter4j.Trends;
import twitter4j.Twitter;
import twitter4j.TwitterException;
public final class GetTrendingTopics {
public static void main(String[] args) {
try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey("yourConsumerKey").setOAuthConsumerSecret("yourConsumerSecret").setOAuthAccessToken("yourOauthToken").setOAuthAccessTokenSecret("yourOauthTokenSecret");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
ResponseList<Location> locations;
locations = twitter.getAvailableTrends();
Integer idTrendLocation = getTrendLocationId("spain");
if (idTrendLocation == null) {
System.out.println("Trend Location Not Found");
System.exit(0);
}
Trends trends = twitter.getPlaceTrends(idTrendLocation);
for (int i = 0; i < trends.getTrends().length; i++) {
System.out.println(trends.getTrends()[i].getName());
}
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get trends: " + te.getMessage());
System.exit(-1);
}
}
private static Integer getTrendLocationId(String locationName) {
int idTrendLocation = 0;
try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey("yourConsumerKey").setOAuthConsumerSecret("yourConsumerSecret").setOAuthAccessToken("yourOauthToken").setOAuthAccessTokenSecret("yourOauthTokenSecret");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
ResponseList<Location> locations;
locations = twitter.getAvailableTrends();
for (Location location : locations) {
if (location.getName().toLowerCase().equals(locationName.toLowerCase())) {
idTrendLocation = location.getWoeid();
break;
}
}
if (idTrendLocation > 0) {
return idTrendLocation;
}
return null;
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get trends: " + te.getMessage());
return null;
}
}
}
I faintly remember but can you try the following
for (Trends trends : dailyTrends) {
System.out.println("As of : " + trends.getAsOf());
System.out.println(" " + trends.getTrendAt());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With