Example usage for twitter4j Twitter getPlaceTrends

List of usage examples for twitter4j Twitter getPlaceTrends

Introduction

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

Prototype

Trends getPlaceTrends(int woeid) throws TwitterException;

Source Link

Document

Returns the top 10 trending topics for a specific WOEID, if trending information is available for it.<br> The response is an array of "trend" objects that encode the name of the trending topic, the query parameter that can be used to search for the topic on <a href="http://search.twitter.com/">Twitter Search</a>, and the Twitter Search URL.<br> This information is cached for 5 minutes.

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/*  ww w  .  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.daiv.android.twitter.ui.drawer_activities.discover.trends.WorldTrends.java

License:Apache License

public void getTrends() {

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

                twitter4j.Trends trends = twitter.getPlaceTrends(1);
                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() {
                        if (currentTrends != null) {
                            listView.setAdapter(new TrendsArrayAdapter(context, currentTrends));
                        }

                        listView.setVisibility(View.VISIBLE);

                        LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress);
                        spinner.setVisibility(View.GONE);
                    }
                });

                HashtagDataSource source = HashtagDataSource.getInstance(context);

                for (String s : currentTrends) {
                    if (s.contains("#")) {
                        // we want to add it to the userAutoComplete
                        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();
            }
        }
    }).start();
}

From source file:com.fsatir.twitter.TwitterManagedBean.java

public void pullTrendTopicImages() {
    try {//from  w ww  .  java2s.  c o m
        Twitter twitter = bringMyTwitterInstance();
        Trends trends = twitter.getPlaceTrends(TURKEY_WOEID);
        int counter = 0;
        for (Trend trend : trends.getTrends()) {
            trendImageList(twitter, trend.getName());
            counter++;
        }
        showMessage(counter + " adet sonu getirildi.!");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("myMediaListFiltered",
                myMediaListFiltered);

    } catch (TwitterException ex) {
        ex.printStackTrace();
    }
}

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//w w  w .j av a 2  s  .  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.activities.drawer_activities.discover.trends.WorldTrends.java

License:Apache License

public void getTrends() {

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

                twitter4j.Trends trends = twitter.getPlaceTrends(1);
                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() {
                        if (currentTrends != null) {
                            listView.setAdapter(new TrendsArrayAdapter(context, currentTrends));
                        }

                        listView.setVisibility(View.VISIBLE);

                        LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress);
                        spinner.setVisibility(View.GONE);
                    }
                });

                HashtagDataSource source = HashtagDataSource.getInstance(context);

                for (String s : currentTrends) {
                    if (s.contains("#")) {
                        // we want to add it to the userAutoComplete
                        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();
            }
        }
    }).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//from   ww  w.  j a v  a 2  s.  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 = 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:com.mmiagency.knime.nodes.twitter.trends.TwitterTrendsNodeModel.java

License:Open Source License

/**
 * {@inheritDoc}/* w w  w  .  ja v  a2 s .c om*/
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {

    TrendRowFactory factory = new TrendRowFactory();
    BufferedDataContainer container = exec.createDataContainer(factory.tableSpec());
    int index = 0;

    Twitter twitter = ((TwitterApiConnectionPortObject) inObjects[0]).getTwitterApiConnection().getTwitter();

    Trends trends = twitter.getPlaceTrends(m_config.getWoeid());
    Location location = trends.getLocation();

    for (Trend trend : trends.getTrends()) {
        container.addRowToTable(factory.createRow("" + index++, location, trend));
    }

    container.close();
    return new PortObject[] { container.getTable() };

}

From source file:kerguelenpetrel.BotherSomeoneServlet.java

License:Apache License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    StringBuilder builder = new StringBuilder();
    long[] friendIDs, victimIDs;

    resp.setContentType("text/plain; charset=UTF-8");
    try {//from  w w w.j  ava  2s.com
        //Get the Twitter object
        Twitter twit = TwitterFactory.getSingleton();

        //Find a friend of a follower to bother
        friendIDs = twit.getFollowersIDs(twit.getId(), cursor).getIDs();

        if (friendIDs.length == 0) {
            resp.getWriter().println("Cannot find any followers to bother \n");
            return;
        }

        //Load the potential victim IDs
        victimIDs = twit.getFollowersIDs(friendIDs[r.nextInt(friendIDs.length)], cursor).getIDs();

        if (victimIDs.length == 0) {
            resp.getWriter().println("Cannot find any followers to bother \n");
            return;
        }

        //Write to our victim
        String victim = twit.showUser(victimIDs[r.nextInt(victimIDs.length)]).getScreenName();

        //Get a global trend
        Trends t = twit.getPlaceTrends(1);
        String trend = t.getTrends()[r.nextInt(t.getTrends().length)].getName();
        builder.append(getWordnikContent(victim, trend, resp));

        if (builder.length() > 280)
            builder.setLength(280); //Tweets are limited to 280 characters

        //Set the status
        StatusUpdate status = new StatusUpdate(builder.toString());

        //Post the status
        twit.updateStatus(status);
        resp.getWriter().println("Tweet posted: " + status.getStatus());
    }

    catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter \n");
        e.printStackTrace(resp.getWriter());
    }
}

From source file:kerguelenpetrel.UpdateStatusServlet.java

License:Apache License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    StringBuilder builder = new StringBuilder();

    resp.setContentType("text/plain; charset=UTF-8");
    resp.getWriter().println("Updating status...");

    try {/*from   ww w .java 2 s.c o m*/
        //Append feed title
        builder.append(getFeedTitle(resp));

        //Append Wordnik example sentence
        builder.append(getWordnikSentence(resp));

        /* Tweets are maximum 280 characters, so trim our sentence appropriately */
        if (builder.length() > 280) {
            if (builder.lastIndexOf(";", 220) > 0)
                builder.setLength(builder.lastIndexOf(";", 220));
            else if (builder.lastIndexOf(":", 220) > 0)
                builder.setLength(builder.lastIndexOf(":", 220));
            else if (builder.lastIndexOf(",", 220) > 0)
                builder.setLength(builder.lastIndexOf(",", 220));
            else
                builder.setLength(220);
        }

        //Append a Global trend
        Twitter twit = TwitterFactory.getSingleton();
        builder.append(
                " " + twit.getPlaceTrends(1).getTrends()[r.nextInt(twit.getPlaceTrends(1).getTrends().length)]
                        .getName());

        // Append a Wordnik trend
        builder.append(getWordnikTrend(resp));

        if (builder.length() > 280)
            builder.setLength(280); //Tweets are limited to 280 characters

        //Set the status
        StatusUpdate status = new StatusUpdate(builder.toString());

        /* Add an image from Flickr for small status */
        if (builder.length() < 180)
            status.setMediaIds(addFlickrImg(twit, resp));

        twit.updateStatus(status);
        resp.getWriter().println("Tweet posted: " + status.getStatus());
    }

    catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter \n");
        e.printStackTrace(resp.getWriter());
    }
}

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  a  2s.  co  m*/
    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;
}