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:Origin.Mypage.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, TwitterException {
    response.setContentType("text/html;charset=UTF-8");
    /* TODO output your page here. You may use following sample code. */
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);//from   w  ww . j av a  2  s  .  co  m
    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
    cb.setOAuthAccessToken(ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    User user = twitter.verifyCredentials();
    request.setCharacterEncoding("UTF-8");
    HttpSession hs = request.getSession();
    UserData ud = (UserData) hs.getAttribute("ud");
    String search = ud.getLine() + "?";
    //String search= request.getParameter("searchtweet");
    Query query = new Query();
    query.setCount(100);
    query.setQuery(search);
    QueryResult queryresult = null;
    try {
        queryresult = twitter.search(query);
    } catch (TwitterException e1) {
        e1.printStackTrace();
    }
    ArrayList<String> userID = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        userID.add(tweet.getUser().getScreenName());
    }
    ArrayList<String> profileimg = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        profileimg.add(tweet.getUser().getBiggerProfileImageURL());
    }
    ArrayList<String> username = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        username.add(tweet.getUser().getName() + "<br>" + "@" + tweet.getUser().getScreenName());
    }
    ArrayList<String> usernameURL = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        username.add(tweet.getUser().getURL());
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

    ArrayList<String> resulttweet = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        resulttweet.add(tweet.getText() + "<br>" + sdf.format(tweet.getCreatedAt()));
    }
    request.setAttribute("search", search);
    request.setAttribute("userID", userID);
    request.setAttribute("profileimg", profileimg);
    request.setAttribute("username", username);
    request.setAttribute("resulttweet", resulttweet);
    request.setAttribute("usernameURL", usernameURL);
    request.getRequestDispatcher("/mypage.jsp").forward(request, response);
}

From source file:Origin.Searchtweet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, TwitterException {
    response.setContentType("text/html;charset=UTF-8");
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);/*from   w ww .j  a  v a 2s.  c om*/
    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
    cb.setOAuthAccessToken(ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    User user = twitter.verifyCredentials();
    request.setCharacterEncoding("UTF-8");
    String search = "?" + request.getParameter("searchtweet");
    //String search= request.getParameter("searchtweet");
    Query query = new Query();
    query.setCount(100);
    query.setQuery(search);
    QueryResult queryresult = null;
    try {
        queryresult = twitter.search(query);
    } catch (TwitterException e1) {
        e1.printStackTrace();
    }
    ArrayList<String> userID = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        userID.add(tweet.getUser().getScreenName());
    }
    ArrayList<String> profileimg = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        profileimg.add(tweet.getUser().getBiggerProfileImageURL());
    }
    ArrayList<String> username = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        username.add(tweet.getUser().getName() + "<br>" + "@" + tweet.getUser().getScreenName());
    }
    ArrayList<String> usernameURL = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        username.add(tweet.getUser().getURL());
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

    ArrayList<String> resulttweet = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        resulttweet.add(tweet.getText() + "<br>" + sdf.format(tweet.getCreatedAt()));
    }
    request.setAttribute("search", search);
    request.setAttribute("userID", userID);
    request.setAttribute("profileimg", profileimg);
    request.setAttribute("username", username);
    request.setAttribute("resulttweet", resulttweet);
    request.setAttribute("usernameURL", usernameURL);
    request.getRequestDispatcher("/searchtweet.jsp").forward(request, response);
}

From source file:proxectotwitt.Metodo.java

/**
 * Pasaremos el paremetro buscar para buscar todos los twits con esa palabra
 * @param buscar /*from   w w  w .  j av  a 2s .  c  om*/
 */
