【问题标题】:Using Twitter4j Daily Trends?使用 Twitter4j 每日趋势?
【发布时间】:2012-05-12 23:09:05
【问题描述】:

我一直在开发一个小程序,我想在其中阅读流行的 twitter 主题并将它们存储在数据库中。目前我正在使用 twitter4j getDailyTrends() 方法,但得到了奇怪的结果。

我目前拥有的代码是:

        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 自己提供的示例代码,用于阅读趋势。我运行他们的代码而不是我的代码,我遇到了同样的问题。趋势是几周前的,并且永远不会改变。以前有没有人真正设法让这种方法起作用?

【问题讨论】:

    标签: java twitter


    【解决方案1】:
    getPlaceTrends(int woeid)
    

    如果有可用的趋势信息,则返回特定 WOEID 的前 10 个热门话题。

    woeid - 雅虎!要返回其趋势信息的位置 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());
    }
    

    【讨论】:

      【解决方案2】:

      我使用 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;
          }
      
          }
      }
      

      【讨论】:

        【解决方案3】:

        我依稀记得但是你可以试试下面的

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

        【讨论】:

        • 感谢您的回复。我尝试了您的建议,并收到了与我几天来相同的趋势列表:(
        • 在这种情况下,我建议您检查您正在使用的 Twitter4J 版本,以及它是否不是版本错误。我看到 getAsOf 返回错误日期的错误。 jira.twitter4j.org/browse/TFJ-613如果没有列出,建议你在那里报告bug
        • 我使用的是 twitter4j (2.2.5) 的最新稳定版本,我尝试了最新的快照版本 (2.2.6) 并再次遇到同样的问题。我的想法已经不多了。
        • 另一个更新,我似乎得到了 25.04.2012 的趋势,而且我已经得到了这些相同的结果几天。手动向 getDailyTrends() 方法添加随机日期时仍然得到相同的结果。
        猜你喜欢
        • 2019-02-22
        • 1970-01-01
        • 2016-05-25
        • 2022-10-07
        • 2021-12-11
        • 2019-04-25
        • 2018-07-18
        • 2020-02-05
        • 1970-01-01
        相关资源
        最近更新 更多