@Before public void setUp() throws Exception { QueryResult queryResult = mock(QueryResult.class); when(queryResult.getTweets()).thenReturn(statuses); when(queryResult.nextQuery()).thenReturn(null); QueryResult emptyQueryResult = mock(QueryResult.class); when(emptyQueryResult.getTweets()).thenReturn(new ArrayList()); when(queryResult.nextQuery()).thenReturn(null); }
public static void main(String[] args) throws TwitterException, IOException { Twitter twitter = TwitterFactory.getSingleton(); JLanguageTool langTool = new JLanguageTool(new AmericanEnglish()); List<String> twts = new ArrayList<String>(); for (String arg : args) { Query query = new Query(arg); QueryResult result; int counter = 0; do { result = twitter.search(query); List<Status> tweets = result.getTweets(); for (Status tweet : tweets) { if (isEligible(tweet)) { System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText()); System.out.println(tweet.getLang()); twts.add(tweet.getText()); counter++; } } } while ((query = result.nextQuery()) != null && counter < 5); } for (String str : twts) { List<RuleMatch> matches = langTool.check(str); for (RuleMatch match : matches) { System.out.println( "Potential error at line " + match.getLine() + ", column " + match.getColumn() + ": " + match.getMessage()); System.out.println("Suggested correction: " + match.getSuggestedReplacements()); } } }
public static void printTweets( String keywords, String location, Integer limit, Boolean hideRetweets) throws TwitterException, URLs.ConnectionException, GeolocationSearch.SearchLocationException, MalformedURLException, GeolocationSearch.NoKeyException, URLs.HTTPQueryException { Twitter twitter = new TwitterFactory().getInstance(); Query query = new Query(keywords); query.setGeoCode(getLocation(location), RADIUS, Query.KILOMETERS); Integer needTweets; Integer numberTweets = 0; if (limit == Integer.MAX_VALUE) { needTweets = NUMBER_TWEETS; } else { needTweets = limit; } QueryResult result; List<Status> tweets; do { result = twitter.search(query); tweets = result.getTweets(); for (Status tweet : tweets) { if (!(hideRetweets && tweet.isRetweet()) && (numberTweets < needTweets)) { printTweet(tweet, false); numberTweets++; } } query = result.nextQuery(); } while ((numberTweets < needTweets) && (query != null)); if (numberTweets == 0) { System.out.println("Твитов по заданному запросу не найдено."); } else { System.out.println(SEPARATOR); } }
// Method to perform the actual query public List<Status> searchTweet(String inQuery) { Query query = new Query(inQuery); query.setCount(100); // Query Length List<Status> tweetList = new ArrayList<>(); try { int count = 0; QueryResult queryResult; do { queryResult = twitter.search(query); // Perform Search tweetList = queryResult.getTweets(); // get the results and put in Array // keep printing as long as there are tweets in the Array } while ((query = queryResult.nextQuery()) != null && count <= LIMIT); } // if anything goes wrong print this line catch (TwitterException te) { System.out.println("Couldn't connect: " + te.getErrorMessage()); } return tweetList; }
public void doNext() { try { if (result == null) { Query query = new Query(this.keywords); query.setCount(tweetBatchCount); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } result = twitter.search(query); i = 0; } else if (i >= tweetBatchCount - 1) { Query nextResult = result.nextQuery(); if (nextResult == null) { finished = true; } else { result = twitter.search(nextResult); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } i = 0; } } activeResultSet = result.getTweets(); } catch (TwitterException e) { e.printStackTrace(); } }