List of usage examples for twitter4j StatusListener StatusListener
StatusListener
From source file:org.twitter.sample.main.Stream.java
public void getTweetsFromTwitter() { StatusListener listener = new StatusListener() { private boolean logQueueFull = true; @Override// ww w . j a v a 2 s . co m public void onException(Exception e) { log.error(e); } @Override public void onDeletionNotice(StatusDeletionNotice notice) { } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onStatus(Status status) { db.insert(status); } @Override public void onTrackLimitationNotice(int notice) { log.warn("*** TRACK LIMITATION REACHED: " + notice + " ***"); } @Override public void onStallWarning(StallWarning arg0) { log.warn("*** STALL WARNING: " + arg0); } }; ConfigurationBuilder twitterConfig = new ConfigurationBuilder(); twitterConfig.setDebugEnabled(false); twitterConfig.setOAuthAccessTokenSecret(access_token_secret); twitterConfig.setOAuthAccessToken(access_token); twitterConfig.setOAuthConsumerKey(consumer_key); twitterConfig.setOAuthConsumerSecret(consumer_secret); twitterConfig.setJSONStoreEnabled(true); TwitterStreamFactory fact = new TwitterStreamFactory(twitterConfig.build()); this.twitterStream = fact.getInstance(); this.twitterStream.addListener(listener); FilterQuery filterQuery = new FilterQuery(); filterQuery.language(new String[] { "en" }); this.twitterStream.filter(filterQuery); this.twitterStream.sample(); }
From source file:org.umd.assignment.spout.TwitterSampleSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<String>(1000); _collector = collector;//from w w w .j a va2 s .c om StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { queue.offer(status.getText()); } @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) { } }; TwitterStreamFactory fact = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()); _twitterStream = fact.getInstance(); _twitterStream.addListener(listener); _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:org.unimonk.flume.source.twitter.TwitterSource.java
License:Apache License
@Override public void start() { final ChannelProcessor channel = getChannelProcessor(); StatusListener listener = new StatusListener() { @Override/*from w w w .ja va 2s . c o m*/ public void onStatus(Status status) { Tweet tweet = new Tweet(); tweet.setId(status.getId()); tweet.setUserId(status.getUser().getId()); tweet.setLatitude(status.getGeoLocation().getLatitude()); tweet.setLongitude(status.getGeoLocation().getLongitude()); tweet.setText(status.getText()); tweet.setCreatedAt(new Timestamp(status.getCreatedAt().getTime())); Event event = EventBuilder.withBody(tweet.toString(), Charsets.UTF_8); sourceCounter.incrementAppendReceivedCount(); try { channel.processEvent(event); sourceCounter.incrementEventAcceptedCount(); } catch (ChannelException ex) { logger.error("In Twitter source {} : Unable to process event due to exception {}.", getName(), ex); } } // This listener will ignore everything except for new tweets @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } @Override public void onScrubGeo(long userId, long upToStatusId) { } @Override public void onException(Exception ex) { } @Override public void onStallWarning(StallWarning arg0) { } }; logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}", new String[] { consumerKey, accessToken }); twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); if (keywords.length == 0) { logger.debug("Starting up Twitter sampling..."); twitterStream.sample(); } else { logger.debug("Starting up Twitter filtering..."); FilterQuery query = new FilterQuery().track(keywords); twitterStream.filter(query); } this.sourceCounter.start(); super.start(); }
From source file:org.vsepml.storm.twitter.StormTwitterStreamSpout.java
License:Apache License
@Override public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) { this.collector = spoutOutputCollector; queue = new LinkedBlockingQueue<Status>(1000); StatusListener listener = new StatusListener() { public void onStatus(Status status) { queue.offer(status);/*from www . j a v a 2 s . c om*/ } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } @Override public void onScrubGeo(long l, long l1) { } @Override public void onStallWarning(StallWarning stallWarning) { } public void onException(Exception ex) { ex.printStackTrace(); } }; ConfigurationBuilder twitterConf = new ConfigurationBuilder(); twitterConf.setIncludeEntitiesEnabled(true); twitterConf.setUseSSL(true); twitterConf.setUserStreamRepliesAllEnabled(true); twitterConf.setOAuthAccessToken(accessToken); twitterConf.setOAuthAccessTokenSecret(accessTokenSecret); twitterConf.setOAuthConsumerKey(consumerKey); twitterConf.setOAuthConsumerSecret(consumerSecret); twitterStream = new TwitterStreamFactory(twitterConf.build()).getInstance(); twitterStream.addListener(listener); // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. twitterStream.sample(); }
From source file:org.xmlsh.twitter.stream.java
License:BSD License
@Override public int run(List<XValue> args) throws Exception { Options opts = new Options(sCOMMON_OPTS + ",p=port:,track:,sample,json,sanitize", SerializeOpts.getOptionDefs()); opts.parse(args);/* ww w. j a v a2 s . co m*/ mSerializeOpts = this.getSerializeOpts(opts); final boolean bJson = opts.hasOpt("json"); final boolean bSanitize = opts.hasOpt("sanitize"); args = opts.getRemainingArgs(); final OutputPort port = mShell.getEnv().getOutputPort(opts.getOptStringRequired("port"), true); StatusListener listener = new StatusListener() { public void onStatus(Status status) { try { if (bJson) { String json = DataObjectFactory.getRawJSON(status); PrintWriter writer = port.asPrintWriter(mSerializeOpts); writer.println(json); writer.close(); } else { TwitterWriter mWriter = new TwitterWriter(port.asXMLStreamWriter(mSerializeOpts), bSanitize); mWriter.startDocument(); mWriter.startElement(TwitterWriter.kTWITTER_NS, "twitter"); mWriter.writeDefaultNamespace(); mWriter.write("status", status); mWriter.endElement(); mWriter.endDocument(); mWriter.closeWriter(); port.writeSequenceTerminator(mSerializeOpts); } } catch (Exception e) { onException(e); } } 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 arg0) { // TODO Auto-generated method stub } }; try { TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. // FilterQuery filter = new FilterQuery().track(Util.toStringArray(args)); // twitterStream.filter(filter); if (opts.hasOpt("sample")) twitterStream.sample(); else twitterStream.filter(new FilterQuery().track(opts.getOptStringRequired("track").split(","))); } finally { } return 0; }
From source file:SentimentAnalyses.PrintSampleStream.java
License:Apache License
public static void main(String[] args) throws TwitterException { final PrintSampleStream pr = new PrintSampleStream(); try {// www . j a va2s . com pr.LinkMongodb(); } catch (Exception e) { e.printStackTrace(); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("Em3WTI7jc90HcvKzPkTLQ") .setOAuthConsumerSecret("vg4p6rOF32bmffqRR8m0jAUClrxvtGiMB5PrSr3Zsw") .setOAuthAccessToken("1681973072-1q0zI0VPjHD3ttNuaBOL94frzCI9sXInxAcDK0w") .setOAuthAccessTokenSecret("ZRLkOyjmhHBkU1iNyEVNyIgIBsKrl0DUDKOcOMneYFYEM"); cb.setJSONStoreEnabled(true); TwitterStreamFactory tf = new TwitterStreamFactory(cb.build()); TwitterStream twitterStream = tf.getInstance(); StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); //System.out.println(status); String str = DataObjectFactory.getRawJSON(status); try { //JSONObject nnstr = new JSONObject(newstr); DBObject dbObject = (DBObject) JSON.parse(str); // System.out.println(dbObject); pr.collection.insert(dbObject); //System.out.println(dbObject); pr.count++; if (pr.count % 1000 == 0) System.out.println(pr.count); if (pr.count > 100000) { pr.mongo.close(); System.exit(0); } } catch (Exception e) { e.printStackTrace(); } } @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(); } }; twitterStream.addListener(listener); String[] trackArray; String[] Track = { "Malaysia Airlines", "Flight MH370", "Boeing-777", "Kuala Lumpur", "Bei jing" }; //trackArray[0] = "Obama"; //trackArray[1] = "Romney"; FilterQuery filter = new FilterQuery(); filter.track(Track); String[] lang = { "en" }; filter.language(lang); twitterStream.filter(filter); //pr.mongo.close(); }
From source file:source.TwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets./*w w w . java2 s . c om*/ */ @Override public void start() { // The channel is the piece of Flume that sits between the Source and // Sink, and is used to process events final ChannelProcessor channel = getChannelProcessor(); final Map<String, String> headers = new HashMap<String, String>(); // The StatusListener is a twitter4j API, which can be added to a // Twitter stream, and will execute methods every time a message comes // in through the stream StatusListener listener = new StatusListener() { // The onStatus method is executed every time a new tweet comes in public void onStatus(Status status) { // The EventBuilder is used to build an event using the headers // and the raw JSON of a tweet logger.debug(status.getUser().getScreenName() + ": " + status.getText()); headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime())); Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers); channel.processEvent(event); } // This listener will ignore everything except for new tweets public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onScrubGeo(long userId, long upToStatusId) { } public void onException(Exception ex) { } public void onStallWarning(StallWarning warning) { } }; logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}", new String[] { consumerKey, accessToken }); // Set up the stream's listener (defined above), and set any necessary // security information twitterStream.addListener(listener); // Set up a filter to pull out industry-relevant tweets logger.debug("Starting up Twitter filtering..."); FilterQuery query = new FilterQuery().count(0); if (locations == null) { logger.debug("No locations specified"); } else { String debugString = "Locations specified: "; debugString += "SW={" + locations[0][0] + ", " + locations[0][1] + "}, "; debugString += "NE={" + locations[1][0] + ", " + locations[1][1] + "}"; logger.debug(debugString); query.locations(locations); } if (keywords == null) { logger.debug("No keywords specified"); } else { String debugString = keywords.length + " keywords specified: "; for (int i = 0; i < keywords.length; i++) { debugString += keywords[i]; if (i != keywords.length - 1) { debugString += ", "; } } logger.debug(debugString); query.track(keywords); } twitterStream.filter(query); super.start(); }
From source file:storm.starter.spout.Q2FetchTweetSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<Status>(1000); _collector = collector;//from w w w . j a v a2 s . c o m StatusListener listener = new StatusListener() { @Override public void onStatus(Status 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 } }; TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); twitterStream.addListener(listener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); twitterStream.sample(); }
From source file:storm.starter.spout.Q2SeqTwitterSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<Status>(1000); _collector = collector;//w w w . j a va 2s . c o m StatusListener listener = new StatusListener() { @Override public void onStatus(Status 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 } }; TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); twitterStream.addListener(listener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); FilterQuery query = new FilterQuery(); query.track(keyWords); query.language(new String[] { "en" }); twitterStream.filter(query); }
From source file:storm.starter.spout.TwitterKeywordsSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<Status>(1000); _collector = collector;//w ww .j av a2 s .co m StatusListener listener = new StatusListener() { @Override public void onStatus(Status 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 } }; TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); twitterStream.addListener(listener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); _twitterStream = twitterStream; if (keyWords.length == 0) { twitterStream.sample(); } else { // TODO: Adjust the query below to also track locations and languages. FilterQuery query = new FilterQuery(); query.track(keyWords); query.language(new String[] { "en" }); twitterStream.filter(query); } }