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:com.mycompany.twitterproductanalysistool.TwitterAPI.java

public ArrayList<String> getQuery(String qry) {
    tweets = new ArrayList<>();
    tweetCountries = new ArrayList<>();
    tweetDates = new ArrayList<>();
    try {//w w  w  . j av  a  2s.  com
        Query query = new Query(qry);
        query.setCount(99);
        result = twitter.search(query);
        for (Status status : result.getTweets()) {
            tweets.add(status.getText());
            tweetDates.add(status.getCreatedAt());
            if (status.getUser() == null) {
                tweetCountries.add("NO COUNTRY");
            } else {
                tweetCountries.add(status.getUser().getLocation());
            }
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    for (int i = 0; i < tweets.size(); i++) {
        String s = tweets.get(i);
        tweets.set(i, s.replace("\n", ""));
    }
    return tweets;
}

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

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* w ww . j  a  v a 2  s .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 {

    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   w ww.j  a v a  2  s  .  c o m
 *            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.rowland.hashtrace.sync.TweetHashTracerSyncAdapter.java

License:Apache License

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {//from w ww.ja v a2 s .  com
    Log.d(LOG_TAG, "Starting sync");
    // Getting the hashtag to send to the API
    String hashTagQuery = Utility.getPreferredHashTag(getContext());

    long hashTagId = addHashTag(hashTagQuery, hashTagQuery);

    ArrayList<Tweet> tweetArrayList = new ArrayList<Tweet>();
    List<Status> statuses = new ArrayList<Status>();

    try {

        mTwitter = Utility.getTwitter();

        statuses = mTwitter.search(new Query(hashTagQuery)).getTweets();

        for (Status s : statuses) {
            Tweet tweet = Utility.createTweet(s);
            tweetArrayList.add(tweet);
        }

    }

    catch (TwitterException e)

    {
        e.printStackTrace();
    }

    try {

        // Insert the new tweet information into the database
        Vector<ContentValues> cVVector = new Vector<ContentValues>(tweetArrayList.size());

        for (int i = 0; i < tweetArrayList.size(); i++) {
            // These are the values that will be collected.
            Tweet tweet = tweetArrayList.get(i);
            long tweet_id = tweet.getTweetId();
            String tweet_text = tweet.getTweetText();
            Date tweet_text_date = tweet.getTweetTextDate();
            int tweet_text_retweet_count = tweet.getTweetRetweetCount();
            int tweet_text_favourite_count = tweet.getTweetFavouriteCount();
            int tweet_text_mentions_count = tweet.getTweetMentionsCount();
            String user_name = tweet.getUserName();
            String user_image_url = tweet.getUserImageUrl();
            String user_cover_url = tweet.getUserCoverUrl();
            String user_location = tweet.getUserLocation();
            String user_description = tweet.getUserDescription();

            ContentValues tweetValues = new ContentValues();

            tweetValues.put(TweetEntry.COLUMN_HASHTAG_KEY, hashTagId);
            tweetValues.put(TweetEntry.COLUMN_TWEET_TEXT_DATE, TweetHashTracerContract
                    .getDbDateString(tweet_text_date, EDbDateLimit.DATE_FORMAT_NOW_LIMIT));
            tweetValues.put(TweetEntry.COLUMN_TWEET_ID, tweet_id);
            tweetValues.put(TweetEntry.COLUMN_TWEET_TEXT, tweet_text);
            tweetValues.put(TweetEntry.COLUMN_TWEET_TEXT_RETWEET_COUNT, tweet_text_retweet_count);
            tweetValues.put(TweetEntry.COLUMN_TWEET_TEXT_FAVOURITE_COUNT, tweet_text_favourite_count);
            tweetValues.put(TweetEntry.COLUMN_TWEET_TEXT_MENTIONS_COUNT, tweet_text_mentions_count);
            tweetValues.put(TweetEntry.COLUMN_TWEET_USERNAME, user_name);
            tweetValues.put(TweetEntry.COLUMN_TWEET_USERNAME_IMAGE_URL, user_image_url);
            tweetValues.put(TweetEntry.COLUMN_TWEET_USERNAME_COVER_URL, user_cover_url);
            tweetValues.put(TweetEntry.COLUMN_TWEET_USERNAME_LOCATION, user_location);
            tweetValues.put(TweetEntry.COLUMN_TWEET_USERNAME_DESCRIPTION, user_description);

            cVVector.add(tweetValues);
        }

        if (cVVector.size() > 0) {
            ContentValues[] cvArray = new ContentValues[cVVector.size()];
            cVVector.toArray(cvArray);
            int insertNo = getContext().getContentResolver().bulkInsert(TweetEntry.CONTENT_URI, cvArray);

            // Get's a calendar object with the current time.
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -3); // Signifies 3 days ago's date
            String previousDate = TweetHashTracerContract.getDbDateString(cal.getTime(),
                    EDbDateLimit.DATE_FORMAT_NOW_LIMIT);
            getContext().getContentResolver().delete(TweetEntry.CONTENT_URI,
                    TweetEntry.COLUMN_TWEET_TEXT_DATE + " <= ?", new String[] { previousDate });

            notifyWeather();

        }
        Log.d(LOG_TAG, "FetchTweetTask Complete. " + cVVector.size() + " Inserted");

    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        e.printStackTrace();
    }

    // This will only happen if there was an error getting or parsing the forecast.
    return;
}

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

License:Open Source License

/**
 * /*from   ww w. j a  v a2  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//from  w w  w  . j  a  v a2 s. co 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/* w w  w.j  a v  a  2s  . 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);
    }
}

From source file:com.twit.AppCrawler.java

public static void main(String[] args) throws TwitterException {

    TwitterFactory tf = new TwitterFactory();
    Twitter twitter = tf.getInstance();/*w w  w .j  av a 2  s  .  c o m*/

    CidadeController cidadeController = new CidadeController(new Cidade());
    List<Cidade> cidades = cidadeController.findEntities();

    String searchStr = "\"#viagem\" ";

    for (Cidade cidade : cidades) {

        //searchStr += "\"" + cidade.getNome() + "\" "; 

    }

    // Create a Query object.
    Query query = new Query(searchStr);

    // Send API request to execute a search with the given query.
    QueryResult result = twitter.search(query);

    // Display search results.
    for (Status status : result.getTweets()) {
        System.out.println("\n@" + status.getUser().getName() + ": " + status.getText());
    }

}

From source file:com.twitter.TwitterCrawler.java

/**
 *
 * @return @throws TwitterException/* ww  w.jav  a 2s. co  m*/
 * @throws InterruptedException returns the number of tweets
 */
public TwitterApiResponse stream() throws TwitterException, InterruptedException {
    //************************ Variables *************************
    TwitterApiResponse response = new TwitterApiResponse();
    long timeNow = System.currentTimeMillis();
    long afterInterval = timeNow + (interval * 1000);
    int countTweets = 0;
    //define the tweet id that the application should read relative to the IDs of Tweets it has already processed
    long maxID = 0;
    ArrayList<Status> tweets = new ArrayList<Status>();

    Query query = new Query(keywords);

    //************************ Action *************************
    //max 450 calls in 15 mins -->30/min, 5 calls/10 secs
    while (timeNow < afterInterval) {
        try {
            if (sinceID != 0) {
                query.setSinceId(sinceID);
            }

            if (maxID != 0) {
                query.setMaxId(maxID);
            }

            query.setResultType(Query.ResultType.recent);
            query.setCount(100);
            QueryResult result = twitter.search(query);
            tweets.addAll(result.getTweets());

            if (tweets.size() == 100) {
                countTweets += tweets.size();
                maxID = tweets.get(tweets.size() - 1).getId() - 1;
            } else {
                countTweets += tweets.size();
                break;
            }
            timeNow = System.currentTimeMillis();
        } catch (TwitterException te) {
            System.out.println("Couldn't connect: " + te);
        }
    }

    sinceID = tweets.get(0).getId();
    response.setCount(countTweets);
    response.setSinceID(sinceID);

    return response;
}

From source file:controllers.modules.CorpusModule.java

License:Open Source License

public static Result update(UUID corpus) {
    OpinionCorpus corpusObj = null;//from www . ja  v a 2s.  c  o m
    if (corpus != null) {
        corpusObj = fetchResource(corpus, OpinionCorpus.class);
    }
    OpinionCorpusFactory corpusFactory = null;

    MultipartFormData formData = request().body().asMultipartFormData();
    if (formData != null) {
        // if we have a multi-part form with a file.
        if (formData.getFiles() != null) {
            // get either the file named "file" or the first one.
            FilePart filePart = ObjectUtils.defaultIfNull(formData.getFile("file"),
                    Iterables.getFirst(formData.getFiles(), null));
            if (filePart != null) {
                corpusFactory = (OpinionCorpusFactory) new OpinionCorpusFactory().setFile(filePart.getFile())
                        .setFormat(FilenameUtils.getExtension(filePart.getFilename()));
            }
        }
    } else {
        // otherwise try as a json body.
        JsonNode json = request().body().asJson();
        if (json != null) {
            OpinionCorpusFactoryModel optionsVM = Json.fromJson(json, OpinionCorpusFactoryModel.class);
            if (optionsVM != null) {
                corpusFactory = optionsVM.toFactory();
            } else {
                throw new IllegalArgumentException();
            }

            if (optionsVM.grabbers != null) {
                if (optionsVM.grabbers.twitter != null) {
                    if (StringUtils.isNotBlank(optionsVM.grabbers.twitter.query)) {
                        TwitterFactory tFactory = new TwitterFactory();
                        Twitter twitter = tFactory.getInstance();
                        twitter.setOAuthConsumer(
                                Play.application().configuration().getString("twitter4j.oauth.consumerKey"),
                                Play.application().configuration().getString("twitter4j.oauth.consumerSecret"));
                        twitter.setOAuthAccessToken(new AccessToken(
                                Play.application().configuration().getString("twitter4j.oauth.accessToken"),
                                Play.application().configuration()
                                        .getString("twitter4j.oauth.accessTokenSecret")));

                        Query query = new Query(optionsVM.grabbers.twitter.query);
                        query.count(ObjectUtils.defaultIfNull(optionsVM.grabbers.twitter.limit, 10));
                        query.resultType(Query.RECENT);
                        if (StringUtils.isNotEmpty(corpusFactory.getLanguage())) {
                            query.lang(corpusFactory.getLanguage());
                        } else if (corpusObj != null) {
                            query.lang(corpusObj.getLanguage());
                        }

                        QueryResult qr;
                        try {
                            qr = twitter.search(query);
                        } catch (TwitterException e) {
                            throw new IllegalArgumentException();
                        }

                        StringBuilder tweets = new StringBuilder();
                        for (twitter4j.Status status : qr.getTweets()) {
                            // quote for csv, normalize space, and remove higher unicode characters. 
                            String text = StringEscapeUtils.escapeCsv(StringUtils
                                    .normalizeSpace(status.getText().replaceAll("[^\\u0000-\uFFFF]", "")));
                            tweets.append(text + System.lineSeparator());
                        }

                        corpusFactory.setContent(tweets.toString());
                        corpusFactory.setFormat("txt");
                    }
                }
            }
        } else {
            // if not json, then just create empty.
            corpusFactory = new OpinionCorpusFactory();
        }
    }

    if (corpusFactory == null) {
        throw new IllegalArgumentException();
    }

    if (corpus == null && StringUtils.isEmpty(corpusFactory.getTitle())) {
        corpusFactory.setTitle("Untitled corpus");
    }

    corpusFactory.setOwnerId(SessionedAction.getUsername(ctx())).setExistingId(corpus).setEm(em());

    DocumentCorpusModel corpusVM = null;
    corpusObj = corpusFactory.create();
    if (!em().contains(corpusObj)) {
        em().persist(corpusObj);

        corpusVM = (DocumentCorpusModel) createViewModel(corpusObj);
        corpusVM.populateSize(em(), corpusObj);
        return created(corpusVM.asJson());
    }

    for (PersistentObject obj : corpusObj.getDocuments()) {
        if (em().contains(obj)) {
            em().merge(obj);
        } else {
            em().persist(obj);
        }
    }
    em().merge(corpusObj);

    corpusVM = (DocumentCorpusModel) createViewModel(corpusObj);
    corpusVM.populateSize(em(), corpusObj);
    return ok(corpusVM.asJson());
}