Example usage for twitter4j Twitter getClosestTrends

List of usage examples for twitter4j Twitter getClosestTrends

Introduction

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

Prototype

ResponseList<Location> getClosestTrends(GeoLocation location) throws TwitterException;

Source Link

Document

Returns the locations that Twitter has trending topic information for, closest to a specified location.<br> The response is an array of "locations" that encode the location's WOEID and some other human-readable information such as a canonical name and country the location belongs in.<br> A WOEID is a <a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo!

Usage

From source file:com.daiv.android.twitter.ui.drawer_activities.discover.trends.LocalTrends.java

License:Apache License

public void getTrends() {

    new Thread(new Runnable() {
        @Override/* w ww . j a v a  2  s . co m*/
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings);

                int i = 0;
                while (!connected && i < 5) {
                    try {
                        Thread.sleep(1500);
                    } catch (Exception e) {

                    }

                    i++;
                }

                twitter4j.Trends trends;

                if (sharedPrefs.getBoolean("manually_config_location", false)) {
                    trends = twitter.getPlaceTrends(sharedPrefs.getInt("woeid", 2379574)); // chicago to default
                } else {
                    Location location = mLastLocation;

                    ResponseList<twitter4j.Location> locations = twitter
                            .getClosestTrends(new GeoLocation(location.getLatitude(), location.getLongitude()));
                    trends = twitter.getPlaceTrends(locations.get(0).getWoeid());
                }

                final ArrayList<String> currentTrends = new ArrayList<String>();

                for (Trend t : trends.getTrends()) {
                    String name = t.getName();
                    currentTrends.add(name);
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (currentTrends != null) {
                                listView.setAdapter(new TrendsArrayAdapter(context, currentTrends));
                                listView.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(context, getResources().getString(R.string.no_location),
                                        Toast.LENGTH_SHORT).show();
                            }

                            LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress);
                            spinner.setVisibility(View.GONE);
                        } catch (Exception e) {
                            // not attached to activity
                        }
                    }
                });

                HashtagDataSource source = HashtagDataSource.getInstance(context);

                for (String s : currentTrends) {
                    Log.v("Test_hashtag", "trend: " + s);
                    if (s.contains("#")) {
                        // we want to add it to the auto complete
                        Log.v("Test_hashtag", "adding: " + s);

                        // could be much more efficient by querying and checking first, but I
                        // just didn't feel like it when there is only ever 10 of them here
                        source.deleteTag(s);

                        // add it to the userAutoComplete database
                        source.createTag(s);
                    }
                }

            } catch (Throwable e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Toast.makeText(context, getResources().getString(R.string.no_location),
                                    Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            // not attached to activity
                        }
                    }
                });
            }
        }
    }).start();
}

From source file:com.klinker.android.twitter.activities.drawer_activities.discover.trends.LocalTrends.java

License:Apache License

public void getTrends() {

    new Thread(new Runnable() {
        @Override//from   w  w  w . ja va  2s. c o  m
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings);

                int i = 0;
                while (!connected && i < 5) {
                    try {
                        Thread.sleep(1500);
                    } catch (Exception e) {

                    }

                    i++;
                }

                twitter4j.Trends trends;

                if (sharedPrefs.getBoolean("manually_config_location", false)) {
                    trends = twitter.getPlaceTrends(sharedPrefs.getInt("woeid", 2379574)); // chicago to default
                } else {
                    Location location = mLastLocation;

                    ResponseList<twitter4j.Location> locations = twitter
                            .getClosestTrends(new GeoLocation(location.getLatitude(), location.getLongitude()));
                    trends = twitter.getPlaceTrends(locations.get(0).getWoeid());
                }

                final ArrayList<String> currentTrends = new ArrayList<String>();

                for (Trend t : trends.getTrends()) {
                    String name = t.getName();
                    currentTrends.add(name);
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (currentTrends != null) {
                                listView.setAdapter(new TrendsArrayAdapter(context, currentTrends));
                                listView.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(context, getResources().getString(R.string.no_location),
                                        Toast.LENGTH_SHORT).show();
                            }

                            LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress);
                            spinner.setVisibility(View.GONE);
                        } catch (Exception e) {
                            // not attached to activity
                        }
                    }
                });

                HashtagDataSource source = HashtagDataSource.getInstance(context);

                for (String s : currentTrends) {
                    Log.v("talon_hashtag", "trend: " + s);
                    if (s.contains("#")) {
                        // we want to add it to the auto complete
                        Log.v("talon_hashtag", "adding: " + s);

                        // could be much more efficient by querying and checking first, but I
                        // just didn't feel like it when there is only ever 10 of them here
                        source.deleteTag(s);

                        // add it to the userAutoComplete database
                        source.createTag(s);
                    }
                }

            } catch (Throwable e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Toast.makeText(context, getResources().getString(R.string.no_location),
                                    Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            // not attached to activity
                        }
                    }
                });
            }
        }
    }).start();
}

