使用Twitter4j每日趋势?

Ada*_*ell 10 java twitter

我一直致力于一个小程序,我想在热门的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());
                       }
        }
Run Code Online (Sandbox Code Playgroud)

但是,当程序运行时,它会显示相同的趋势列表24次.我尝试在不同的日子运行该程序,但无论我在哪个日期运行程序,列表总是相同的.

我也尝试将getDailyTrends()方法传递给当前日期并获得相同的结果.

非常感谢任何帮助,这让我发疯.:)

编辑:我不断得到的结果集显示从2012年4月25日开始的推特趋势.无论我何时运行该程序,或者我给它的日期 - 我都得到了相同的结果.

编辑2:好的,所以这一直困扰着我,我最终找到了twitter4j自己提供的示例代码来阅读趋势.我运行他们的代码而不是我的代码,我遇到了同样的问题.趋势是几周之久,永远不会改变.有没有人真的设法让这个方法以前工作?

yAs*_*AsH 14

getPlaceTrends(int woeid)
Run Code Online (Sandbox Code Playgroud)

如果可以获得趋势信息,则返回特定WOEID的前10个趋势主题.

woeid - 雅虎!Where On Earth 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() + ")");
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用下面的WOEID获取特定位置的当前趋势

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


bor*_*hvm 5

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

    }
}
Run Code Online (Sandbox Code Playgroud)


San*_*air 1

我依稀记得,但你能尝试以下操作吗

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

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

        }
Run Code Online (Sandbox Code Playgroud)