Example usage for twitter4j Location getPlaceCode

List of usage examples for twitter4j Location getPlaceCode

Introduction

In this page you can find the example usage for twitter4j Location getPlaceCode.

Prototype

int getPlaceCode();

Source Link

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;/*from  w  w  w.  j av  a  2 s  .  c o  m*/
    }

    // 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:DataCollections.LocationHelper.java

public Location_dbo convertLocationToLocation_dbo(Location l) {

    Location_dbo loc = new Location_dbo();
    //LogPrinter.printLog("Conversion of current location to database object is started");
    loc.values[Location_dbo.map.get("country_code")].setValue(l.getCountryCode());
    loc.values[Location_dbo.map.get("country_name")].setValue(l.getCountryName());
    loc.values[Location_dbo.map.get("name")].setValue(l.getName());
    loc.values[Location_dbo.map.get("place_code")].setValue(String.valueOf(l.getPlaceCode()));
    loc.values[Location_dbo.map.get("place_name")].setValue(l.getPlaceName());
    loc.values[Location_dbo.map.get("url")].setValue(l.getURL());
    loc.values[Location_dbo.map.get("woeid")].setValue(String.valueOf(l.getWoeid()));
    //LogPrinter.printLog("Conversion of current location to database object complete");
    return loc;// ww w.ja v a  2s  .co m
}

From source file:DataCollections.TrendsCollections.java

public void collectLatestTrendingHashTags() throws InterruptedException {

    ListIterator<Location> i = locations.listIterator();

    while (i.hasNext()) {
        Location l = i.next();

        try {/*  w  w  w .  j a  va 2s  . c o  m*/
            trends = trendsresources.getPlaceTrends(l.getWoeid());
        } catch (TwitterException e) {
            Thread.sleep((e).getRetryAfter() * 1000 + 5000);
            i.previous();
            continue;

        }

        System.out.println("\nLocation:- CountryCode: " + l.getCountryCode() + " Country Name: "
                + l.getCountryName() + " Name: " + l.getName() + " PlaceCode: " + l.getPlaceCode()
                + " PlaceName: " + l.getPlaceName() + " URL: " + l.getURL() + " Woeid: " + l.getWoeid() + "\n");
        Trend[] ts = trends.getTrends();

        for (int t = 0; t < ts.length; t++) {
            System.out.println("Trend " + t + ts[t].getName() + " " + ts[t].toString());
        }
        htaghelper.insertTrends(ts);
    }
}