List of usage examples for twitter4j Query setCount
public void setCount(int count)
From source file:TwitterPull.java
public void retrieveTweets() { try {// ww w . j av a2 s . c om Query query = new Query(this.queryString); query.setLang("en"); query.setCount(100); QueryResult result; int i = 0; do { result = twitter.search(query); List<Status> tweets = result.getTweets(); // int i = 0; for (Status tweet : tweets) { System.out.println(tweet.getText().replaceAll("\n", "").replaceAll("\r", "")); // appendTweetDocument(tweet.getText()); } i++; } while ((query = result.nextQuery()) != null && i < 10); // setTwitterFeed(tweets); // System.exit(0); } catch (TwitterException te) { Logger.getLogger(TwitterPull.class.getName()).log(Level.SEVERE, null, te); System.out.println("Failed to search tweets: " + te.getMessage()); System.exit(-1); } }
From source file:aic.group5.topic3.analysis.TwitterQueryWrapper.java
License:Apache License
public List<Status> query(String queryString) throws Exception { List<Status> result = new ArrayList<Status>(); twitter = twitterFactory.getInstance(); Query query = new Query(queryString); query.setCount(100); QueryResult queryResult = null;// w w w.ja va 2s . c om queryResult = searchWithRetry(query, retryAttempts); List<Status> tweets = queryResult.getTweets(); for (Status tweet : tweets) result.add(tweet); return result; }
From source file:Beans.Crawler.java
public String teste() { String result = ""; int totalTweets = 0; long maxID = -1; GeoLocation geo = new GeoLocation(Double.parseDouble("-19.9225"), Double.parseDouble("-43.9450")); result += geo.getLatitude() + " " + geo.getLongitude() + "<br><br>"; Twitter twitter = getTwitter();// w w w. j a v a2 s. c om try { Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets"); result += "You have " + searchTweetsRateLimit.getRemaining() + " calls remaining out of " + searchTweetsRateLimit.getLimit() + ", Limit resets in " + searchTweetsRateLimit.getSecondsUntilReset() + " seconds<br/><br/>"; for (int queryNumber = 0; queryNumber < MAX_QUERIES; queryNumber++) { // Do we need to delay because we've already hit our rate limits? if (searchTweetsRateLimit.getRemaining() == 0) { // Yes we do, unfortunately ... // System.out.printf("!!! Sleeping for %d seconds due to rate limits\n", searchTweetsRateLimit.getSecondsUntilReset()); // If you sleep exactly the number of seconds, you can make your query a bit too early // and still get an error for exceeding rate limitations // // Adding two seconds seems to do the trick. Sadly, even just adding one second still triggers a // rate limit exception more often than not. I have no idea why, and I know from a Comp Sci // standpoint this is really bad, but just add in 2 seconds and go about your business. Or else. // Thread.sleep((searchTweetsRateLimit.getSecondsUntilReset()+2) * 1000l); } Query q = new Query(SEARCH_TERM); //.geoCode((geo), 100, "mi"); // Search for tweets that contains this term q.setCount(TWEETS_PER_QUERY); // How many tweets, max, to retrieve // q.setGeoCode(geo, 100, Query.Unit.mi); // q.setSince("2012-02-20"); // // q.resultType("recent"); // Get all tweets // q.setLang("en"); // English language tweets, please // If maxID is -1, then this is our first call and we do not want to tell Twitter what the maximum // tweet id is we want to retrieve. But if it is not -1, then it represents the lowest tweet ID // we've seen, so we want to start at it-1 (if we start at maxID, we would see the lowest tweet // a second time... if (maxID != -1) { q.setMaxId(maxID - 1); } // This actually does the search on Twitter and makes the call across the network QueryResult r = twitter.search(q); // If there are NO tweets in the result set, it is Twitter's way of telling us that there are no // more tweets to be retrieved. Remember that Twitter's search index only contains about a week's // worth of tweets, and uncommon search terms can run out of week before they run out of tweets if (r.getTweets().size() == 0) { break; // Nothing? We must be done } // loop through all the tweets and process them. In this sample program, we just print them // out, but in a real application you might save them to a database, a CSV file, do some // analysis on them, whatever... for (Status s : r.getTweets()) // Loop through all the tweets... { // Increment our count of tweets retrieved totalTweets++; // Keep track of the lowest tweet ID. If you do not do this, you cannot retrieve multiple // blocks of tweets... if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } // Do something with the tweet.... result += "ID: " + s.getId() + " Data " + s.getCreatedAt().toString() + " user " + s.getUser().getScreenName() + " texto: " + cleanText(s.getText()) + " <br/>"; } // As part of what gets returned from Twitter when we make the search API call, we get an updated // status on rate limits. We save this now so at the top of the loop we can decide whether we need // to sleep or not before making the next call. searchTweetsRateLimit = r.getRateLimitStatus(); } } catch (Exception e) { // Catch all -- you're going to read the stack trace and figure out what needs to be done to fix it } result += "<br><br>" + totalTweets + "<br>"; return result; }
From source file:Beans.Crawler.java
public List search(Categoria categoria, Localizacao localizacao) throws Exception { long maxID = -1; List<Tweet> list = new ArrayList<Tweet>(); Twitter twitter = getTwitter();// w ww.ja v a2s . c om try { Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets"); for (int queryNumber = 0; queryNumber < MAX_QUERIES; queryNumber++) { if (searchTweetsRateLimit.getRemaining() == 0) { } String works = localizacao.getPalavrasChaves(); String[] arrWorks = works.split(","); String keywork = "", or = "OR"; for (int i = 0; i < arrWorks.length; i++) { if ((i + 1) >= arrWorks.length) { or = ""; } keywork += " \"" + arrWorks[i] + "\" " + or; } System.out.println("exclude:retweets " + categoria.getDescricao() + keywork); Query q = new Query("exclude:retweets " + categoria.getDescricao() + keywork); q.setCount(TWEETS_PER_QUERY); if (maxID != -1) { q.setMaxId(maxID - 1); } QueryResult r = twitter.search(q); if (r.getTweets().size() == 0) { break; } for (Status s : r.getTweets()) { if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } Tweet t = new Tweet(); t.setUsuario(s.getUser().getId()); String dataPostagem = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(s.getCreatedAt()); t.setDataPostagem(dataPostagem); t.setTweet(s.getText()); t.setTweetId(s.getId()); t.setCategoriaId(categoria.getId()); t.setLocalizacaoId(localizacao.getId()); list.add(t); } searchTweetsRateLimit = r.getRateLimitStatus(); } } catch (Exception e) { throw e; } return list; }
From source file:br.com.controller.TweetController.java
public String search() { Categoria c = new CategoriaDAO().find(Integer.parseInt(categoria)); Localizacao l = new LocalizacaoDAO().find(Integer.parseInt(localizacao)); long maxID = -1; Twitter twitter = getTwitter();/*from w w w . j a v a2s.c o m*/ try { Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets"); for (int queryNumber = 0; queryNumber < MAX_QUERIES; queryNumber++) { if (searchTweetsRateLimit.getRemaining() == 0) { } String works = c.getPalavrasChaves(); String[] arrWorks = works.split(","); String keywork = "", or = "OR", query = ""; for (int i = 0; i < arrWorks.length; i++) { if ((i + 1) >= arrWorks.length) { or = ""; } keywork += " \"" + arrWorks[i] + "\" " + or; } query = "exclude:retweets " + keywork; works = l.getPalavrasChaves(); arrWorks = works.split(","); keywork = ""; or = "OR"; for (int i = 0; i < arrWorks.length; i++) { if ((i + 1) >= arrWorks.length) { or = ""; } keywork += " \"" + arrWorks[i] + "\" " + or; } query += keywork; Query q = new Query(query); q.setCount(TWEETS_PER_QUERY); if (maxID != -1) { q.setMaxId(maxID - 1); } QueryResult r = twitter.search(q); if (r.getTweets().size() == 0) { break; } for (Status s : r.getTweets()) { if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } Tweet t = new Tweet(); t.setUsuario(s.getUser().getId()); t.setDataPostagem(s.getCreatedAt()); t.setTweet(s.getText()); if (!new DAO.TweetDAO().hastTweet(s.getId())) { t.setTweetId(s.getId()); t.setCategoria(c); t.setLocalizacao(l); jpa.saveOrUpdate(t); } } searchTweetsRateLimit = r.getRateLimitStatus(); } } catch (Exception e) { } return "index"; }
From source file:ch.schrimpf.core.TwitterCrawler.java
License:Open Source License
/** * @param queryString describes keywords and filters * @return an initialized Query// www . j a v a 2 s .c o m */ private Query initQuery(String queryString) { Query query = new Query(queryString); try { Properties prop = new Properties(); prop.load(new FileInputStream("easyTwitterCrawler.properties")); query.setCount(Integer.parseInt(prop.getProperty("queryLimit"))); query.setLocale(prop.getProperty("locale")); query.setLang(prop.getProperty("lang")); GeoLocation location = new GeoLocation(Double.parseDouble(prop.getProperty("latitude")), Double.parseDouble(prop.getProperty("longitude"))); double radius = Double.parseDouble(prop.getProperty("radius")); query.setGeoCode(location, radius, Query.KILOMETERS); } catch (IOException e) { // Properties could not be load query.setCount(DEFAULT_QUERY_LIMIT); query.setLocale(DEFAULT_LOCALE); query.setLang(DEFAULT_LANG); query.setGeoCode(DEFAULT_GEO_LOCATION, DEFAULT_RADIUS, Query.KILOMETERS); } return query; }
From source file:Classes.TwitterPull.java
public void retrieveTweets() throws TwitterException { Query query = new Query("\"" + this.queryString + "\""); query.setLang("en"); query.setCount(100); QueryResult result;// w w w . j a va2 s .c o m int i = 0; do { result = twitter.search(query); List<Status> tweets = result.getTweets(); for (Status tweet : tweets) { String t = tweet.getText().replaceAll("\n", "").replaceAll("\r", ""); // appendTweetDocument(t); retrievedTweets.add(t); } i++; } while ((query = result.nextQuery()) != null && i < 50); }
From source file:Collector.TweetCollector.java
public static List<Status> getTweets(final String q) { Timer timer = new Timer(); TimerTask hourlyTask = new TimerTask() { @Override// ww w .j av a 2 s . com public void run() { long amountOfTweets = 0; try { long maxID = -1; Query query = new Query(q); //printTimeLine(query); Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); RateLimitStatus searchLimit = rateLimitStatus.get("/search/tweets"); for (int batchNumber = 0; MAX_QUERIES < 10; batchNumber++) { System.out.printf("\n\n!!! batch %d\n\n", batchNumber); if (searchLimit.getRemaining() == 0) { // so as to not get blocked by twitter Thread.sleep(searchLimit.getSecondsUntilReset() + 3 * 1001); } query.setCount(TWEETS_PER_QUERY);// constant value of 100 query.setResultType(Query.ResultType.recent); query.setLang("en");// only english tweets if (maxID != -1) { query.setMaxId(maxID - 1);// so the first querys not set to previous max } QueryResult result = twitter.search(query); if (result.getTweets().size() == 0) { break; } for (Status s : result.getTweets()) { amountOfTweets++; if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } storeTweet(s);// where stored in db System.out.printf("At%s : %s\n", // debugging purposes s.getCreatedAt().toString(), s.getText()); searchLimit = result.getRateLimitStatus(); //resets System.out.printf("\n\nA total of %d tweet retrieved\n", amountOfTweets); } } } catch (TwitterException te) { System.out.println("Error Code :" + te.getErrorCode()); System.out.println("Exception Code " + te.getExceptionCode()); System.out.println("Status Code " + te.getStatusCode()); if (te.getStatusCode() == 401) { System.out.println("Twitter Error :\nAuthentication " + "credentials (https://dev.twitter.com/auth) " + " are either missing of incorrect, " + "\nplease check consumer key /secret"); } } catch (InterruptedException ex) { } } }; // schedule the task to run starting now and then every hour... timer.schedule(hourlyTask, 0l, 1000 * 60 * 60); return statuses; }
From source file:collector.TwitterCollector.java
public LinkedHashSet<Tweet> search(String queryExpression, int maxResults) { Query query = new Query(queryExpression); int numberOfTweets = maxResults;//512; long lastID = Long.MAX_VALUE; query = query.lang("pt"); List<Status> tweets = new ArrayList<>(); boolean finish = false; while ((tweets.size() < numberOfTweets) && !finish) { System.out.print("."); if (numberOfTweets - tweets.size() > 100) {//100) { query.setCount(100);//100); } else {/* w ww . j av a 2 s . c o m*/ query.setCount(numberOfTweets - tweets.size()); } try { QueryResult result = twitter.search(query); List<Status> resultList = result.getTweets(); if (resultList == null || resultList.isEmpty()) { finish = true; System.out.println("no foram encontrados mais tweets"); } else { tweets.addAll(resultList); for (Status t : tweets) { if (t.getId() < lastID) { lastID = t.getId(); } } } } catch (TwitterException ex) { System.err.println(ex.getMessage()); } query.setMaxId(lastID - 1); } LinkedHashSet<Tweet> out = new LinkedHashSet<>(); for (Status status : tweets) { if (!status.getText().startsWith("RT")) { TwitterUser user; user = new TwitterUser().addID(status.getUser().getId()).addName(status.getUser().getName()) .addLocation(status.getUser().getLocation()).addDateSignin(status.getUser().getCreatedAt()) .addCountTweets(status.getUser().getStatusesCount()) .addCountFavorites(status.getUser().getFavouritesCount()) .addCountFriends(status.getUser().getFriendsCount()) .addCountFollowers(status.getUser().getFollowersCount()); Tweet tweet = new Tweet().addUser(user).addText(status.getText()).addID(status.getId()) .addDate(status.getCreatedAt()) .addLatitude(status.getGeoLocation() != null ? status.getGeoLocation().getLatitude() : Double.MAX_VALUE) .addLongitude(status.getGeoLocation() != null ? status.getGeoLocation().getLongitude() : Double.MAX_VALUE); out.add(tweet); } } return out; }
From source file:com.daemon.Minion.java
License:Open Source License
/** * Sends a request specified as search term to Twitter and returns the * results Twitter returned./* w ww . j av a 2 s. c om*/ * * @param term The search term object. * @return The answer of the sent request as QueryResult. * @throws TwitterException Thrown whenever there is a problem querying Twitter * (i. e the Rate Limit was reached). */ public QueryResult search(SearchTerm term) throws TwitterException { Query query = new Query(term.getTerm()); query.setCount(100); query.setResultType(Query.RECENT); if (term.getLastFetchedTweetId() == null) { // Start a new backwards search fron the current given start if (term.getOldStart() == null) query.setUntil(Localization.DATETIME_FORMATTER.print(term.getCurrentStart().plusDays(1))); } else // Continue the current search from the last fetched tweet id query.setMaxId(term.getLastFetchedTweetId()); return _twitter.search(query); }