List of usage examples for twitter4j RawStreamListener RawStreamListener
RawStreamListener
From source file:PrintRawSampleStream.java
License:Apache License
/** * Main entry of this application./* w w w . j a va 2 s . c o m*/ * * @param args arguments doesn't take effect with this example * @throws TwitterException when Twitter service or network is unavailable */ public static void main(String[] args) throws TwitterException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(""); cb.setOAuthConsumerSecret(""); cb.setOAuthAccessToken(""); cb.setOAuthAccessTokenSecret(""); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); RawStreamListener listener = new RawStreamListener() { @Override public void onMessage(String rawJSON) { System.out.println(rawJSON); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; twitterStream.addListener(listener); twitterStream.sample(); }
From source file:StreamEmoContentTime.java
License:Apache License
public static void main(String[] args) throws TwitterException { startup();//from w w w.j a va 2 s. c o m final JSONParser parser = new JSONParser(); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); RawStreamListener listener = new RawStreamListener() { @Override public void onMessage(String rawJSON) { //System.out.println(rawJSON); try { JSONObject json = (JSONObject) new JSONParser().parse(rawJSON); String lang = (String) json.get("lang"); if (lang.equals("en")) { String text = (String) json.get("text"); String time = (String) json.get("created_at"); int emoscore = scoreTweet(text); try { Statement stmt = c.createStatement(); String sql = "INSERT INTO tbl2(time, score, text) " + "VALUES ('" + time + "','" + emoscore + "','" + text.replaceAll("'", "''") + "')"; System.out.println("Score: " + emoscore + "\t" + text); stmt.executeUpdate(sql); stmt.close(); //c.commit(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(rawJSON); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; twitterStream.addListener(listener); twitterStream.sample(); }
From source file:cc.twittertools.stream.GatherStatusStream.java
License:Apache License
public static void main(String[] args) throws TwitterException { PatternLayout layoutStandard = new PatternLayout(); layoutStandard.setConversionPattern("[%p] %d %c %M - %m%n"); PatternLayout layoutSimple = new PatternLayout(); layoutSimple.setConversionPattern("%m%n"); // Filter for the statuses: we only want INFO messages LevelRangeFilter filter = new LevelRangeFilter(); filter.setLevelMax(Level.INFO); filter.setLevelMin(Level.INFO); filter.setAcceptOnMatch(true);// w w w .j a va2s. c o m filter.activateOptions(); TimeBasedRollingPolicy statusesRollingPolicy = new TimeBasedRollingPolicy(); statusesRollingPolicy.setFileNamePattern("statuses.log" + HOUR_ROLL); statusesRollingPolicy.activateOptions(); RollingFileAppender statusesAppender = new RollingFileAppender(); statusesAppender.setRollingPolicy(statusesRollingPolicy); statusesAppender.addFilter(filter); statusesAppender.setLayout(layoutSimple); statusesAppender.activateOptions(); TimeBasedRollingPolicy warningsRollingPolicy = new TimeBasedRollingPolicy(); warningsRollingPolicy.setFileNamePattern("warnings.log" + HOUR_ROLL); warningsRollingPolicy.activateOptions(); RollingFileAppender warningsAppender = new RollingFileAppender(); warningsAppender.setRollingPolicy(statusesRollingPolicy); warningsAppender.setThreshold(Level.WARN); warningsAppender.setLayout(layoutStandard); warningsAppender.activateOptions(); ConsoleAppender consoleAppender = new ConsoleAppender(); consoleAppender.setThreshold(Level.WARN); consoleAppender.setLayout(layoutStandard); consoleAppender.activateOptions(); // configures the root logger Logger rootLogger = Logger.getRootLogger(); rootLogger.setLevel(Level.INFO); rootLogger.removeAllAppenders(); rootLogger.addAppender(consoleAppender); rootLogger.addAppender(statusesAppender); rootLogger.addAppender(warningsAppender); // creates a custom logger and log messages final Logger logger = Logger.getLogger(GatherStatusStream.class); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); RawStreamListener rawListener = new RawStreamListener() { @Override public void onMessage(String rawString) { cnt++; logger.info(rawString); if (cnt % 100 == 0) { System.out.println(cnt + " messages received."); } } @Override public void onException(Exception ex) { logger.warn(ex); } }; twitterStream.addListener(rawListener); FilterQuery fq = new FilterQuery(); /* double locations[][]={{112.0,-44.0},{155.0,-10.5}}; fq.locations(locations);*/ long ids[] = { 87818409, 16675569 }; fq.follow(ids); twitterStream.filter(fq); //twitterStream.sample(); }
From source file:com.esri.geoevent.transport.twitter.TwitterInboundTransport.java
License:Apache License
private void receiveData() { try {/*from w w w. j a v a 2 s . c om*/ applyProperties(); setRunningState(RunningState.STARTED); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(consumerKey); cb.setOAuthConsumerSecret(consumerSecret); cb.setOAuthAccessToken(accessToken); cb.setOAuthAccessTokenSecret(accessTokenSecret); twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); RawStreamListener rl = new RawStreamListener() { @Override public void onException(Exception ex) { LOGGER.error("INBOUND_TRANSPORT_RAW_STREAM_LISTERNER_EXCEPTION", ex.getMessage()); } @Override public void onMessage(String rawString) { receive(rawString); } }; FilterQuery fq = new FilterQuery(); String keywords[] = tracks; if (follows != null && follows.length > 0) fq.follow(follows); else if (keywords != null && keywords.length > 0) fq.track(keywords); else if (locations != null) fq.locations(locations); else throw new Exception("INBOUND_TRANSPORT_NOFILTER_ERROR"); fq.count(count); LOGGER.info("INBOUND_TRANSPORT_FILTER", filterString); twitterStream.addListener(rl); twitterStream.filter(fq); } catch (Throwable ex) { LOGGER.error("UNEXPECTED_ERROR", ex); setRunningState(RunningState.ERROR); } }
From source file:com.storm.demo.TwitterSampleSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<String>(1000); _collector = collector;//w w w . j a v a 2s .c o m // StatusListener listener = new StatusListener() { // // @Override // public void onStatus(Status status) { // System.out.println(DataObjectFactory.getRawJSON(status)); // queue.offer(status); // } // // @Override // public void onDeletionNotice(StatusDeletionNotice sdn) { // } // // @Override // public void onTrackLimitationNotice(int i) { // } // // @Override // public void onScrubGeo(long l, long l1) { // } // // @Override // public void onException(Exception ex) { // } // // @Override // public void onStallWarning(StallWarning arg0) { // // TODO Auto-generated method stub // // } // // }; RawStreamListener rawListener = new RawStreamListener() { @Override public void onMessage(String rawJSON) { //System.out.println(rawJSON); queue.offer(rawJSON); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); //twitterStream.addListener(listener); twitterStream.addListener(rawListener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); if (keyWords.length == 0) { twitterStream.sample(); } else { FilterQuery query = new FilterQuery().track(keyWords); twitterStream.filter(query); } }
From source file:example.justids.java
License:Apache License
/** * Usage: java twitter4j.examples.search.SearchTweets [query] * * @param args//from ww w . ja v a 2s . co m */ public static void main(String[] args) { StatusListener listener = new StatusListener() { public Double count = 0d; Date started = new Date(); Date previous = new Date(); @Override public void onStatus(Status status) { try { File file = new File("Filtered_over1percent_lab_pc_obama_all.txt"); File file2 = new File("Filtered_over1percent_lab_pc_obama.txt"); // if file doesnt exists, then create it FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); FileWriter fw2 = new FileWriter(file2.getAbsoluteFile(), true); BufferedWriter bw2 = new BufferedWriter(fw2); if (this.count % 1000 == 0) { Date finished10k = new Date(); System.out.println("\n\n\n\n AVERAGE RATE OF TWEETS is " + (this.count * 1000 / (finished10k.getTime() - this.started.getTime()))); System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime())); System.out.println(this.count); System.out.println(finished10k.getTime() + " " + this.started.getTime() + " " + (finished10k.getTime() - this.started.getTime())); System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " " + (finished10k.getTime() - this.previous.getTime())); System.out.println(status.getSource()); System.out.println("\n\n\n\n"); this.previous = finished10k; } this.count++; // System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count); bw.write(status.getId() + "\n"); bw.close(); if (status.getText().contains("obama")) { bw2.write(status.getId() + "\n"); bw2.close(); } } catch (IOException e) { e.printStackTrace(); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println(statusDeletionNotice.getUserId()+" has deleted this tweet"); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println(numberOfLimitedStatuses + " are missing from here"); } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); RawStreamListener rawst = new RawStreamListener() { public Double count = 0d; public Double lengthsum = 0d; Date started = new Date(); Date previous = new Date(); @Override public void onMessage(String message) { if (!message.startsWith("{\"delete")) { count++; lengthsum += message.length(); if (count % 1000 == 0) { System.out.println(lengthsum / count); // lengthsum=0d; // count=0d; } } // TODO Auto-generated method stub } @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } }; // twitterStream.addListener(rawst); String[] searchfor = { "language", "people", "problem", "microsoft", "epidemic", "obama", "zoo" }; FilterQuery query = new FilterQuery(); query.track(searchfor); twitterStream.addListener(listener); twitterStream.filter(query); // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. // twitterStream.sample(); }
From source file:example.search.java
License:Apache License
/** * Usage: java twitter4j.examples.search.SearchTweets [query] * * @param args/*from www. ja v a2 s. c om*/ */ public static void main(String[] args) { StatusListener listener = new StatusListener() { public Double count = 0d; Date started = new Date(); Date previous = new Date(); @Override public void onStatus(Status status) { try { File file = new File("whythissucks.txt"); // if file doesnt exists, then create it FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); if (this.count % 1000 == 0) { Date finished10k = new Date(); System.out.println("\n\n\n\n AVERAGE RATE OF TWEETS is " + (this.count * 1000 / (finished10k.getTime() - this.started.getTime()))); System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime())); System.out.println(this.count); System.out.println(finished10k.getTime() + " " + this.started.getTime() + " " + (finished10k.getTime() - this.started.getTime())); System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " " + (finished10k.getTime() - this.previous.getTime())); System.out.println(status.getSource()); System.out.println("\n\n\n\n"); this.previous = finished10k; } this.count++; // System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count); bw.write(status.getId() + "|" + status.getText() + "\n"); bw.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println(statusDeletionNotice.getUserId()+" has deleted this tweet"); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("limited " + numberOfLimitedStatuses); } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub System.out.println(arg0.getMessage()); } }; TwitterInstanceCreator tic = new TwitterInstanceCreator(); RawStreamListener rawst = new RawStreamListener() { public Double count = 0d; public Double lengthsum = 0d; Date started = new Date(); Date previous = new Date(); String filename; String previousfilename = ""; @Override public void onMessage(String message) { if (!message.startsWith("{\"delete")) { try { // if file doesnt exists, then create it if (this.count % 1000 == 0) { Date finished10k = new Date(); System.out.println("\n\n\n\n AVERAGE RATE OF TWEETS is " + (this.count * 1000 / (finished10k.getTime() - this.started.getTime()))); System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime())); System.out.println(this.count); System.out.println(finished10k.getTime() + " " + this.started.getTime() + " " + (finished10k.getTime() - this.started.getTime())); System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " " + (finished10k.getTime() - this.previous.getTime())); System.out.println("\n\n\n\n"); this.previous = finished10k; } File file = new File("tweetstoimport/" + this.filename); FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); this.count++; // System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count); bw.write(message + "\n"); bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } // TODO Auto-generated method stub } @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } }; // twitterStream.addListener(rawst); // String[] searchfor={"flu", "influenza", "fever", "cough", "sore", "throat", "sore throat", "headache"}; // FilterQuery query=new FilterQuery(); // query.track(searchfor); TwitterStream twitterStream = tic.getStream(1); twitterStream.addListener(listener); // twitterStream.filter(query); // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. twitterStream.sample(); //Filters from the stream //twitterStream.filter(query); }
From source file:stream.PrintRawSampleStream.java
License:Apache License
/** * Main entry of this application./*from w ww .ja v a 2 s . c o m*/ * * @param args arguments doesn't take effect with this example */ public static void main(String[] args) throws TwitterException { TwitterStream twitterStream = new TwitterStreamFactory(Data.getConf().build()).getInstance(); RawStreamListener listener = new RawStreamListener() { public void onMessage(String rawJSON) { System.out.println(rawJSON); } public void onException(Exception ex) { ex.printStackTrace(); } }; twitterStream.addListener(listener); twitterStream.sample(); }
From source file:twitter4j.examples.stream.PrintRawSampleStream.java
License:Apache License
/** * Main entry of this application./*w w w . j a v a 2s . c om*/ * * @param args arguments doesn't take effect with this example * @throws TwitterException when Twitter service or network is unavailable */ public static void main(String[] args) throws TwitterException { TwitterStream twitterStream = new TwitterStreamFactory().getInstance().addListener(new RawStreamListener() { @Override public void onMessage(String rawJSON) { System.out.println(rawJSON); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }).sample(); }