Example usage for twitter4j QueryResult getTweets

List of usage examples for twitter4j QueryResult getTweets

Introduction

In this page you can find the example usage for twitter4j QueryResult getTweets.

Prototype

List<Status> getTweets();

Source Link

Usage

From source file:libreriatwitter.Metodos.java

/**
 * Buscador de Tweets/*from w w  w.  j  ava 2 s  .c  om*/
 * @param buscar
 * @throws twitter4j.TwitterException
 */
public void buscarTweet(String buscar) throws TwitterException {

    Query query = new Query(buscar);
    QueryResult result = twitter.search(query);
    for (twitter4j.Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }

}

From source file:mashup.MashTweetManager.java

public static ArrayList<String> getTweets(String topic) throws IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthAccessToken(Common.AccessToken)
            .setOAuthAccessTokenSecret(Common.AccessTokenSecret).setOAuthConsumerKey(Common.ConsumerKey)
            .setOAuthConsumerSecret(Common.ConsumerSecret);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();//from  w w w .ja v  a 2 s .c  om

    //Twitter twitter = new TwitterFactory().getInstance();

    //  twitter.setOAuthAccessToken(null);

    ArrayList<String> tweetList = new ArrayList<>();

    try {
        Query query = new Query(topic.toLowerCase().trim());

        query.setCount(100);
        query.setLocale("en");
        query.setLang("en");

        QueryResult result = null;

        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();

            for (Status tweet : tweets) {
                String data = tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", "");

                HinghlishStopWords.removeStopWords(data.trim());
                // Remove Special... Characters 
                // Remove stop words 
                tweetList.add(tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", ""));

            }

        } while ((query = result.nextQuery()) != null);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
    return tweetList;
}

From source file:mitwitter.MiTwitter.java

/**
 * @param args the command line arguments
 *//*from  w w  w.  ja v  a  2s.  co  m*/
public static void main(String[] args) throws TwitterException {
    // TODO code application logic here

    //camobios

    Twitter twitter = new TwitterFactory().getInstance();

    //CODIGO CAMBIAR ESTADO
    Status miStatus = twitter.updateStatus("Tercer ejemplo");//cambia el estado de twitter
    System.out.println(miStatus.getText());

    //CODIGO TIMELINE
    List<Status> statuses = twitter.getHomeTimeline();
    System.out.println("Showing home timeline.");//aparecen los ultimos twitts
    for (Status status : statuses) {
        System.out.println(status.getUser().getName() + ":" + status.getText());
    }

    //CODIGO BUSCAR TAGS
    Query query = new Query("Chelsea"); //Dentro del String va el tag que quieres buscar
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }

}

From source file:moderation.Moderate.java

public List getTwitterpost() {

    List posts = new ArrayList();
    try {//from w ww .  j  av a  2s  .c  o  m
        List savedpost = getSavedList(album_id);
        String tagname = this.hash;
        Twitter twitter = setting.TwitterToken.twitterObject();
        Query query = new Query(tagname);
        query.setCount(50);
        QueryResult result;
        result = twitter.search(query);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            System.out.println("\n\n\n" + tweet);
            PostModel post = new PostModel();
            post.setAlbum_id(this.album_id);
            if (savedpost.contains(tweet.getId()))
                post.setStatus("old");
            else
                post.setStatus("new");
            post.setPost_id("" + tweet.getId());
            post.setSender_name(URLEncoder.encode(tweet.getUser().getScreenName(), "UTF-8"));
            post.setCaption_text(URLEncoder.encode(tweet.getText(), "UTF-8"));
            post.setSender_pic(tweet.getUser().getProfileImageURL());
            post.setSender_id("" + tweet.getUser().getId());

            for (MediaEntity mediaEntity : tweet.getMediaEntities()) {
                post.setImage_standard(mediaEntity.getMediaURL());
                post.setImage_low(mediaEntity.getMediaURL());
            }
            post.setPost_time(tweet.getCreatedAt().toString());
            post.setType("twitter_post");
            post.setLink(null);
            post.setParam("post_id=" + post.getPost_id() + "&album_id=" + post.getAlbum_id() + "&type="
                    + post.getType() + "&post_time=" + post.getPost_time() + "&link=" + post.getLink()
                    + "&pic_low=" + post.getImage_low() + "&pic_standard=" + post.getImage_standard()
                    + "&post_message=" + post.getCaption_text() + "&sender_name=" + post.getSender_name()
                    + "&sender_id=" + post.getSender_id() + "&sender_pic=" + post.getSender_pic());

            posts.add(post);

        }
        this.twitternext = result.nextQuery();

    } catch (Exception e) {
        System.err.println("Exception occure in getTwitter " + e);
    }

    return posts;
}

