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:com.learnncode.twitter.async.HashtagTweetFetchAsyncTask.java

License:Apache License

@Override
protected List<twitter4j.Status> doInBackground(Object... params) {

    List<twitter4j.Status> list = new ArrayList<twitter4j.Status>();
    if (AppUtilities.IsNetworkAvailable(mContext)) {

        try {//w ww.  j a v  a 2  s .  c o  m
            Twitter twitter;
            if (AppSettings.isTwitterAuthenticationDone(mContext)) {
                twitter = TwitterHelper.getTwitterInstance(mContext);

            } else {
                twitter = TwitterHelper.getTwitterInstanceWithoutAuthentication(mContext);
            }
            Query query = new Query(hashtag);
            query.count(10);
            if (lastSeenId != 0) {
                query.setMaxId(lastSeenId);
            }
            QueryResult qr = twitter.search(query);
            List<twitter4j.Status> qrTweets = qr.getTweets();

            for (twitter4j.Status t : qrTweets) {
                list.add(t);
            }

        } catch (Exception e) {
            System.err.println();
        }
    }
    if (lastSeenId != 0) {
        iHashTagList.setTweetList(list, true);
    } else {
        iHashTagList.setTweetList(list, false);
    }

    return list;
}

From source file:com.mothsoft.integration.twitter.TwitterServiceImpl.java

License:Apache License

public List<Status> search(final String query) {
    try {//from w  w  w  . ja  va  2 s. com
        final Twitter twitter = factory.getInstance();
        final QueryResult queryResult = twitter.search(new Query(query));
        final List<Status> tweets = queryResult.getTweets();
        return tweets;
    } catch (TwitterException e) {
        throw wrapException(e);
    }
}

From source file:com.mycompany.dovetune.SearchTwitter.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w w w. j  av  a 2s  . c  o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, TwitterException {

    //            HttpSession session = request.getSession(false);

    //            if(session != null){

    Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");

    if (twitter != null) {
        String soundcloudUrl = request.getParameter("soundcloudUrl");
        String songName = request.getParameter("songName");
        out.println(twitter.toString());

        int i = songName.indexOf("- ");
        String song = songName.substring(i + 2);
        try {
            Query query = new Query("(#" + song + ") OR ( " + songName + ") OR (#DoveTune)");

            QueryResult result = twitter.search(query);

            List<Status> tweets = result.getTweets();
            //                String list = "<ulid='tweetList'>";
            String list = "";
            for (Status tweet : tweets) {
                list += "<li><img class='profileImg' src='" + tweet.getUser().getProfileImageURL()
                        + "' title='Profile Image' alt='Profile Image'><strong>" + tweet.getUser().getName()
                        + " @" + tweet.getUser().getScreenName() + "</strong> - " + tweet.getText() + "</li>";
            }

            list = list.replace("#" + song, "<strong class='hashtags'>#" + song + "</strong>");
            list = list.replace("#DoveTune", "<strong class='hashtags'>#DoveTune</strong>");
            list = list.replace("#SoundCloud", "<strong class='hashtags'>#SoundCloud</strong>");
            String songL = song;
            songL = songL.toLowerCase();
            list = list.replace("#" + songL, "<strong class='hashtags'>#" + songL + "</strong>");
            if (list.equals("")) {
                list = "<li>There are no tweets to display. Tweet your own!</li>";
            }

            request.setAttribute("soundcloudUrl", soundcloudUrl);
            request.setAttribute("songName", songName);
            request.setAttribute("song", songName);
            request.setAttribute("list", list);

        } catch (TwitterException te) {
            te.printStackTrace();
            out.println("Failed to search tweets: " + te.getMessage());
            System.exit(-1);
        }
    } else {
        String list = "<ul><li>In order to view tweets, please sign in to <a href=\"SignIn\" title=\"Sign in to Twitter\"><strong class=\"notSignedIn\">Twitter</strong></a></li></ul>";
        request.setAttribute("list", list);
    }

    request.getRequestDispatcher("details.jsp").forward(request, response);

}

From source file:com.mycompany.omnomtweets.TweetsAboutCandidates.java

/**
 * Searches for tweets using the given query string.
 * @param str the query to use/*from  w ww  . j  a  v  a 2  s  . c  o m*/
 * @return List of the tweet statuses that match the query.
 */
