List of usage examples for twitter4j StatusListener StatusListener
StatusListener
From source file:com.hortonworks.amuise.cdrstorm.kafka.producers.CDRTestDataProducer.java
private void start() { /**/*from w w w . j a va2 s . c om*/ * Kafka Twitter Producer properties * */ Properties twitterconprops = new Properties(); twitterconprops.put("metadata.broker.list", globalconfigs.getProperty("twitter4j.brokerlist")); twitterconprops.put("serializer.class", globalconfigs.getProperty("twitter4j.serializer")); twitterconprops.put("request.required.acks", globalconfigs.getProperty("twitter4j.requiredacks")); ProducerConfig twitterproducerconfig = new ProducerConfig(twitterconprops); final Producer<String, String> twitterproducer = new Producer<String, String>(twitterproducerconfig); /** * Kafka CDR Producer properties * */ Properties cdrconprops = new Properties(); cdrconprops.put("metadata.broker.list", globalconfigs.getProperty("cdr.brokerlist")); cdrconprops.put("serializer.class", globalconfigs.getProperty("cdr.serializer")); cdrconprops.put("request.required.acks", globalconfigs.getProperty("cdr.requiredacks")); ProducerConfig cdrproducerconfig = new ProducerConfig(cdrconprops); final Producer<String, String> cdrproducer = new Producer<String, String>(cdrproducerconfig); /** * Twitter4j properties * */ ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(globalconfigs.getProperty("twitter4j.consumerkey")); cb.setOAuthConsumerSecret(globalconfigs.getProperty("twitter4j.consumersecretkey")); cb.setOAuthAccessToken(globalconfigs.getProperty("twitter4j.accesstokenkey")); cb.setOAuthAccessTokenSecret(globalconfigs.getProperty("twitter4j.accesstokensecretkey")); cb.setJSONStoreEnabled(true); cb.setIncludeEntitiesEnabled(true); twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); /** * Twitter listener * */ StatusListener listener = new StatusListener() { // The onStatus method is executed every time a new tweet comes // in. public void onStatus(Status status) { StringBuilder sb = new StringBuilder(); sb.append(status.getUser().getScreenName()); sb.append("|"); sb.append(status.getCreatedAt()); sb.append("|"); sb.append(status.getRetweetCount()); sb.append("|"); sb.append(status.getSource()); sb.append("|"); sb.append(status.getText()); //call CDR create message String cdrmessage = createCDRMessage(status.getText()); //Debug output System.out.println("_________________________________________________________"); System.out.println(sb.toString()); System.out.println("cdr message: " + cdrmessage); System.out.println("_________________________________________________________"); //call producer for tweet KeyedMessage<String, String> twitterdata = new KeyedMessage<String, String>( globalconfigs.getProperty("twitter4j.kafkatopic"), sb.toString()); twitterproducer.send(twitterdata); //call producer for cdr KeyedMessage<String, String> cdrmessagedata = new KeyedMessage<String, String>( globalconfigs.getProperty("cdr.kafkatopic"), cdrmessage); cdrproducer.send(cdrmessagedata); } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onScrubGeo(long userId, long upToStatusId) { } public void onException(Exception ex) { System.out.println("General Exception: shutting down Twitter sample stream..."); System.out.println(ex.getMessage()); ex.printStackTrace(); twitterStream.shutdown(); } public void onStallWarning(StallWarning warning) { } }; twitterStream.addListener(listener); // Filter stream with targeted words String filterstring = globalconfigs.getProperty("twitter4j.filterwords"); FilterQuery filterq = new FilterQuery(); filterq.track(filterstring.split(",")); twitterStream.filter(filterq); //twitterStream.sample(); }
From source file:com.isdp.twitterposter.TwitterManager.java
License:Open Source License
public void startTwitterStream() { TwitterStream twitterStream = new TwitterStreamFactory(configuration).getInstance(); StatusListener listener = new StatusListener() { @Override//from ww w. ja v a 2s . c om public void onStatus(Status status) { try { System.out.println("Text recieved: @" + status.getUser().getScreenName() + " - " + status.getText() + "\n"); StringTokenizer st = new StringTokenizer(status.getText(), " "); //first token indicates search engine String searchEngine = st.nextToken(); //burn the next random token String randToken = st.nextToken(); //next token indicates max number of results to return int maxResults = Integer.parseInt(st.nextToken()); String searchString = ""; while (st.hasMoreTokens()) { searchString += st.nextToken() + " "; } if (searchEngine.equals(GoogleManager.TAG_YELP)) { String[] results = GoogleManager.getInstance().trySearch(searchString, GoogleManager.SEARCH_YELP); if (results != null) { for (int i = 0; i < results.length && i < maxResults; ++i) { String tweetMsg = Util.truncateString(results[i], TWITTER_CHARACTER_LIMIT); System.out.println("Tweeting" + tweetMsg); tweet(tweetMsg); } } else if (results == null || results.length == 0) { tweet(Util.generateRandomString(7) + "\n" + "No results found!"); } } else if (searchEngine.equals(GoogleManager.TAG_WIKI)) { String[] results = GoogleManager.getInstance().trySearch(searchString, GoogleManager.SEARCH_WIKI); if (results != null) { for (int i = 0; i < results.length && i < maxResults; ++i) { String tweetMsg = Util.generateRandomString(3) + "\n" + Util.shortenText(results[i]); tweetMsg = Util.truncateString(tweetMsg, TWITTER_CHARACTER_LIMIT); System.out.println("Tweeting " + tweetMsg); tweet(tweetMsg); } } else if (results == null || results.length == 0) { tweet(Util.generateRandomString(7) + "\n" + "No results found!"); } } } catch (Exception e) { } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println( "onDeletionNotice: Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out .println("onTrackLimitationNotice: Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { System.out.println( "onScrubGeo: Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { System.out.println("onStallWarning: Got stall warning:" + warning); } @Override public void onException(Exception ex) { System.out.println("onException" + ex.toString()); } }; twitterStream.addListener(listener); FilterQuery tweetFilterQuery = new FilterQuery(); tweetFilterQuery.follow(new long[] { FOLLOW_ID }); twitterStream.filter(tweetFilterQuery); }
From source file:com.isdp.twitterposterandroid.TwitterManager.java
License:Open Source License
public void startTwitterStream() { TwitterStream twitterStream = new TwitterStreamFactory(configuration).getInstance(); StatusListener listener = new StatusListener() { @Override//from ww w. j av a2s .c om public void onStatus(Status status) { try { Log.d("Text recieved", "@" + status.getUser().getScreenName() + " - " + status.getText() + "\n"); StringTokenizer st = new StringTokenizer(status.getText(), " "); //first token indicates search engine String searchEngine = st.nextToken(); //burn the next random token String randToken = st.nextToken(); //next token indicates max number of results to return int maxResults = Integer.parseInt(st.nextToken()); String searchString = ""; while (st.hasMoreTokens()) { searchString += st.nextToken() + " "; } if (searchEngine.equals(GoogleManager.TAG_YELP)) { String[] results = GoogleManager.getInstance().trySearch(searchString, GoogleManager.SEARCH_YELP); if (results != null) { for (int i = 0; i < results.length && i < maxResults; ++i) { String tweetMsg = Util.truncateString(results[i], TWITTER_CHARACTER_LIMIT); Log.d("Tweeting", tweetMsg); tweet(tweetMsg); } } else if (results == null || results.length == 0) { tweet(Util.generateRandomString(7) + "\n" + "No results found!"); } } else if (searchEngine.equals(GoogleManager.TAG_WIKI)) { String[] results = GoogleManager.getInstance().trySearch(searchString, GoogleManager.SEARCH_WIKI); if (results != null) { for (int i = 0; i < results.length && i < maxResults; ++i) { String tweetMsg = Util.generateRandomString(3) + "\n" + Util.shortenText(results[i]); tweetMsg = Util.truncateString(tweetMsg, TWITTER_CHARACTER_LIMIT); Log.d("Tweeting", tweetMsg); tweet(tweetMsg); } } else if (results == null || results.length == 0) { tweet(Util.generateRandomString(7) + "\n" + "No results found!"); } } } catch (Exception e) { } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { Log.d("onDeletionNotice", "Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { Log.d("onTrackLimitationNotice", "Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { Log.d("onScrubGeo", "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { Log.d("onStallWarning", "Got stall warning:" + warning); } @Override public void onException(Exception ex) { Log.d("onException", ex.toString()); } }; twitterStream.addListener(listener); FilterQuery tweetFilterQuery = new FilterQuery(); tweetFilterQuery.follow(new long[] { FOLLOW_ID }); twitterStream.filter(tweetFilterQuery); }
From source file:com.left8.evs.utilities.dsretriever.TweetsRetriever.java
License:Open Source License
/** * Method that handles the Twitter streaming API. <br> * <b>WARNING:</b> Method does not terminate by itself, due to the fact that * the streamer runs in a different thread. * @param keywords The keywords for which the streamer searches for tweets. * @param mongoDB A handler for the MongoDB database. * @param config A configuration object. *//*from w ww . ja va 2 s. c o m*/ public final void retrieveTweetsWithStreamingAPI(String[] keywords, MongoHandler mongoDB, Config config) { ConfigurationBuilder cb = getAuthorization(); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); final StatusListener listener; listener = new StatusListener() { @Override public final void onStatus(Status status) { //Insert tweet to MongoDB mongoDB.insertSingleTweetIntoMongoDB(status, "NULL"); } @Override public final void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { Utilities.printMessageln("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public final void onTrackLimitationNotice(int numberOfLimitedStatuses) { Utilities.printMessageln("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public final void onScrubGeo(long userId, long upToStatusId) { Utilities.printMessageln("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public final void onStallWarning(StallWarning warning) { Utilities.printMessageln("Got stall warning:" + warning); } @Override public final void onException(Exception ex) { ex.printStackTrace(System.out); } }; FilterQuery fq = new FilterQuery(); fq.language("en"); //Set language of tweets to "English" fq.track(keywords); //Load the search terms twitterStream.addListener(listener); //Start listening to the stream twitterStream.filter(fq); //Apply the search filters }
From source file:com.microsoft.example.TwitterSampleSpout.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 av a2s .c om 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); if (keyWords.length == 0) { twitterStream.sample(); } else { FilterQuery query = new FilterQuery().track(keyWords); twitterStream.filter(query); } }
From source file:com.mycompany.omnomtweets.Search.java
public static void main(String[] args) throws TwitterException { System.setProperty("twitter4j.loggerFactory", "twitter4j.internal.logging.NullLoggerFactory"); if (args.length >= 1) { candidate = Candidate.valueOf(args[0]); System.out.println("Getting tweets on " + candidate.name); } else {/*from ww w. j av a 2 s. c o m*/ //System.out.println("You didn't give me a candidate, so you get TRUMP"); } //System.out.println("Starting at: " + sdf.format(new Date())); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(candidate.consumerKey) .setOAuthConsumerSecret(candidate.consumerSecret).setOAuthAccessToken(candidate.accessToken) .setOAuthAccessTokenSecret(candidate.accessTokenSecret); final ArrayList<Status> statuses = new ArrayList<>(); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { public void onStatus(Status status) { statuses.add(status); writeToStdErr(status); //Removed because writing to STD OUT instead. //System.out.println("statuses: " + statuses.size()); if (statuses.size() >= 100) { /*System.out.println("Writing " + statuses.size() + " to file " + "at: " + sdf.format(new Date()));*/ writeTweetsToFile(statuses, candidate.name + "StreamTweets.txt"); statuses.clear(); } } 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) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; FilterQuery fq = new FilterQuery(); String keywords[] = candidate.aliases; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); }
From source file:com.saugereau.spout.TwitterSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<Status>(1000); _collector = collector;/* ww w.j ava 2 s . c om*/ 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) { LOG.info("Got track limitation notice:" + i); } @Override public void onScrubGeo(long l, long l1) { } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; _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); if (keyWords.length == 0) { _twitterStream.sample(); } else { FilterQuery query = new FilterQuery().track(keyWords); query.language(new String[] { "en" }); _twitterStream.filter(query); } }
From source file:com.sinfonier.spouts.Twitter.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { xml = new XMLProperties(spoutName, ComponentType.SPOUT, xmlPath); _collector = collector;//ww w . j av a2 s. c o m SinfonierUtils.broadcastWorker((String) conf.get(Config.TOPOLOGY_NAME), context); twitterStream = createTwitterStream(); queue = new LinkedBlockingQueue<String>(1000); StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { queue.offer(TwitterObjectFactory.getRawJSON(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 e) { } @Override public void onStallWarning(StallWarning warning) { } }; twitterStream.addListener(listener); List<Object> rawKeywords = xml.getList("keyword"); // String rawKeywords = xml.get("keywords"); if (rawKeywords.isEmpty()) { twitterStream.sample(); } else { List<String> keywords = new ArrayList<String>(); for (Object field : rawKeywords) { keywords.add(((String) field).trim()); } FilterQuery query = new FilterQuery().track(keywords.toArray(new String[keywords.size()])); twitterStream.filter(query); } }
From source file:com.spec.CityTwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets./* ww w .j a v a 2 s .co m*/ */ @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 flag = false; for (int i = 0; i < cities.length; i++) { if (status.getUser().getLocation().toLowerCase().contains(cities[i].toLowerCase())) flag = true; } if (flag) { logger.debug(status.getUser().getLocation() + " : " + flag); headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime())); Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status.getUser()).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 sw) { } }; //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); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); // Set up a filter to pull out industry-relevant tweets 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); } super.start(); }
From source file:com.sstrato.flume.source.TwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets.// w w w.jav a 2s.co m */ @Override public void start() { // ? ? final ChannelProcessor channel = getChannelProcessor(); final Map<String, String> headers = new HashMap<String, String>(); // ? twitter4j? StatusListener ? ?. StatusListener listener = new StatusListener() { // The onStatus method is executed every time a new tweet comes in. public void onStatus(Status status) { // header raw json ? ? . 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) { } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}", new String[] { this.consumerKey, this.accessToken }); // Set up the stream's listener (defined above), and set any necessary // security information. twitterStream.addListener(listener); twitterStream.setOAuthConsumer(this.consumerKey, this.consumerSecret); AccessToken token = new AccessToken(this.accessToken, this.accessTokenSecret); twitterStream.setOAuthAccessToken(token); // Set up a filter to pull out industry-relevant tweets 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); } super.start(); }