Example usage for twitter4j Query setQuery

List of usage examples for twitter4j Query setQuery

Introduction

In this page you can find the example usage for twitter4j Query setQuery.

Prototype

public void setQuery(String query) 

Source Link

Document

Sets the query string

Usage

From source file:org.xmlsh.twitter.search.java

License:BSD License

@Override
public int run(List<XValue> args) throws Exception {

    Options opts = new Options(sCOMMON_OPTS
            + ",q=query:,geo=geocode:,lang:,locale:,t=result_type:,until:,since_id:,max_id:,include_entities:,sanitize",
            SerializeOpts.getOptionDefs());
    opts.parse(args);/*ww  w  .j ava  2  s.  c o m*/
    mSerializeOpts = this.getSerializeOpts(opts);
    final boolean bSanitize = opts.hasOpt("sanitize");

    args = opts.getRemainingArgs();

    try {

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

        if (opts.hasOpt("query"))
            query.setQuery(opts.getOptStringRequired("query"));

        if (opts.hasOpt("lang"))
            query.setLang(opts.getOptStringRequired("lang"));

        if (opts.hasOpt("locale"))
            query.setLocale(opts.getOptStringRequired("locale"));

        if (opts.hasOpt("result_type"))
            query.setResultType(opts.getOptStringRequired("result_type"));

        if (opts.hasOpt("until"))
            query.setUntil(opts.getOptStringRequired("until"));

        if (opts.hasOpt("since_id"))
            query.setSinceId(opts.getOptValue("since_id").toLong());
        if (opts.hasOpt("since"))
            query.setUntil(opts.getOptStringRequired("since"));

        if (opts.hasOpt("max_id"))
            query.setSinceId(opts.getOptValue("max_id").toLong());

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

        OutputPort out = this.getStdout();
        mWriter = new TwitterWriter(out.asXMLStreamWriter(mSerializeOpts), bSanitize);

        mWriter.startDocument();
        mWriter.startElement("twitter");
        mWriter.writeDefaultNamespace();

        for (Status t : tweets)
            mWriter.write(t);

        mWriter.endElement();
        mWriter.endDocument();
        mWriter.closeWriter();

        out.release();
    } finally {

    }
    return 0;

}

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);// w  ww .  j a  v a 2  s .c o  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  w  w . j a v a2  s  .c  o  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");
    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:twitterrest.SearchTweet.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    //?/*from  www. j a  v a 2  s  . c  o m*/
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
    Twitter twitter = new TwitterFactory(configuration).getInstance();
    Query query = new Query();
    try {
        File file = new File("./file/tweets.txt");
        PrintWriter pw;
        //???
        pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));

        // ???
        query.setQuery("?");
        query.setLang("ja");
        // 1????Tweet?100?
        query.setCount(100);
        //?????????
        //query.setSince("2013-06-13");
        //query.setUntil("2013-06-17");

        // 150015
        for (int i = 1; i <= 15; i++) {
            // 
            QueryResult result = twitter.search(query);
            System.out.println(": " + result.getTweets().size());
            System.out.println(" : " + new Integer(i).toString());

            // ???
            for (Status tweet : result.getTweets()) {
                // 
                String str = tweet.getText();
                System.out.println(str);
                //
                System.out.println(tweet.getUser());
                //
                System.out.println(tweet.getCreatedAt());
                // ??URL?   
                StringTokenizer sta = new StringTokenizer(str, " ");
                //?
                while (sta.hasMoreTokens()) {
                    String wk = sta.nextToken();
                    if (wk.indexOf("#") == -1 && wk.indexOf("http") == -1 && wk.indexOf("RT") == -1
                            && wk.indexOf("@") == -1) {
                        pw.println(wk);
                    }
                }
                String u = tweet.getUser().getName();
                pw.println(u);
            }
            if (result.hasNext()) {
                query = result.nextQuery();
            } else {
                break;
            }
        }
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:twitterrest.UserSearch.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    //?/* w w w  .ja  v a 2  s.  c  o m*/
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
    Twitter twitter = new TwitterFactory(configuration).getInstance();
    Query query = new Query();

    // masason?Tweet
    //query.setQuery("from:anondroid3 OR to:anondroid3");
    query.setQuery("from:anondroid3");

    // ???
    QueryResult result = twitter.search(query);
    //
    System.out.println(":" + result.getTweets().size());
    // 1????Tweet?100?
    query.setCount(100);

    for (Status tweet : result.getTweets()) {
        System.out.println("tweet:" + tweet.getText());//
        System.out.println("UserID:" + tweet.getUser().getId());//ID
        System.out.println("Application:" + tweet.getSource());//
        System.out.println("Created Date:" + tweet.getCreatedAt());//?
        System.out.println("GeoLocation:" + tweet.getGeoLocation());//
    }
}