From source file:com.klinker.android.twitter.ui.drawer_activities.discover.trends.LocalTrends.java

License:Apache License

public void getTrends() {

    new Thread(new Runnable() {
        @Override//  w  w  w.ja va 2 s .  c  om
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings);

                int i = 0;
                while (!connected && i < 5) {
                    try {
                        Thread.sleep(1500);
                    } catch (Exception e) {

                    }

                    i++;
                }

                twitter4j.Trends trends;

                if (sharedPrefs.getBoolean("manually_config_location", false)) {
                    trends = twitter.getPlaceTrends(sharedPrefs.getInt("woeid", 2379574)); // chicago to default
                } else {
                    Location location = mLocationClient.getLastLocation();

                    ResponseList<twitter4j.Location> locations = twitter
                            .getClosestTrends(new GeoLocation(location.getLatitude(), location.getLongitude()));
                    trends = twitter.getPlaceTrends(locations.get(0).getWoeid());
                }

                final ArrayList<String> currentTrends = new ArrayList<String>();

                for (Trend t : trends.getTrends()) {
                    String name = t.getName();
                    currentTrends.add(name);
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (currentTrends != null) {
                                listView.setAdapter(new TrendsArrayAdapter(context, currentTrends));
                                listView.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(context, getResources().getString(R.string.no_location),
                                        Toast.LENGTH_SHORT).show();
                            }

                            LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress);
                            spinner.setVisibility(View.GONE);
                        } catch (Exception e) {
                            // not attached to activity
                        }
                    }
                });

                HashtagDataSource source = HashtagDataSource.getInstance(context);

                for (String s : currentTrends) {
                    Log.v("talon_hashtag", "trend: " + s);
                    if (s.contains("#")) {
                        // we want to add it to the auto complete
                        Log.v("talon_hashtag", "adding: " + s);

                        // could be much more efficient by querying and checking first, but I
                        // just didn't feel like it when there is only ever 10 of them here
                        source.deleteTag(s);

                        // add it to the userAutoComplete database
                        source.createTag(s);
                    }
                }

            } catch (Throwable e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Toast.makeText(context, getResources().getString(R.string.no_location),
                                    Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            // not attached to activity
                        }
                    }
                });
            }
        }
    }).start();
}

From source file:project.mum.TwitterTrend.java

public List getTrends(double longi, double lati) throws TwitterException {
    List availableTrends = new ArrayList<String>();

    ConfigurationBuilder cf = new ConfigurationBuilder();
    cf.setDebugEnabled(true).setOAuthConsumerKey("TBFU8jBXiIOEde0cnSglw2m7B")
            .setOAuthConsumerSecret("c0tJVVvGgpY2rI1Ol5qmxzMpB1MiBx8PGlLNPG7TYAAVXwYVvL")
            .setOAuthAccessToken("1148852892-OR8mM62nOH4WPJf991X5bCp4zVKT2EU57fBmjWQ")
            .setOAuthAccessTokenSecret("zpXLqUxlkHZT58RDbGEPLnXVB3Kpwp7d8Z4CKb4X4UJW6");
    TwitterFactory tf = new TwitterFactory(cf.build());
    twitter4j.Twitter twitter = tf.getInstance();
    ResponseList<Location> locations;
    List<MyTrend> myTrend = new ArrayList<MyTrend>();
    MyTrend myTwitterTrend;//from w  w w.ja  v a2  s.  c  om
    GeoLocation geo = new GeoLocation(longi, lati);
    locations = twitter.getClosestTrends(geo);
    Trends trends = twitter.getPlaceTrends(locations.get(0).getWoeid());
    int count = 0;
    for (Trend trend : trends.getTrends()) {
        if (count < 5) {
            myTwitterTrend = new MyTrend(trend.getName(), trend.getURL());
            myTrend.add(myTwitterTrend);
            count++;
            availableTrends.add(trend.getName());
        }
    }
    System.out.println(" available Trends :" + availableTrends);
    return myTrend;
}

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

public List<Trend> getTrends(GeoLocation loc) throws TwitterException {
    TwitterFactory tf = config();/*from w  w  w.j a v  a2 s . c o  m*/
    Twitter twitter = tf.getInstance();
    List<Trend> tds = new ArrayList<Trend>();
    ResponseList<Location> locations;
    locations = twitter.getClosestTrends(loc);
    for (Location location : locations) {
        Trends trends = twitter.getPlaceTrends(location.getWoeid());
        for (int i = 0; i < trends.getTrends().length; i++) {
            if (i == 10) {
                break;
            }
            tds.add(trends.getTrends()[i]);
        }
    }
    return tds;
}