public List<Status> search(String str, long maxId) {
    List<Status> tweets = null;
    try {
        Query query = new Query(str);
        query.setCount(100);
        //English only.
        query.setLang("en");
        QueryResult result;
        query.setMaxId(maxId);
        result = twitter.search(query);
        tweets = result.getTweets();
    } catch (TwitterException te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
    return tweets;
}

From source file:com.mycompany.twitterapp.TwitterApp.java

public void getTweet() {

    Query query = new Query(tweetSearchWord);
    QueryResult result = null;
    try {/* www .  j ava  2 s  .c  om*/
        result = twitter.search(query);
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(TwitterApp.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (result == null) {
        System.out.println("nope");
        return;
    }
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }
}

From source file:com.mycompany.twittersearch.SearchTwitter.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   www. j a  v a2 s .  c om
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, TwitterException {

    Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");
    out.println(twitter.toString());
    String searchItem = request.getParameter("term");

    try {
        Query query = new Query(searchItem);

        QueryResult result = twitter.search(query);

        List<Status> tweets = result.getTweets();
        String list = "";
        for (Status tweet : tweets) {
            list += "@" + tweet.getUser().getScreenName() + " - " + tweet.getText() + "<br>";
        }

        request.setAttribute("list", list);

    } catch (TwitterException te) {
        te.printStackTrace();
        out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    request.getRequestDispatcher("showTweets.jsp").forward(request, response);

}

From source file:com.ontotext.s4.TwitterVisualization.downloadTweets.SearchTweets.java

License:Open Source License

/**
 * This method executes search query on Twitter. The results are saved into the preconfigured result folder.
 * @param queryString//from ww  w .  jav  a  2  s  .com
 *            the search query string. For more information refer to https://dev.twitter.com/docs/using-search
 * @throws TwitterException
 *            any problem related to authentication, communication, search, etc.
 */
public void search(String queryString) throws TwitterException {
    // configures twitter API keys
    Twitter twitter = ConfigurateTwitterAccount();

    // preparint the query
    Query query = new Query(queryString);

    // search in both popular and recent tweets
    query.resultType(Query.MIXED);

    // restrict result to English language
    query.lang("en");

    // result of the query
    QueryResult result;

    do {
        logger.debug(query);

        // executing the query
        result = twitter.search(query);

        // collect all tweets available
        List<Status> tweets = result.getTweets();

        // save each tweet into a file
        for (Status tweet : tweets) {
            saveTweetIntoFile(tweet);
        }

    }
    // handle the paging of the results
    while ((query = result.nextQuery()) != null);

}

From source file:com.schnee.tweetgeister.visualization.TweetgeisterBalloonLayout.java

License:Open Source License

/**
 * /*from   ww w  .  j  a v  a  2  s.c  om*/
 */
private void createTree() {

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

        List<Tweet> allTweets = new ArrayList<Tweet>();
        String qString = "bp";
        Query query = new Query(qString);
        query.setRpp(100);
        query.setLang("en");

        int hits = 100;
        int page = 1;
        while (allTweets.size() < 1400 && page < 16 && hits == 100) {

            query.setPage(page);

            // System.out.println(query.toString());
            QueryResult result = twitter.search(query);
            List<Tweet> tweets = result.getTweets();
            allTweets.addAll(tweets);
            hits = tweets.size();
            System.out.println("page: " + page + " hits: " + hits + " all tweets:" + allTweets.size());
            page++;
        }
        System.out.println("hits: " + allTweets.size());

        Set<CharSequence> inputSet = new HashSet<CharSequence>();

        for (Tweet tweet : allTweets) {
            inputSet.add(tweet.getText());
        }

        Clusterer cl = new Clusterer(inputSet, TokenizedCharSequence.TOKENIZER_FACTORY);

        com.schnee.tweetgeister.data.Tree<CharSequence> mindmap = cl.buildTree();

        fillTree(mindmap);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.thesmartweb.swebrank.TwitterAnalysis.java

License:Apache License

/**
 * Method to get tweets regarding a string 
 * @param query_string the string to search for
 * @param config_path the directory with the twitter api key
 * @return the tweets in a string// w ww.  j a v  a2 s  . c  o m
 */
public String perform(String query_string, String config_path) {
    try {
        List<String> twitterkeys = GetKeys(config_path);
        //configuration builder in order to set the keys of twitter
        ConfigurationBuilder cb = new ConfigurationBuilder();
        String consumerkey = twitterkeys.get(0);
        String consumersecret = twitterkeys.get(1);
        String accesstoken = twitterkeys.get(2);
        String accesstokensecret = twitterkeys.get(3);
        cb.setDebugEnabled(true).setOAuthConsumerKey(consumerkey).setOAuthConsumerSecret(consumersecret)
                .setOAuthAccessToken(accesstoken).setOAuthAccessTokenSecret(accesstokensecret);
        TwitterFactory tf = new TwitterFactory(cb.build());
        AccessToken acc = new AccessToken(accesstoken, accesstokensecret);

        Twitter twitter = tf.getInstance(acc);

        //query the twitter
        Query query = new Query(query_string);
        int rpp = 100;
        query.count(rpp);
        query.setQuery(query_string);

        //----------get the tweets------------
        QueryResult result = twitter.search(query);
        List<Status> tweets = result.getTweets();

        RateLimitStatus rls = result.getRateLimitStatus();

        String tweet_txt = "";
        for (Status tweet : tweets) {
            tweet_txt = tweet_txt + " " + tweet.getText();
        }
        DataManipulation txtpro = new DataManipulation();
        Stopwords st = new Stopwords();
        tweet_txt = txtpro.removeChars(tweet_txt);
        tweet_txt = st.stop(tweet_txt);
        tweet_txt = txtpro.removeChars(tweet_txt);
        return tweet_txt;
    } catch (TwitterException ex) {
        String tweet_txt = "";
        Logger.getLogger(TwitterAnalysis.class.getName()).log(Level.SEVERE, null, ex);
        return tweet_txt = "fail";
    }
}

From source file:com.tweet.analysis.SearchTweets.java

License:Apache License

/**
 * Usage: java twitter4j.examples.search.SearchTweets [query]
 *
 * @param args search query/*ww w .jav a2 s  .  co m*/
 */
public static void main(String[] args) {
    if (args.length < 1) {
        //System.out.println("java twitter4j.examples.search.SearchTweets [query]");
        //System.exit(-1);
    }
    Twitter twitter = new TwitterFactory().getInstance();
    //twitter.getFollowersList("Kuldeep.loveiit");
    try {
        Query query = new Query("Modi");
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
            }
        } while ((query = result.nextQuery()) != null);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
}