List of usage examples for twitter4j Status getUser
User getUser();
From source file:bditac.TwitterCaptura.java
public static void main(String[] args) throws TwitterException { int criid, apiid, capid; if (args.length == 3) { criid = Integer.parseInt(args[0]); apiid = Integer.parseInt(args[1]); capid = Integer.parseInt(args[2]); } else {//from w w w. j a va 2 s .co m criid = 1; apiid = 1; capid = 1; } CriseJpaController crijpa = new CriseJpaController(factory); Crise crise = crijpa.findCrise(criid); CriseApiJpaController criapijpa = new CriseApiJpaController(factory); CriseApi criapi = criapijpa.findCriseApi(capid); ApiJpaController apijpa = new ApiJpaController(factory); Api api = apijpa.findApi(apiid); CidadeJpaController cidjpa = new CidadeJpaController(factory); Cidade cidade = cidjpa.findCidade(crise.getCidId()); String coords[] = crise.getCriRegiao().split(","); TwitterGeo geo = new TwitterGeo(coords, cidade.getCidNome(), crise.getCriGeotipo()); Thread threadGeo = new Thread(geo); TwitterMens mens = new TwitterMens(crise.getCrtId()); Thread threadMens = new Thread(mens); TwitterGravar gravar = new TwitterGravar(); Thread threadGravar = new Thread(gravar); threadGeo.setName("ThreadGeo"); threadGeo.start(); threadMens.setName("ThreadMens"); threadMens.start(); threadGravar.setName("ThreadGravar"); threadGravar.start(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(criapi.getCapKey()) .setOAuthConsumerSecret(criapi.getCapSecret()).setOAuthAccessToken(criapi.getCapToken()) .setOAuthAccessTokenSecret(criapi.getCapTokenSecret()); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); String texto = "[\\xF0\\x9F]"; StatusListener listener; listener = new StatusListener() { int cont = 0; @Override public void onStatus(Status status) { if (!(status.getText().contains(texto) || status.getText().isEmpty())) { Ocorrencia ocor = new Ocorrencia(); ocor.setApiId(apiid); ocor.setCriId(criid); ocor.setCapId(capid); ocor.setOcrIdApi(status.getId()); ocor.setOcrCriacao(status.getCreatedAt()); ocor.setOcrTexto(status.getText()); // System.out.println(ocor.getOcrTexto()); ocor.setOcrUsuId(status.getUser().getId()); ocor.setOcrUsuNome(status.getUser().getName()); ocor.setOcrUsuScreenNome(status.getUser().getScreenName()); ocor.setOcrFonte(status.getSource()); ocor.setOcrLingua(status.getLang()); ocor.setOcrFavorite(status.getFavoriteCount()); ocor.setOcrRetweet(status.getRetweetCount()); String coords = ""; if (status.getPlace() == null) { ocor.setOcrPaisCodigo(""); ocor.setOcrPais(""); ocor.setOcrLocal(""); } else { ocor.setOcrPaisCodigo(status.getPlace().getCountryCode()); ocor.setOcrPais(status.getPlace().getCountry()); ocor.setOcrLocal(status.getPlace().getFullName()); GeoLocation locs[][] = status.getPlace().getBoundingBoxCoordinates(); for (int x = 0; x < locs.length; x++) { for (int y = 0; y < locs[x].length; y++) { coords += "[" + locs[x][y].getLongitude() + "," + locs[x][y].getLatitude() + "]"; if (!(x == locs.length - 1 && y == locs[x].length - 1)) { coords += ","; } } } } ocor.setOcrCoordenadas(coords); ocor.setOcrGeo('0'); ocor.setOcrIdentificacao('0'); ocor.setOcrIdenper(0.0f); ocor.setOcrGravado('0'); ocor.setOcrSentimento('0'); ocor.setOcrTempo('0'); boolean add = ocors.add(ocor); cont++; if (ocors.size() > 1000) { Limpar(); } // System.out.println(cont+" - "+status.getId() + " - " + status.getCreatedAt() + status.getPlace().getFullName()); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); } @Override public void onException(Exception ex) { ex.printStackTrace(); } private void Limpar() { while (!threadGeo.isInterrupted()) { threadGeo.interrupt(); } while (!threadMens.isInterrupted()) { threadMens.interrupt(); } while (!threadGravar.isInterrupted()) { threadGravar.interrupt(); } boolean achou = true; int x = 0; System.out.println("Removendo: " + ocors.size()); while (x < ocors.size()) { if (ocors.get(x).getOcrGravado() != '0') { ocors.remove(x); } else { x++; } } System.out.println("Final: " + ocors.size()); if (!threadGeo.isAlive()) { threadGeo.start(); } if (!threadMens.isAlive()) { threadMens.start(); } if (!threadGravar.isAlive()) { threadGravar.start(); } } }; FilterQuery filter = new FilterQuery(); double[][] location = new double[2][2]; location[0][0] = Double.parseDouble(coords[0]); location[0][1] = Double.parseDouble(coords[1]); location[1][0] = Double.parseDouble(coords[4]); location[1][1] = Double.parseDouble(coords[5]); filter.locations(location); twitterStream.addListener(listener); twitterStream.filter(filter); }
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 ww . java 2 s . com*/ 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. j a v a 2s . co 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 = 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:birdseye.Sample.java
License:Apache License
public List<TweetData> execute(String[] args) throws TwitterException { final List<TweetData> statuses = new ArrayList(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthAccessToken("14538839-3MX2UoCEUaA6u95iWoYweTKRbhBjqEVuK1SPbCjDV"); cb.setOAuthAccessTokenSecret("nox7eYyOJpyiDiISHRDou90bGkHKasuw1IMqqJUZMaAbj"); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { public void onStatus(Status status) { String user = status.getUser().getScreenName(); String content = status.getText(); TweetData newTweet = new TweetData(user, content); statuses.add(newTweet);//from w w w . j a v a 2 s . c om System.out.println(statuses.size() + ":" + status.getText()); if (statuses.size() > 15) { synchronized (lock) { lock.notify(); } System.out.println("unlocked"); } } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onStallWarning(StallWarning sw) { System.out.println(sw.getMessage()); } }; FilterQuery fq = new FilterQuery(); String[] keywords = args; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); try { synchronized (lock) { lock.wait(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("returning statuses"); twitterStream.shutdown(); return statuses; }
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. ja 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:br.com.porcelli.hornetq.integration.twitter.support.TweetMessageConverterSupport.java
License:Apache License
public static ServerMessage buildMessage(final String queueName, final Status status) { final ServerMessage msg = new ServerMessageImpl(status.getId(), InternalTwitterConstants.INITIAL_MESSAGE_BUFFER_SIZE); msg.setAddress(new SimpleString(queueName)); msg.setDurable(true);//from w ww . j a va 2s .co m msg.putStringProperty(TwitterConstants.KEY_MSG_TYPE, MessageType.TWEET.toString()); msg.putStringProperty(TwitterConstants.KEY_CREATED_AT, read(status.getCreatedAt())); msg.putStringProperty(TwitterConstants.KEY_ID, read(status.getId())); msg.putStringProperty(TwitterConstants.KEY_TEXT, read(status.getText())); msg.putStringProperty(TwitterConstants.KEY_SOURCE, read(status.getSource())); msg.putStringProperty(TwitterConstants.KEY_TRUNCATED, read(status.isTruncated())); msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID, read(status.getInReplyToStatusId())); msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_USER_ID, read(status.getInReplyToUserId())); msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_SCREEN_NAME, read(status.getInReplyToScreenName())); msg.putStringProperty(TwitterConstants.KEY_RETWEET, read(status.isRetweet())); msg.putStringProperty(TwitterConstants.KEY_FAVORITED, read(status.isFavorited())); msg.putStringProperty(TwitterConstants.KEY_ENTITIES_URLS_JSON, read(status.getURLEntities())); msg.putStringProperty(TwitterConstants.KEY_ENTITIES_HASHTAGS_JSON, read(status.getHashtagEntities())); msg.putStringProperty(TwitterConstants.KEY_ENTITIES_MENTIONS_JSON, read(status.getUserMentionEntities())); msg.putStringProperty(TwitterConstants.KEY_CONTRIBUTORS_JSON, read(status.getContributors())); if (status.getUser() != null) { buildUserData("", status.getUser(), msg); } GeoLocation gl; if ((gl = status.getGeoLocation()) != null) { msg.putStringProperty(TwitterConstants.KEY_GEO_LATITUDE, read(gl.getLatitude())); msg.putStringProperty(TwitterConstants.KEY_GEO_LONGITUDE, read(gl.getLongitude())); } Place place; if ((place = status.getPlace()) != null) { msg.putStringProperty(TwitterConstants.KEY_PLACE_ID, read(place.getId())); msg.putStringProperty(TwitterConstants.KEY_PLACE_URL, read(place.getURL())); msg.putStringProperty(TwitterConstants.KEY_PLACE_NAME, read(place.getName())); msg.putStringProperty(TwitterConstants.KEY_PLACE_FULL_NAME, read(place.getFullName())); msg.putStringProperty(TwitterConstants.KEY_PLACE_COUNTRY_CODE, read(place.getCountryCode())); msg.putStringProperty(TwitterConstants.KEY_PLACE_COUNTRY, read(place.getCountry())); msg.putStringProperty(TwitterConstants.KEY_PLACE_STREET_ADDRESS, read(place.getStreetAddress())); msg.putStringProperty(TwitterConstants.KEY_PLACE_TYPE, read(place.getPlaceType())); msg.putStringProperty(TwitterConstants.KEY_PLACE_GEO_TYPE, read(place.getGeometryType())); msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_TYPE, read(place.getBoundingBoxType())); msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_COORDINATES_JSON, read(place.getBoundingBoxCoordinates().toString())); msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_GEOMETRY_COORDINATES_JSON, read(place.getGeometryCoordinates().toString())); } msg.putStringProperty(TwitterConstants.KEY_RAW_JSON, status.toString()); return msg; }
From source file:br.unisal.twitter.bean.TwitterBean.java
public void postingToTwitter() { try {//from w w w . ja va2 s . c o m TwitterFactory TwitterFactory = new TwitterFactory(); Twitter twitter = TwitterFactory.getSingleton(); String message = getTwitt(); Status status = twitter.updateStatus(message); /*String s = "status.toString() = " + status.toString() + "status.getInReplyToScreenName() = " + status.getInReplyToScreenName() + "status.getSource() = " + status.getSource() + "status.getText() = " + status.getText() + "status.getContributors() = " + Arrays.toString(status.getContributors()) + "status.getCreatedAt() = " + status.getCreatedAt() + "status.getCurrentUserRetweetId() = " + status.getCurrentUserRetweetId() + "status.getGeoLocation() = " + status.getGeoLocation() + "status.getId() = " + status.getId() + "status.getInReplyToStatusId() = " + status.getInReplyToStatusId() + "status.getInReplyToUserId() = " + status.getInReplyToUserId() + "status.getPlace() = " + status.getPlace() + "status.getRetweetCount() = " + status.getRetweetCount() + "status.getRetweetedStatus() = " + status.getRetweetedStatus() + "status.getUser() = " + status.getUser() + "status.getAccessLevel() = " + status.getAccessLevel() + "status.getHashtagEntities() = " + Arrays.toString(status.getHashtagEntities()) + "status.getMediaEntities() = " + Arrays.toString(status.getMediaEntities()) + "status.getURLEntities() = " + Arrays.toString(status.getURLEntities()) + "status.getUserMentionEntities() = " + Arrays.toString(status.getUserMentionEntities());*/ String s = "status.getId() = " + status.getId() + "\nstatus.getUser() = " + status.getUser().getName() + " - " + status.getUser().getScreenName() + "\nstatus.getGeoLocation() = " + status.getGeoLocation() + "\nstatus.getText() = " + status.getText(); setTwittsResult(s); this.getUiMsg().setSubmittedValue(""); this.getUiResultMsg().setSubmittedValue(getTwittsResult()); } catch (TwitterException ex) { LOG.error("Erro no postingToTwitter() - " + ex.toString()); } }
From source file:br.unisal.twitter.bean.TwitterBean.java
public void consultarTweets() { TwitterFactory TwitterFactory = new TwitterFactory(); Twitter twitter = TwitterFactory.getSingleton(); List<Status> tweets = new ArrayList<>(); Query query = new Query(getTwittQuery()); try {/*from w w w . j a v a2 s .com*/ QueryResult result = twitter.search(query); tweets.addAll(result.getTweets()); StringBuilder builder = new StringBuilder(); double lon = 0; double lat = 0; for (Status s : tweets) { if ((s.getGeoLocation() != null)) { lon = s.getGeoLocation().getLongitude(); lat = s.getGeoLocation().getLatitude(); } Tweet t = new Tweet(s.getUser().getName(), s.getUser().getLocation(), s.getText(), s.getCreatedAt(), lat, lon); dao.insert(t); builder.append(t.toString()); } this.getUiResultQuery().setSubmittedValue(builder.toString()); } catch (TwitterException te) { System.out.println("Failed to search tweets: " + te.getMessage()); } }
From source file:cats.twitter.collect.Collect.java
@Override public boolean runCollect() { dateEnd = Calendar.getInstance(); dateEnd.add(Calendar.DAY_OF_YEAR, duree); cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);//ww w. j a v a 2 s. c om cb.setOAuthConsumerKey(user.getConsumerKey()); System.out.println("CONSUMER KEY " + user.getConsumerKey()); cb.setOAuthConsumerSecret(user.getConsumerSecret()); System.out.printf("CONSUMER SECRET " + user.getConsumerSecret()); cb.setOAuthAccessToken(user.getToken()); System.out.printf("TOKEN" + user.getToken()); cb.setOAuthAccessTokenSecret(user.getTokenSecret()); System.out.printf("TOKEN SECRET " + user.getTokenSecret()); twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); setStatus(State.WAITING_FOR_CONNECTION); StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { if (!corpus.getState().equals(State.INPROGRESS)) { setStatus(State.INPROGRESS); } if (status.getCreatedAt().after(dateEnd.getTime())) { shutdown(); } else if (corpus.getLang() == null || corpus.getLang().equals(status.getLang())) { Tweet t = new Tweet(); t.setText(status.getText().replace("\r", "\n")); t.setAuthor(status.getUser().getId()); t.setId(status.getId()); t.setDate(status.getCreatedAt()); if (status.getGeoLocation() != null) t.setLocation(status.getGeoLocation().toString()); t.setName(status.getUser().getName()); t.setDescriptionAuthor(status.getUser().getDescription()); t.setLang(status.getLang()); t.setCorpus(corpus); if (tweetRepository != null) tweetRepository.save(t); } } @Override public void onDeletionNotice(StatusDeletionNotice sdn) { System.out.println(sdn); } @Override public void onTrackLimitationNotice(int i) { corpus.setLimitationNotice(i); corpus = corpusRepository.save(corpus); } @Override public void onScrubGeo(long l, long l1) { System.out.println(l + "" + l1); } @Override public void onStallWarning(StallWarning sw) { System.out.println(sw); } @Override public void onException(Exception excptn) { corpus.setErrorMessage(excptn.getMessage()); setStatus(State.ERROR); excptn.printStackTrace(); } }; twitterStream.addListener(listener); twitterStream.filter(filter); return false; }
From source file:cc.twittertools.corpus.demo.ReadStatuses.java
License:Apache License
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input directory or file") .create(INPUT_OPTION));//from www . j a v a 2s . co m options.addOption(VERBOSE_OPTION, false, "print logging output every 10000 tweets"); options.addOption(DUMP_OPTION, false, "dump statuses"); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!cmdline.hasOption(INPUT_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ReadStatuses.class.getName(), options); System.exit(-1); } PrintStream out = new PrintStream(System.out, true, "UTF-8"); StatusStream stream; // Figure out if we're reading from HTML SequenceFiles or JSON. File file = new File(cmdline.getOptionValue(INPUT_OPTION)); if (!file.exists()) { System.err.println("Error: " + file + " does not exist!"); System.exit(-1); } if (file.isDirectory()) { stream = new JsonStatusCorpusReader(file); } else { stream = new JsonStatusBlockReader(file); } int cnt = 0; Status status; while ((status = stream.next()) != null) { if (cmdline.hasOption(DUMP_OPTION)) { String text = status.getText(); if (text != null) { text = text.replaceAll("\\s+", " "); text = text.replaceAll("\0", ""); } out.println(String.format("%d\t%s\t%s\t%s", status.getId(), status.getUser().getScreenName(), status.getCreatedAt(), text)); } cnt++; if (cnt % 10000 == 0 && cmdline.hasOption(VERBOSE_OPTION)) { LOG.info(cnt + " statuses read"); } } stream.close(); LOG.info(String.format("Total of %s statuses read.", cnt)); }