From source file:net.lacolaco.smileessence.activity.MainActivity.java

License:Open Source License

public void startNewSearch(final Twitter twitter, final String query) {
    setLastSearch(query);/*from   w  w  w  .j  a  va2s. c  om*/
    if (!TextUtils.isEmpty(query)) {
        final SearchListAdapter adapter = (SearchListAdapter) getListAdapter(ADAPTER_SEARCH);
        adapter.initSearch(query);
        adapter.clear();
        adapter.updateForce();
        new SearchTask(twitter, query, this) {
            @Override
            protected void onPostExecute(QueryResult queryResult) {
                super.onPostExecute(queryResult);
                if (queryResult != null) {
                    List<twitter4j.Status> tweets = queryResult.getTweets();
                    for (int i = tweets.size() - 1; i >= 0; i--) {
                        twitter4j.Status status = tweets.get(i);
                        if (!status.isRetweet()) {
                            StatusViewModel viewModel = new StatusViewModel(status, getCurrentAccount());
                            adapter.addToTop(viewModel);
                            StatusFilter.filter(MainActivity.this, viewModel);
                        }
                    }
                    adapter.setTopID(queryResult.getMaxId());
                    adapter.updateForce();
                }
            }
        }.execute();
    }
}

From source file:net.lacolaco.smileessence.twitter.task.SearchTask.java

License:Open Source License

@Override
protected void onPostExecute(QueryResult queryResult) {
    if (queryResult != null) {
        for (twitter4j.Status status : queryResult.getTweets()) {
            StatusCache.getInstance().put(status);
            FavoriteCache.getInstance().put(status);
        }/*  ww  w . ja  v  a  2 s.  c  om*/
    }
}

From source file:net.lacolaco.smileessence.view.SearchFragment.java

License:Open Source License

@Override
public void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) {
    final MainActivity activity = getMainActivity();
    final Account currentAccount = activity.getCurrentAccount();
    Twitter twitter = TwitterApi.getTwitter(currentAccount);
    final SearchListAdapter adapter = getListAdapter(activity);
    String queryString = adapter.getQuery();
    if (TextUtils.isEmpty(queryString)) {
        new UIHandler() {
            @Override/*www  .  ja v  a 2  s .c  o m*/
            public void run() {
                notifyTextEmpty(activity);
                refreshView.onRefreshComplete();
            }
        }.post();
        return;
    }
    final Query query = SearchTask.getBaseQuery(activity, queryString);
    if (adapter.getCount() > 0) {
        query.setSinceId(adapter.getTopID());
    }
    new SearchTask(twitter, query, activity) {
        @Override
        protected void onPostExecute(QueryResult queryResult) {
            super.onPostExecute(queryResult);
            if (queryResult != null) {
                java.util.List<twitter4j.Status> tweets = queryResult.getTweets();
                for (int i = tweets.size() - 1; i >= 0; i--) {
                    twitter4j.Status status = tweets.get(i);
                    if (!status.isRetweet()) {
                        StatusViewModel viewModel = new StatusViewModel(status, currentAccount);
                        adapter.addToTop(viewModel);
                        StatusFilter.filter(activity, viewModel);
                    }
                }
                updateListViewWithNotice(refreshView.getRefreshableView(), adapter, true);
                adapter.setTopID(queryResult.getMaxId());
                refreshView.onRefreshComplete();
            }
        }
    }.execute();
}