public static void buscar(String buscar) {

    QueryResult result;
    try {
        Query query = new Query(JOptionPane.showInputDialog("Hashtag a buscar: "));
        result = twitter.search(query);
        for (twitter4j.Status statuse : result.getTweets()) {
            System.out.println("@" + statuse.getUser().getScreenName() + ":" + statuse.getText());
        }
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(Metodo.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:proyectotwitter.AppTwitter.java

public void buscar(String text) {
    try {//from   ww w.j a  va2s  .  c o m
        Query query = new Query("#" + text);
        QueryResult result = twitter.search(query);
        for (Status status : result.getTweets()) {
            System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
        }
    } catch (TwitterException ex) {
        System.out.println("Error al buscar");
    }
}

From source file:ru.mail.sphere.java_hw5_vasilyev.twitteraccessor.Accessor.java

public static Collection<Tweet> search(String query, Date since, Date until, String lang, int querySize) {
    Twitter twitter = new TwitterFactory(config).getInstance();
    Collection<Tweet> methodResult = new ArrayList<>();
    try {/*from   ww  w .  j a v a  2s. c o  m*/
        Accessor.meetRateLimits(twitter);
        Query queryObject = Accessor.buildSearchQuery(query, since, until, lang, querySize);
        QueryResult result = twitter.search(queryObject);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            methodResult.add(Accessor.buildTweetFromStatus(tweet));
        }
    } catch (TwitterException te) {
        System.err.println(String.format("TwitterException occured: %s", te.getMessage()));
        System.err.println(Arrays.toString(te.getStackTrace()));
        System.err.println(Program.EXTERMINATUS);
    }
    return methodResult;
}

From source file:samples.TwitterSentiment.java

private static String[] get_twits(Date tempdate, String querystr) {

    ArrayList<String> res = new ArrayList<String>();
    try {//from w w  w.j av a2  s . c  o m
        twitter4j.Twitter twitter = getTwitter();

        Query query = new Query(querystr);
        query.setCount(5000);
        QueryResult result = twitter.search(query);
        for (Status status : result.getTweets()) {
            //System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
            res.add("@" + status.getUser().getScreenName() + ":" + status.getText());
        }
    } catch (TwitterException ex) {
        Logger.getLogger(TwitterSentiment.class.getName()).log(Level.SEVERE, null, ex);
    }

    return res.toArray(new String[] {});
}

From source file:search.TwitterSearchBean.java

@Override
public List<Tweet> search(final String keyword) throws TweetsNotFound, TwitterException {
    Query query = new Query(keyword + " -filter:retweets -filter:links -filter:replies -filter:images");
    query.setCount(20);/*from w  ww. j  ava 2 s  . co  m*/
    query.setLocale("en");
    query.setLang("en");
    QueryResult queryResult = twitter.search(query);
    List<Status> tweetsStatus = queryResult.getTweets();
    if (tweetsStatus.isEmpty())
        throw new TweetsNotFound();
    Collection<Tweet> tweets = mapper.transform(tweetsStatus);
    return (List<Tweet>) tweets;
}

From source file:sentimentanalysis.Twitter.GetTweets.java

public static void main(String args[]) {

    Twitter4JConfig t4jConfig = new Twitter4JConfig();
    ConfigurationBuilder cb = t4jConfig.getConfigurationBuilder();

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();//from w w w  . j  a  v  a2 s.c  o  m
    String keyword = "Trump";

    ArrayList<Post> TweetResultList = new ArrayList<Post>();
    int count = 0;
    try {

        Query query = new Query(keyword + "+exclude:retweets+lang:en").until("2016-11-19");
        QueryResult result;

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

            for (Status tweet : tweets) {
                count++;

                String user = tweet.getUser().getScreenName();
                String content = tweet.getText();
                Date time = tweet.getCreatedAt();
                System.out.println(time.getTime());
                String tweet_url = "https://twitter.com/" + tweet.getUser().getScreenName() + "/status/"
                        + tweet.getId();
                System.out.println(tweet.getLang());
                Post tr = new Post(user, content, tweet_url, new java.sql.Date(time.getTime()));
                TweetResultList.add(tr);

                //                    System.out.println("[user]@" + tweet.getUser().getScreenName() 
                //                        + "[/user][tweet]" + tweet.getText()+"[/tweet][link]"+tweet_url+"[/link]");
            }

        } while (((query = result.nextQuery()) != null) && (count < 500));

        try {
            SaveToDB.savePosts(TweetResultList);
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(GetTweets.class.getName()).log(Level.SEVERE, null, ex);
        }

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

}

From source file:sentinets.ImportTweets.java

License:Open Source License

private List<Status> getTweets(String queryStr, int countOfTweets) {
    Query query = new Query(queryStr);
    query.setCount(countOfTweets);//from  w  w w . ja  va2 s .com
    query.setLang("en");
    QueryResult result = null;
    List<Status> tweets = new ArrayList<Status>();
    do {
        try {
            result = twitter.search(query);
        } catch (TwitterException e) {
            e.printStackTrace();
            e.printStackTrace();
            System.out.println("Failed to search tweets: " + e.getMessage());

        }
        tweets.addAll(result.getTweets());
    } while ((query = result.nextQuery()) != null && tweets.size() <= countOfTweets);
    return tweets;
}

From source file:Situational_Awareness.TwitterSearch.java

public ArrayList<Information> twitterFeed(String twitterURL) {
    ArrayList<Information> informationList = new ArrayList<>();

    try {/*from  www  .  jav a2 s.c o  m*/
        Query query = new Query(twitterURL);
        QueryResult result;
        result = twitter.search(query);
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {
            System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());

            Date tweeted = tweet.getCreatedAt();

            if (tweeted.getTime() >= java.lang.System.currentTimeMillis() - 36000000) {
                Information newInfo = new Information(tweet.getId(), tweet.getUser().getScreenName(),
                        tweet.getText(), tweet.getUser().getLocation(), tweet.getUser().getProfileImageURL());
                informationList.add(newInfo);
            } else {
                System.out.println("information not relevant");
            }

        }
    } catch (Exception te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
    return informationList;
}