Example usage for twitter4j Twitter getAvailableTrends

List of usage examples for twitter4j Twitter getAvailableTrends

Introduction

In this page you can find the example usage for twitter4j Twitter getAvailableTrends.

Prototype

ResponseList<Location> getAvailableTrends() throws TwitterException;

Source Link

Document

Returns the locations that Twitter has trending topic information for.

Usage

From source file:com.mmiagency.knime.nodes.twitter.trends.TwitterTrendsNodeDialog.java

License:Open Source License

private void initWoeid(TwitterApiConnection twitterApiConnection) throws TwitterException {

    // don't run again if it's already initialized
    if (m_country.getItemCount() > 1) {
        return;/* www. jav a2  s.  c om*/
    }

    // add worldwide as default
    woeidMap.put(1, "Worldwide");
    placeMap.put("Worldwide", 1);

    // if Twitter API Connection is not available, use Worldwide as default
    if (twitterApiConnection == null) {
        m_country.addItem("Worldwide");
        return;
    }

    Twitter twitter = twitterApiConnection.getTwitter();

    ResponseList<Location> response = twitter.getAvailableTrends();

    for (Location location : response) {
        // skip worldwide as it's already added as default
        if (location.getWoeid() == 1)
            continue;
        // check if it's city or country
        if (location.getPlaceCode() == 12) {
            woeidMap.put(location.getWoeid(), location.getCountryName());
            placeMap.put(location.getCountryName(), location.getWoeid());
        } else {
            woeidMap.put(location.getWoeid(), location.getCountryName() + "|" + location.getName());
            placeMap.put(location.getCountryName() + "|" + location.getName(), location.getWoeid());
        }
    }

    // split country and city
    List<String> cities;
    String[] tokens;
    String lastCountry = "";
    for (Map.Entry<String, Integer> entry : placeMap.entrySet()) {
        // skip worldwide as it's already added as default
        if (entry.getValue() == 1)
            continue;
        tokens = entry.getKey().split("\\|");
        if (!lastCountry.equals(tokens[0])) {
            m_country.addItem(tokens[0]);
        }
        lastCountry = tokens[0];
        if (countryCityMap.containsKey(tokens[0])) {
            cities = countryCityMap.get(tokens[0]);
        } else {
            cities = new ArrayList<String>();
            countryCityMap.put(tokens[0], cities);
        }
        if (tokens.length > 1) {
            cities.add(tokens[1]);
        } else {
            cities.add("-" + tokens[0] + "-");
        }
    }

}

From source file:org.wso2.carbon.connector.twitter.TwiterGetAvailableTrends.java

License:Apache License

@Override
public void connect(MessageContext messageContext) throws ConnectException {
    // TODO Auto-generated method stub
    try {/*w  w  w . j  a v a  2s  .  c  o  m*/
        Twitter twitter = new TwitterClientLoader(messageContext).loadApiClient();
        List<Location> locations = twitter.getAvailableTrends();
        OMElement resultElement = AXIOMUtil.stringToOM("<jsonObject><trends/></jsonObject>");
        for (Location location : locations) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("{ \"trend\" : ");
            String json = DataObjectFactory.getRawJSON(location);
            stringBuilder.append(json);
            stringBuilder.append("} ");
            OMElement element = super.parseJsonToXml(stringBuilder.toString());
            resultElement.addChild(element);
        }

        if (locations.size() == 0) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("{ \"trend\" :{} ");
            stringBuilder.append("} ");
            OMElement element = super.parseJsonToXml(stringBuilder.toString());
            resultElement.addChild(element);
        }

        super.preparePayload(messageContext, resultElement);

    } catch (TwitterException te) {
        log.error("Failed to load  available trends: " + te.getMessage(), te);
        TwitterUtils.storeErrorResponseStatus(messageContext, te);
    } catch (Exception te) {
        // TODO Auto-generated catch block
        log.error("Failed to load  available trends: " + te.getMessage(), te);
        TwitterUtils.storeErrorResponseStatus(messageContext, te);
    }

}

From source file:twitter4j.examples.trends.GetAvailableTrends.java

License:Apache License

/**
 * Usage: java twitter4j.examples.trends.GetAvailableTrends
 *
 * @param args message/*from   w ww  .  ja  v  a 2 s .co m*/
 */
public static void main(String[] args) {
    try {
        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() + ")");
        }
        System.out.println("done.");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get trends: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:wap.twitter.model.TwitterUtility.java

private static Integer getTrendLocationId(String locationName) {

    int idTrendLocation = 0;

    try {//from   ww w .  ja  v a  2  s . co  m

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

}