List of usage examples for twitter4j QueryResult hasNext
boolean hasNext();
From source file:com.klinker.android.twitter.ui.drawer_activities.discover.NearbyTweets.java
License:Apache License
public void getTweets() { canRefresh = false;/*from w ww .j a v a 2 s. c o m*/ new Thread(new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings); boolean manualLoc = sharedPrefs.getBoolean("manually_config_location", false); int i = 0; while (!connected && i < 5 && !manualLoc) { try { Thread.sleep(1500); } catch (Exception e) { } i++; } double latitude = -1; double longitude = -1; if (manualLoc) { // need to query yahoos api for the location... double[] loc = getLocationFromYahoo(sharedPrefs.getInt("woeid", 2379574)); latitude = loc[0]; longitude = loc[1]; } else { // set it from the location client Location location = mLocationClient.getLastLocation(); latitude = location.getLatitude(); longitude = location.getLongitude(); } query = new Query(); query.setGeoCode(new GeoLocation(latitude, longitude), 10, Query.MILES); QueryResult result = twitter.search(query); if (result.hasNext()) { hasMore = true; query = result.nextQuery(); } else { hasMore = false; } for (Status s : result.getTweets()) { statuses.add(s); } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { adapter = new TimelineArrayAdapter(context, statuses); listView.setAdapter(adapter); listView.setVisibility(View.VISIBLE); LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress); spinner.setVisibility(View.GONE); } }); } catch (Throwable e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { try { Toast.makeText(context, getString(R.string.error), Toast.LENGTH_SHORT).show(); } catch (IllegalStateException e) { // not attached to activity } } }); } canRefresh = true; } }).start(); }
From source file:com.klinker.android.twitter.ui.drawer_activities.discover.trends.SearchedTrendsActivity.java
License:Apache License
public void doSearch(final String mQuery) { spinner.setVisibility(View.VISIBLE); new Thread(new Runnable() { @Override/* w w w . j a v a2 s. co m*/ public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); query = new Query(); if (mQuery.contains(" TOP")) { query.setResultType(Query.ResultType.popular); } query.setQuery(mQuery.replace(" TOP", "")); QueryResult result; try { result = twitter.search(query); } catch (OutOfMemoryError e) { return; } tweets.clear(); for (twitter4j.Status status : result.getTweets()) { tweets.add(status); } if (result.hasNext()) { query = result.nextQuery(); hasMore = true; } else { hasMore = false; } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { adapter = new TimelineArrayAdapter(context, tweets); listView.setAdapter(adapter); listView.setVisibility(View.VISIBLE); spinner.setVisibility(View.GONE); } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { spinner.setVisibility(View.GONE); } }); } } }).start(); }
From source file:org.apache.camel.component.twitter.consumer.search.SearchConsumer.java
License:Apache License
private List<Status> search(Query query) throws TwitterException { Integer numberOfPages = 1;//from w w w. j a v a 2 s . com if (ObjectHelper.isNotEmpty(te.getProperties().getLang())) { query.setLang(te.getProperties().getLang()); } if (ObjectHelper.isNotEmpty(te.getProperties().getCount())) { query.setCount(te.getProperties().getCount()); } if (ObjectHelper.isNotEmpty(te.getProperties().getNumberOfPages())) { numberOfPages = te.getProperties().getNumberOfPages(); } LOG.debug("Searching with " + numberOfPages + " pages."); Twitter twitter = te.getProperties().getTwitter(); QueryResult qr = twitter.search(query); List<Status> tweets = qr.getTweets(); for (int i = 1; i < numberOfPages; i++) { if (!qr.hasNext()) { break; } qr = twitter.search(qr.nextQuery()); tweets.addAll(qr.getTweets()); } if (te.getProperties().isFilterOld()) { for (Status t : tweets) { checkLastId(t.getId()); } } return tweets; }
From source file:org.apache.camel.component.twitter.producer.SearchProducer.java
License:Apache License
@Override public void process(Exchange exchange) throws Exception { long myLastId = lastId; // KEYWORDS/* w w w .j a v a 2 s . c o m*/ // keywords from header take precedence String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_KEYWORDS, String.class); if (keywords == null) { keywords = te.getProperties().getKeywords(); } if (keywords == null) { throw new CamelExchangeException("No keywords to use for query", exchange); } Query query = new Query(keywords); // filter of older tweets if (te.getProperties().isFilterOld() && myLastId != 0) { query.setSinceId(myLastId); } // language String lang = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_LANGUAGE, String.class); if (lang == null) { lang = te.getProperties().getLang(); } if (ObjectHelper.isNotEmpty(lang)) { query.setLang(lang); } // number of elemnt per page Integer count = exchange.getIn().getHeader(TwitterConstants.TWITTER_COUNT, Integer.class); if (count == null) { count = te.getProperties().getCount(); } if (ObjectHelper.isNotEmpty(count)) { query.setCount(count); } // number of pages Integer numberOfPages = exchange.getIn().getHeader(TwitterConstants.TWITTER_NUMBER_OF_PAGES, Integer.class); if (numberOfPages == null) { numberOfPages = te.getProperties().getNumberOfPages(); } Twitter twitter = te.getProperties().getTwitter(); log.debug("Searching twitter with keywords: {}", keywords); QueryResult results = twitter.search(query); List<Status> list = results.getTweets(); for (int i = 1; i < numberOfPages; i++) { if (!results.hasNext()) { break; } log.debug("Fetching page"); results = twitter.search(results.nextQuery()); list.addAll(results.getTweets()); } if (te.getProperties().isFilterOld()) { for (Status t : list) { long newId = t.getId(); if (newId > myLastId) { myLastId = newId; } } } exchange.getIn().setBody(list); // update the lastId after finished the processing if (myLastId > lastId) { lastId = myLastId; } }
From source file:tweetmining.MiningFunctions.java
/** * This method mines data from youw twitter account based on the query string that you pass by * parameters./*www.ja v a 2 s .c om*/ * @param query Query you want to place. * @throws FileNotFoundException * @throws TwitterException */ public void MineFromQuery(String query) throws FileNotFoundException, TwitterException { Query q = new Query(query); QueryResult r; q.count(100); do { r = twitter.search(q); List<Status> statuses = r.getTweets(); for (Status st : statuses) { GeoLocation loc = st.getGeoLocation(); if (loc != null) { System.out.println("Loc not null"); Double lat = loc.getLatitude(); Double lon = loc.getLongitude(); pw.println(lat.toString() + ";" + lon.toString() + ";" + st.getUser().getName()); } } q = r.nextQuery(); } while (r.hasNext()); }
From source file:twitterrest.SearchTweet.java
License:Apache License
public static void main(String[] args) throws TwitterException { //?// ww w .j a v a 2 s. c om Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN) .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build(); Twitter twitter = new TwitterFactory(configuration).getInstance(); Query query = new Query(); try { File file = new File("./file/tweets.txt"); PrintWriter pw; //??? pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true))); // ??? query.setQuery("?"); query.setLang("ja"); // 1????Tweet?100? query.setCount(100); //????????? //query.setSince("2013-06-13"); //query.setUntil("2013-06-17"); // 150015 for (int i = 1; i <= 15; i++) { // QueryResult result = twitter.search(query); System.out.println(": " + result.getTweets().size()); System.out.println(" : " + new Integer(i).toString()); // ??? for (Status tweet : result.getTweets()) { // String str = tweet.getText(); System.out.println(str); // System.out.println(tweet.getUser()); // System.out.println(tweet.getCreatedAt()); // ??URL? StringTokenizer sta = new StringTokenizer(str, " "); //? while (sta.hasMoreTokens()) { String wk = sta.nextToken(); if (wk.indexOf("#") == -1 && wk.indexOf("http") == -1 && wk.indexOf("RT") == -1 && wk.indexOf("@") == -1) { pw.println(wk); } } String u = tweet.getUser().getName(); pw.println(u); } if (result.hasNext()) { query = result.nextQuery(); } else { break; } } pw.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:uk.ac.susx.tag.method51.twitter.pipeline.TweetSearch.java
License:Apache License
@Override protected void doWork() throws Exception { try {//from ww w . j a va 2 s . com final QueryResult result = twitter.search(query); final List<Status> statuses = result.getTweets(); final List<Tweet> tweets = new ArrayList<>(); for (final Status status : statuses) { getStats().update(status); final Tweet tweet = new Tweet(status); //strip out descs and urls if (!incProfileDescriptionUrl) { tweet.setDescription(null); tweet.setUrl(null); } tweets.add(tweet); } LOG.info("got {} tweets", tweets.size()); long minId = getStats().getMinId(); LOG.info("min id {}", new DateTime(Util.getInfoFromId(minId)[0]).toString("yyyy-MM-dd HH:mm:ss")); getStats().incrementTotal(tweets.size()); LOG.info("{} / {} = {} tps", getStats().getTotal(), getStats().getDuration(TimeUnit.SECONDS), getStats().getTweetsPer(TimeUnit.SECONDS)); produce(tweets); if (false && !result.hasNext()) { throw new ProducerUnderflowException("No more search result available."); } else if (tweets.size() == 0) { throw new ProducerUnderflowException("No more search result available."); } else if (searchRequested >= 0 && getStats().getTotal() >= searchRequested) { throw new ProducerUnderflowException("Reached requested size."); } else { query.setMaxId(getStats().getMinId() - 1); //query = result.nextQuery(); } } catch (final TwitterException e) { switch (e.getStatusCode()) { case 130: { //over-capacity LOG.warn(e.getMessage()); query.setMaxId(getStats().getMinId()); int retryAfter = 60; LOG.info("rate limited - sleeping for {} seconds", retryAfter); Thread.sleep(1000 * (retryAfter + 1)); break; } case 403: query.setMaxId(getStats().getMinId()); LOG.info("ID reached: {}", getStats().getMinId()); LOG.warn(e.getMessage()); throw e; //break; case 420: LOG.info("rate limited - sleeping for 60 seconds", e); Thread.sleep(60000); break; case 429: LOG.error("", e); Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus(); int retryAfter = rateLimitStatus.get("/search/tweets").getSecondsUntilReset(); LOG.info("rate limited - sleeping for {} seconds", retryAfter); Thread.sleep(1000 * (retryAfter + 1)); break; default: throw e; } } catch (Method51Exception e) { setStateFailure(e); } }