List of usage examples for twitter4j Query Query
public Query(String query)
From source file:de.jetwick.tw.NewClass.java
License:Apache License
/** A thread using the search API */ public Thread search() { return new Thread() { @Override//from ww w. j av a 2 s. c om public void run() { int MINUTES = 2; Twitter twitter = new TwitterFactory().getInstance(); try { while (!isInterrupted()) { Query query = new Query(queryTerms); // RECENT or POPULAR or MIXED // doesn't make a difference if MIXED or RECENT query.setResultType(Query.MIXED); query.setPage(1); query.setRpp(100); QueryResult res = twitter.search(query); for (Tweet tw : res.getTweets()) { searchMap.put(tw.getId(), tw.getText()); } Thread.sleep(MINUTES * 60 * 1000L); } } catch (Exception ex) { ex.printStackTrace(); } } }; }
From source file:de.jetwick.tw.TwitterSearch.java
License:Apache License
long search(String term, Collection<JTweet> result, Map<String, JUser> userMap, int tweets, long lastMaxCreateTime) throws TwitterException { long maxId = 0L; long maxMillis = 0L; int hitsPerPage; int maxPages; if (tweets < 100) { hitsPerPage = tweets;//ww w . ja va 2s . c om maxPages = 1; } else { hitsPerPage = 100; maxPages = tweets / hitsPerPage; if (tweets % hitsPerPage > 0) maxPages++; } boolean breakPaging = false; for (int page = 0; page < maxPages; page++) { Query query = new Query(term); // RECENT or POPULAR query.setResultType(Query.MIXED); // avoid that more recent results disturb our paging! if (page > 0) query.setMaxId(maxId); query.setPage(page + 1); query.setRpp(hitsPerPage); QueryResult res = twitter.search(query); // is res.getTweets() sorted? for (Object o : res.getTweets()) { Tweet twe = (Tweet) o; // determine maxId in the first page if (page == 0 && maxId < twe.getId()) maxId = twe.getId(); if (maxMillis < twe.getCreatedAt().getTime()) maxMillis = twe.getCreatedAt().getTime(); if (twe.getCreatedAt().getTime() + 1000 < lastMaxCreateTime) breakPaging = true; else { String userName = twe.getFromUser().toLowerCase(); JUser user = userMap.get(userName); if (user == null) { user = new JUser(userName).init(twe); userMap.put(userName, user); } result.add(new JTweet(twe, user)); } } // minMillis could force us to leave earlier than defined by maxPages // or if resulting tweets are less then request (but -10 because of twitter strangeness) if (breakPaging || res.getTweets().size() < hitsPerPage - 10) break; } return maxMillis; }
From source file:de.vanita5.twittnuker.loader.support.TweetSearchLoader.java
License:Open Source License
@Override public List<Status> getStatuses(final Twitter twitter, final Paging paging) throws TwitterException { if (twitter == null) return null; final Query query = new Query(processQuery(mQuery)); query.setRpp(paging.getCount());//ww w .j a v a2s .c o m if (paging.getMaxId() > 0) { query.setMaxId(paging.getMaxId()); } return Arrays.asList(twitter.search(query).getStatuses()); }
From source file:dhbw.clippinggorilla.external.twitter.TwitterUtils.java
/** * @param includedTagsSet/*from w ww. ja v a 2 s . c o m*/ * @param date * @return */ private static Query queryBuilder(Set<String> includedTagsSet, LocalDateTime date) { List<String> includedTags = new ArrayList<>(); includedTags.addAll(includedTagsSet); String query = ""; if (includedTags.size() > 0) { query = replaceWhitespaces(includedTags.get(0)); for (int i = 1; i < includedTags.size(); i++) { query += " OR " + replaceWhitespaces(includedTags.get(i)); } } //System.out.println(date.toString()); //System.out.println(query); Query result = new Query(query); result.setResultType(Query.ResultType.popular); result.setSince(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); return result; }
From source file:edu.american.student.util.TwitterSpider.java
License:Open Source License
/** * Grabs the last 100 tweet objects (Status) from a handle * @param handle// w w w .ja v a2 s . c om * @return * @throws SpiderException */ public static List<Status> spider(String handle) throws SpiderException { //connect to Twitter ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.getTwitterOAuthConsumerKey()) .setOAuthConsumerSecret(Constants.getTwitterOAuthConsumerSecret()) .setOAuthAccessToken(Constants.getTwitterOAuthAccessToken()) .setOAuthAccessTokenSecret(Constants.getTwitterOAuthAccessTokenSecret()); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); //get tweets Query query = new Query("from:" + handle.replace("@", "")); query.setCount(100); QueryResult result; try { result = twitter.search(query); List<Status> toReturn = result.getTweets(); return toReturn; } catch (TwitterException e) { String gripe = "Twitter spider failed to...err spider!"; throw new SpiderException(gripe, e); } }
From source file:edu.mum.cs.wap.TwitterUtil.java
private static List<Status> getStatusesByKeyword(String keyword) { try {/* w ww. ja v a 2 s . c om*/ Query query = new Query("movie " + keyword + "-filter:retweets"); query.setCount(8); QueryResult result; do { result = twitter.search(query); return result.getTweets(); } while ((query = result.nextQuery()) != null); } catch (TwitterException te) { System.out.println("Failed to search tweets: " + te.getMessage()); } return null; }
From source file:edu.uci.ics.asterix.external.dataset.adapter.PullBasedTwitterFeedClient.java
License:Apache License
private void initialize(Map<String, String> params) { this.keywords = (String) params.get(SearchAPIConstants.QUERY); this.requestInterval = Integer.parseInt((String) params.get(SearchAPIConstants.INTERVAL)); this.query = new Query(keywords); this.query.setCount(100); }
From source file:edu.uci.ics.asterix.external.dataset.adapter.PushBasedTwitterFeedClient.java
License:Apache License
private void initialize(Map<String, String> params) { this.keywords = (String) params.get(SearchAPIConstants.QUERY); this.query = new Query(keywords); this.query.setCount(100); }
From source file:edu.ucsc.twitter.PeriodicTweetsSearch.java
License:Apache License
private Query buildOrQuery(Set<String> keywords, String language) { final StringBuilder queryString = new StringBuilder(); for (Iterator<String> itr = keywords.iterator(); itr.hasNext();) { queryString.append(String.format("\"%s\"", itr.next())); if (itr.hasNext()) { queryString.append(" OR "); }//from ww w. j ava 2 s . c o m } final Query query = new Query(queryString.toString() + " OR " + buildAndQuery(keywords)); query.setLang(language); return query; }
From source file:edu.uml.TwitterDataMining.TwitterConsumer.java
/** * Search twitter with the given search term Results will be saved in a file * with the same name as the query/* w w w . j av a 2s . c o m*/ * * @param searchTerm The search term to be queried * @return a list of tweets */ public static List<Status> queryTwitter(String searchTerm) { Query query = new Query(searchTerm); query.setLang("en"); // we only want english tweets // get and format date to get tweets no more than a year old SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date = sdf.format(new Date(System.currentTimeMillis() - (long) (365 * 24 * 60 * 60 * 1000))); query.setSince(date); QueryResult result; List<Status> tweets = null; try { //ONLY GETTING FIRST SET OF RESULTS result = twitter.search(query); tweets = result.getTweets(); // for (Status tweet : tweets) { // System.out.println("@" + tweet.getUser().getScreenName() + "\t" + tweet.getText()); // } // Wait loop for reset } catch (TwitterException te) { try { // try block for sleep thread if (!te.isCausedByNetworkIssue()) { // Not really checking for anything else but it is likely that we are out of requests int resetTime = te.getRateLimitStatus().getSecondsUntilReset(); while (resetTime > 0) { Thread.sleep(1000); // 1 second stop System.out.println("seconds till reset: " + resetTime); --resetTime; } } else { te.printStackTrace(); } } catch (InterruptedException ie) { ie.printStackTrace(); System.exit(-1); } } return tweets; }