Example usage for twitter4j Query Query

List of usage examples for twitter4j Query Query

Introduction

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

Prototype

public Query(String query) 

Source Link

Usage

From source file:proxectotwitt.Metodo.java

/**
 * Pasaremos el paremetro buscar para buscar todos los twits con esa palabra
 * @param buscar /*w  w w . ja  v a2  s  . c o m*/
 */
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   w  w  w.j  a v  a 2 s .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

private static Query buildSearchQuery(String query, Date since, Date until, String lang, int querySize) {
    Query queryObject = new Query(query);
    queryObject.setCount(querySize <= MAX_QUERY_SIZE ? querySize : MAX_QUERY_SIZE);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    queryObject.setSince(dateFormat.format(since));
    if (until != null) {
        queryObject.setUntil(dateFormat.format(until));
    }/*  w  w w.java2s.  c o m*/
    if (lang != null) {
        queryObject.setLang(lang);
    }
    return queryObject;
}

From source file:samples.TwitterSentiment.java

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

    ArrayList<String> res = new ArrayList<String>();
    try {//  w  w w. j  a va2s.  co 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   ww  w. ja va  2  s.  c om*/
    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();//w  ww .j a  v  a2s .  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);//  www  . j a va 2s. c  om
    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  . j a  v  a  2  s .c  om*/
        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;
}

From source file:StringMatching.GetTweet.java

/**
 * @param args the command line arguments
 *///from   w ww  .ja  v a 2s  .com
public static void main(String[] args) throws JSONException, IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Gkr9iZwYDALZ16OdxMp5rubBH")
            .setOAuthConsumerSecret("nhEwYFfiX5qp90sLLwO2eeYMxLwb3WC120lgihrocZDPWRNcUK")
            .setOAuthAccessToken("94107100-572UpcOkkz9kMWGaJS8YFsIGdlmJAd2cDw8y9rOnA")
            .setOAuthAccessTokenSecret("ST0XtXUjYgYWKHryL2feNM0VcDQQAgrov2V7nB7hq1xBC")
            .setHttpProxyHost("cache.itb.ac.id").setHttpProxyPort(8080).setHttpProxyUser("jonathan.benedict")
            .setHttpProxyPassword("rollingonthefloor");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    JSONObject obj = new JSONObject();
    int counterTweet = 0;
    FileWriter file = new FileWriter("C:\\Users\\user\\IdeaProjects\\TwitterStringMatching\\input.txt");
    file.flush();
    try {
        Query query = new Query("barca".toLowerCase());
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                counterTweet++;
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                obj.put("user", tweet.getUser().getScreenName());
                obj.put("tweets", tweet.getText());

                //Tulis file ke dalam txt

                try {
                    file.write(obj.toString());
                    System.out.println("Successfully Copied JSON Object to File...");
                    System.out.println("\nJSON Object: " + obj);

                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        } while (counterTweet < 1000);

        file.close();
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    System.exit(0);
}

From source file:study.bot.TweetEngine.java

License:Open Source License

public void replyToARandomPerson() throws TwitterException {

    aRandomTweet = TweetsCreator.getARandomTweet("search");
    query = new Query(TweetsCreator.searches.get(aRandomTweet));

    result = twitter.search(query);/*from w w  w  .  j a v  a  2s.com*/
    aRandomTweet = TweetsCreator.getARandomTweet("mention");
    tweetResult = result.getTweets().get(aRandomTweet);

    aRandomTweet = TweetsCreator.getARandomTweet("reply");
    statusUpdate = new StatusUpdate(
            ".@" + tweetResult.getUser().getScreenName() + TweetsCreator.replies.get(aRandomTweet));
    statusUpdate.inReplyToStatusId(tweetResult.getId());
    status = twitter.updateStatus(statusUpdate);

}