2012-05-03 41 views
10

我一直在研究一个小程序,我想阅读热门twitter主题并将它们存储在数据库中。目前我正在使用twitter4j getDailyTrends()方法,但得到了奇怪的结果。使用Twitter4j每日趋势?

我目前拥有的代码是:

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

然而,当程序运行时,它显示了趋势24倍相同的列表。我曾尝试在不同的日子运行该程序,但无论我在哪一天运行该程序,该列表总是相同的。

我还试图使getDailyTrends()方法的当前日期和获得相同的结果。

将不胜感激任何帮助,它的驾驶我疯了。 :)

编辑:结果集我不断收到正在显示从25.04.2012 Twitter的趋势。不管我什么时候运行程序,或者我给它什么日期 - 我都会得到相同的结果。

EDIT2:行,所以这已被窃听了我一整天,我终于发现自己twitter4j阅读趋势提供的示例代码。我运行他们的代码而不是我的代码,而且我遇到了同样的问题。这些趋势已经过去了几周,从未改变过。有没有人真的设法让这种方法工作?

回答

1

我依稀记得,但你可以尝试以下

for (Trends trends : dailyTrends) { 
        System.out.println("As of : " + trends.getAsOf()); 

        System.out.println(" " + trends.getTrendAt()); 

     } 
+0

感谢您的回复。我尝试了你的建议,并收到了相同的趋势列表( –

+0

)。在这种情况下,我建议你检查你使用的是哪个版本的Twitter4J,以及它是否是版本错误。看到一个错误的getAsOf返回不正确的日期。http://jira.twitter4j.org/browse/TFJ-613如果不上市,我建议你我用twitter4j的最新的稳定版本(报告bug有 –

+0

2.2.5),我尝试了最新的快照版本(2.2.6),并再次遇到同样的问题。我现在已经没有想法了。 –

13
getPlaceTrends(int woeid) 

返回特定WOEID排名前10位的热门话题,如果趋势信息可用于它。

WOEID - Yahoo!的哪里地球上的地址ID返回的趋势信息。全球信息可通过使用1作为WOEID

你可以得到不同位置的WOEID与下面

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() + ")"); 
} 

然后代码,你可以用它WOEID像下面

Trends trends = twitter.getPlaceTrends(2295414); 
for (int i = 0; i < trends.getTrends().length; i++) { 
    System.out.println(trends.getTrends()[i].getName()); 
} 
3

我用得到的特定位置的当前趋势twitter4j和这是我的班,以获得趋势的主题,从你得到的位置,作为参数,在这个例子中“西班牙”

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

    } 
}