From source file:net.lacolaco.smileessence.view.SearchFragment.java

License:Open Source License

@Override
public void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) {
    final MainActivity activity = getMainActivity();
    final Account currentAccount = activity.getCurrentAccount();
    Twitter twitter = TwitterApi.getTwitter(currentAccount);
    final SearchListAdapter adapter = getListAdapter(activity);
    String queryString = adapter.getQuery();
    if (TextUtils.isEmpty(queryString)) {
        new UIHandler() {
            @Override/*from  w ww.  ja va 2  s .  c o m*/
            public void run() {
                notifyTextEmpty(activity);
                refreshView.onRefreshComplete();
            }
        }.post();
        return;
    }
    final Query query = SearchTask.getBaseQuery(activity, queryString);
    if (adapter.getCount() > 0) {
        query.setMaxId(adapter.getLastID() - 1);
    }
    new SearchTask(twitter, query, activity) {
        @Override
        protected void onPostExecute(QueryResult queryResult) {
            super.onPostExecute(queryResult);
            if (queryResult != null) {
                List<twitter4j.Status> tweets = queryResult.getTweets();
                for (twitter4j.Status status : tweets) {
                    if (!status.isRetweet()) {
                        StatusViewModel viewModel = new StatusViewModel(status, currentAccount);
                        adapter.addToBottom(viewModel);
                        StatusFilter.filter(activity, viewModel);
                    }
                }
                updateListViewWithNotice(refreshView.getRefreshableView(), adapter, false);
                refreshView.onRefreshComplete();
            }
        }
    }.execute();
}

From source file:nl.b3p.viewer.stripes.TwitterActionBean.java

License:Open Source License

public Resolution create() throws JSONException {
    JSONObject json = new JSONObject();

    json.put("success", Boolean.FALSE);
    String error = null;/*  w  w  w  . jav  a 2  s.  c om*/

    try {
        // The factory instance is re-useable and thread safe.
        Twitter twitter = new TwitterFactory().getInstance();
        Query query = new Query(term);
        if (latestId != null) {
            Long longVal = Long.valueOf(latestId);
            query.setSinceId(longVal);
        }

        QueryResult result = twitter.search(query);
        JSONArray tweets = new JSONArray();
        for (Status tweet : result.getTweets()) {

            //System.out.println(tweet.getFromUser() + ":" + tweet.getText());
            JSONObject t = new JSONObject();
            t.put("id_str", String.valueOf(tweet.getId()));
            t.put("text", tweet.getText());
            t.put("user_from", tweet.getUser().getScreenName());
            t.put("img_url", tweet.getUser().getProfileImageURL());

            JSONObject geo = new JSONObject();
            if (tweet.getGeoLocation() != null) {
                geo.put("lat", tweet.getGeoLocation().getLatitude());
                geo.put("lon", tweet.getGeoLocation().getLongitude());
            }
            t.put("geo", geo);
            tweets.put(t);
        }
        json.put("tweets", tweets);
        if (tweets.length() > 0) {
            json.put("maxId", String.valueOf(result.getMaxId()));
        } else {
            json.put("maxId", String.valueOf(latestId));
        }
        json.put("success", Boolean.TRUE);
    } catch (Exception e) {

        error = e.toString();
        if (e.getCause() != null) {
            error += "; cause: " + e.getCause().toString();
        }
    }

    if (error != null) {
        json.put("error", error);
    }

    return new StreamingResolution("application/json", new StringReader(json.toString()));
}

From source file:nl.isaac.dotcms.twitter.util.TwitterUtil.java

License:Creative Commons License

public List<CustomStatus> search(String from) {
    try {//  w w  w .  jav a  2  s  . c o m
        Query query = new Query(from);
        QueryResult result;

        result = twitter.search(query);
        List<CustomStatus> tweets = formatStatus(result.getTweets());
        return tweets;

    } catch (TwitterException te) {
        throw new RuntimeException(te.toString(), te);
    }
}