List of usage examples for twitter4j StatusListener StatusListener
StatusListener
From source file:com.telefonica.iot.cygnus.sources.TwitterSource.java
License:Open Source License
@Override public synchronized void start() { LOGGER.info("Starting twitter source {} ...", this); documentCount = 0;//w w w . j a va 2 s . c o m startTime = System.currentTimeMillis(); exceptionCount = 0; totalTextIndexed = 0; skippedDocs = 0; batchEndTime = System.currentTimeMillis() + maxBatchDurationMillis; final ChannelProcessor channel = getChannelProcessor(); StatusListener listener = new StatusListener() { public void onStatus(Status status) { String jsonTweet = getStringJSONTweet(status); Event event = EventBuilder.withBody(jsonTweet, Charset.forName("UTF8")); eventBatch.add(event); if (eventBatch.size() >= maxBatchSize || System.currentTimeMillis() >= batchEndTime) { batchEndTime = System.currentTimeMillis() + maxBatchDurationMillis; channel.processEventBatch(eventBatch); // send batch of events (one per tweet) to the flume sink eventBatch.clear(); } documentCount++; if ((documentCount % REPORT_INTERVAL) == 0) { LOGGER.info(String.format("Processed %s docs", numFormatter.format(documentCount))); } if ((documentCount % STATS_INTERVAL) == 0) { logStats(); } } // 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 onStallWarning(StallWarning warning) { } public void onException(Exception e) { LOGGER.error("Exception while streaming tweets", e); } }; twitterStream.addListener(listener); if (haveFilters) { FilterQuery filterQuery = new FilterQuery(); if (haveCoordinateFilter) { filterQuery.locations(boundingBox); LOGGER.info("Coordinates added to filter query: {}", boundingBox[0][0] + " " + boundingBox[0][1] + " " + boundingBox[1][0] + " " + boundingBox[1][1]); } if (haveKeywordFilter) { filterQuery.track(splitKeywords); LOGGER.info("Keywords added to filter query: {}", Arrays.toString(splitKeywords)); } twitterStream.filter(filterQuery); LOGGER.info("Filter Query created: {}", filterQuery.toString()); } LOGGER.info("Twitter source {} started.", getName()); super.start(); }
From source file:com.tilab.ca.sda.ctw.connector.TwitterReceiver.java
@Override public void onStart() { try {// w ww . ja va 2s . co m log.info(String.format("[%s] CALLED ONSTART on Twitter Receiver...", Constants.TW_RECEIVER_LOG_TAG)); log.debug(String.format("[%s] Init twitterStreamFactory...", Constants.TW_RECEIVER_LOG_TAG)); TwitterStream twStream = new TwitterStreamFactory(twStreamConf).getInstance(); log.debug(String.format("[%s] twitterStreamFactory initialized!", Constants.TW_RECEIVER_LOG_TAG)); log.debug(String.format("[%s] adding status listener..", Constants.TW_RECEIVER_LOG_TAG)); twStream.addListener(new StatusListener() { @Override public void onException(Exception e) { log.error(String.format("[%s] Exception on twitterStream", Constants.TW_RECEIVER_LOG_TAG), e); restart("Error on receiving tweets!", e); } @Override public void onTrackLimitationNotice(int notDelivered) { log.warn(String.format( "[%s] WARNING:number of statuses matching keywords but not delivered => %d", Constants.TW_RECEIVER_LOG_TAG, notDelivered)); } @Override public void onStatus(Status status) { log.debug(String.format("[%s] new status received! storing on spark..", Constants.TW_RECEIVER_LOG_TAG)); store(status); } @Override public void onStallWarning(StallWarning stallW) { log.warn(String.format("[%s] WARNING: Received Stall Warning message: %s", Constants.TW_RECEIVER_LOG_TAG, stallW.getMessage())); } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } }); log.debug(String.format("[%s] Creating filterquery", Constants.TW_RECEIVER_LOG_TAG)); FilterQuery fq = new FilterQuery(); if (trackArray != null) { log.debug(String.format("[%s] added keys to track", Constants.TW_RECEIVER_LOG_TAG)); fq.track(trackArray); } if (locationFilterArray != null) { log.info(String.format("[%s] added geolocations to track", Constants.TW_RECEIVER_LOG_TAG)); fq.locations(locationFilterArray); } if (user2FollowArray != null) { log.debug(String.format("[%s] Added users to track", Constants.TW_RECEIVER_LOG_TAG)); fq.follow(user2FollowArray); } twStream.filter(fq); log.debug(String.format("[%s] Setting twitterStream..", Constants.TW_RECEIVER_LOG_TAG)); setTwitterStream(twStream); log.info(String.format("[%s] Twitter Stream started", Constants.TW_RECEIVER_LOG_TAG)); } catch (Throwable t) { log.error(String.format("[%s] Error starting twitter stream", Constants.TW_RECEIVER_LOG_TAG), t); restart("Error starting twitter stream", t); } }
From source file:com.twasyl.slideshowfx.server.service.TwitterService.java
License:Apache License
private StatusListener buildTwitterStreamListener() { final StatusListener listener = new StatusListener() { @Override// w w w .j a v a 2 s. co m public void onStatus(Status status) { final ChatMessage chatMessage = new ChatMessage(); chatMessage.setId(System.currentTimeMillis() + ""); chatMessage.setSource(ChatMessageSource.TWITTER); chatMessage.setStatus(ChatMessageStatus.NEW); chatMessage.setAuthor("@" + status.getUser().getScreenName()); chatMessage.setContent(status.getText()); final JsonObject jsonTweet = chatMessage.toJSON(); TwitterService.this.vertx.eventBus().publish("slideshowfx.chat.attendee.message.add", jsonTweet); TwitterService.this.vertx.eventBus().publish("slideshowfx.chat.presenter.message.add", jsonTweet); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onTrackLimitationNotice(int i) { } @Override public void onScrubGeo(long l, long l1) { } @Override public void onStallWarning(StallWarning stallWarning) { } @Override public void onException(Exception e) { e.printStackTrace(); } }; return listener; }
From source file:com.twitstreet.twitter.AdsListenerMgrImpl.java
License:Open Source License
@Override public void start() { TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); Announcer announcer = announcerMgr.randomAnnouncerData(); twitterStream.setOAuthConsumer(announcer.getConsumerKey(), announcer.getConsumerSecret()); twitterStream//w w w. j a va 2 s.c o m .setOAuthAccessToken(new AccessToken(announcer.getAccessToken(), announcer.getAccessTokenSecret())); twitterStream.addListener(new StatusListener() { @Override public void onException(Exception arg0) { } @Override public void onTrackLimitationNotice(int arg0) { } @Override public void onStatus(Status status) { HashtagEntity[] hashtagEntities = status.getHashtagEntities(); String screenName = status.getUser().getScreenName(); User user = status.getUser(); if (user != null && (System.currentTimeMillis() - lastMessage > TEN_MIN)) { lastMessage = System.currentTimeMillis(); int action = (int) (ACTION_TYPES * Math.random()); switch (action) { case REGULAR_TWEET: LocalizationUtil lutil = LocalizationUtil.getInstance(); int sentenceSize = Integer .parseInt(lutil.get("announcer.sentence.size", LocalizationUtil.DEFAULT_LANGUAGE)); int random = (int) (Math.random() * sentenceSize); String rndMessage = lutil.get("announcer.sentence." + random, LocalizationUtil.DEFAULT_LANGUAGE); announcerMgr.announceFromRandomAnnouncer(rndMessage); break; case RETWEEET: announcerMgr.retweet(status.getId()); break; case FAVOURITE: announcerMgr.favourite(status.getId()); break; default: String message = constructAdsMessage(screenName, hashtagEntities, status.getUser().getLang()); announcerMgr.reply(message, status.getId()); break; } } } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } }); FilterQuery filterQuery = new FilterQuery(); filterQuery.count(0); filterQuery.track(FILTER_TERMS); twitterStream.filter(filterQuery); }
From source file:com.twitstreet.twitter.FollowBackMgrImpl.java
License:Open Source License
@Override public void start() { Announcer announcer = announcerMgr.randomAnnouncerData(); twitterProxy = twitterProxyFactory.create(announcer.getAccessToken(), announcer.getAccessTokenSecret()); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.setOAuthConsumer(announcer.getConsumerKey(), announcer.getConsumerSecret()); twitterStream/* w w w . j a v a2 s .com*/ .setOAuthAccessToken(new AccessToken(announcer.getAccessToken(), announcer.getAccessTokenSecret())); twitterStream.addListener(new StatusListener() { @Override public void onException(Exception arg0) { } @Override public void onTrackLimitationNotice(int arg0) { } @Override public void onStatus(Status status) { if (System.currentTimeMillis() - lastFollow > FOLLOW_INTERVAL) { twitter4j.User user = status.getUser(); announcerMgr.follow(user.getId()); lastFollow = System.currentTimeMillis(); } // if(System.currentTimeMillis() - lastFollowDiabloBird > FOLLOW_INTERVAL/6){ // twitter4j.User user = status.getUser(); // announcerMgr.followForDiabloBird(user.getId()); // lastFollowDiabloBird = System.currentTimeMillis(); // } } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } }); FilterQuery filterQuery = new FilterQuery(); filterQuery.count(0); filterQuery.track(FILTER_TERMS); twitterStream.filter(filterQuery); }
From source file:com.twitstreet.twitter.Welcome2ListenerMgrImpl.java
License:Open Source License
@Override public void start() { Announcer announcer = announcerMgr.randomAnnouncerData(); twitterProxy = twitterProxyFactory.create(announcer.getAccessToken(), announcer.getAccessTokenSecret()); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.setOAuthConsumer(announcer.getConsumerKey(), announcer.getConsumerSecret()); twitterStream//ww w .j a v a2 s .c om .setOAuthAccessToken(new AccessToken(announcer.getAccessToken(), announcer.getAccessTokenSecret())); twitterStream.addListener(new StatusListener() { @Override public void onException(Exception arg0) { } @Override public void onTrackLimitationNotice(int arg0) { } @Override public void onStatus(Status status) { UserMentionEntity[] userMentionEntities = status.getUserMentionEntities(); for (UserMentionEntity userMentionEntity : userMentionEntities) { idSet.add(userMentionEntity.getId()); if (idSet.size() >= TwitterProxyImpl.IDS_SIZE) { List<User> userList = twitterProxy.getTwUsers(new ArrayList<Long>(idSet)); if (userList != null) { for (User user : userList) { if (user.getFollowersCount() > MIN_FOLLOWER_COUNT_FOR_TREND) { Stock stock = new Stock(user); stockMgr.saveStock(stock); stockMgr.saveTrend(stock.getId()); } } } idSet.clear(); } } } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } }); FilterQuery filterQuery = new FilterQuery(); filterQuery.count(0); filterQuery.track(FILTER_TERMS); twitterStream.filter(filterQuery); }
From source file:com.twitter.graphjet.demo.TwitterStreamReader.java
License:Open Source License
public static void main(String[] argv) throws Exception { final TwitterStreamReaderArgs args = new TwitterStreamReaderArgs(); CmdLineParser parser = new CmdLineParser(args, ParserProperties.defaults().withUsageWidth(90)); try {/*from w w w .j a va2 s.c om*/ parser.parseArgument(argv); } catch (CmdLineException e) { System.err.println(e.getMessage()); parser.printUsage(System.err); return; } final Date demoStart = new Date(); final MultiSegmentPowerLawBipartiteGraph userTweetBigraph = new MultiSegmentPowerLawBipartiteGraph( args.maxSegments, args.maxEdgesPerSegment, args.leftSize, args.leftDegree, args.leftPowerLawExponent, args.rightSize, args.rightDegree, args.rightPowerLawExponent, new IdentityEdgeTypeMask(), new NullStatsReceiver()); final MultiSegmentPowerLawBipartiteGraph tweetHashtagBigraph = new MultiSegmentPowerLawBipartiteGraph( args.maxSegments, args.maxEdgesPerSegment, args.leftSize, args.leftDegree, args.leftPowerLawExponent, args.rightSize, args.rightDegree, args.rightPowerLawExponent, new IdentityEdgeTypeMask(), new NullStatsReceiver()); // Note that we're keeping track of the nodes on the left and right sides externally, apart from the bigraphs, // because the bigraph currently does not provide an API for enumerating over nodes. Currently, this is liable to // running out of memory, but this is fine for the demo. Long2ObjectOpenHashMap<String> users = new Long2ObjectOpenHashMap<>(); LongOpenHashSet tweets = new LongOpenHashSet(); Long2ObjectOpenHashMap<String> hashtags = new Long2ObjectOpenHashMap<>(); // It is accurate of think of these two data structures as holding all users and tweets observed on the stream since // the demo program was started. StatusListener listener = new StatusListener() { long statusCnt = 0; public void onStatus(Status status) { String screenname = status.getUser().getScreenName(); long userId = status.getUser().getId(); long tweetId = status.getId(); long resolvedTweetId = status.isRetweet() ? status.getRetweetedStatus().getId() : status.getId(); HashtagEntity[] hashtagEntities = status.getHashtagEntities(); userTweetBigraph.addEdge(userId, resolvedTweetId, (byte) 0); if (!users.containsKey(userId)) { users.put(userId, screenname); } if (!tweets.contains(tweetId)) { tweets.add(tweetId); } if (!tweets.contains(resolvedTweetId)) { tweets.add(resolvedTweetId); } for (HashtagEntity entity : hashtagEntities) { long hashtagHash = (long) entity.getText().toLowerCase().hashCode(); tweetHashtagBigraph.addEdge(tweetId, hashtagHash, (byte) 0); if (!hashtags.containsKey(hashtagHash)) { hashtags.put(hashtagHash, entity.getText().toLowerCase()); } } statusCnt++; // Note that status updates are currently performed synchronously (i.e., blocking). Best practices dictate that // they should happen on another thread so as to not interfere with ingest, but this is okay for the pruposes // of the demo and the volume of the sample stream. // Minor status update: just print counters. if (statusCnt % args.minorUpdateInterval == 0) { long duration = (new Date().getTime() - demoStart.getTime()) / 1000; System.out.println(String.format( "%tc: %,d statuses, %,d unique tweets, %,d unique hashtags (observed); " + "%.2f edges/s; totalMemory(): %,d bytes, freeMemory(): %,d bytes", new Date(), statusCnt, tweets.size(), hashtags.size(), (float) statusCnt / duration, Runtime.getRuntime().totalMemory(), Runtime.getRuntime().freeMemory())); } // Major status update: iterate over right and left nodes. if (statusCnt % args.majorUpdateInterval == 0) { int leftCnt = 0; LongIterator leftIter = tweets.iterator(); while (leftIter.hasNext()) { if (userTweetBigraph.getLeftNodeDegree(leftIter.nextLong()) != 0) leftCnt++; } int rightCnt = 0; LongIterator rightIter = hashtags.keySet().iterator(); while (rightIter.hasNext()) { if (userTweetBigraph.getRightNodeDegree(rightIter.nextLong()) != 0) rightCnt++; } System.out.println(String.format("%tc: Current user-tweet graph state: %,d left nodes (users), " + "%,d right nodes (tweets)", new Date(), leftCnt, rightCnt)); } } public void onScrubGeo(long userId, long upToStatusId) { } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onStallWarning(StallWarning warning) { } public void onException(Exception e) { e.printStackTrace(); } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); twitterStream.sample(); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); Server jettyServer = new Server(args.port); jettyServer.setHandler(context); context.addServlet(new ServletHolder(new TopUsersServlet(userTweetBigraph, users)), "/userTweetGraph/topUsers"); context.addServlet( new ServletHolder( new TopTweetsServlet(userTweetBigraph, tweets, TopTweetsServlet.GraphType.USER_TWEET)), "/userTweetGraph/topTweets"); context.addServlet(new ServletHolder( new TopTweetsServlet(tweetHashtagBigraph, tweets, TopTweetsServlet.GraphType.TWEET_HASHTAG)), "/tweetHashtagGraph/topTweets"); context.addServlet(new ServletHolder(new TopHashtagsServlet(tweetHashtagBigraph, hashtags)), "/tweetHashtagGraph/topHashtags"); context.addServlet(new ServletHolder(new GetEdgesServlet(userTweetBigraph, GetEdgesServlet.Side.LEFT)), "/userTweetGraphEdges/users"); context.addServlet(new ServletHolder(new GetEdgesServlet(userTweetBigraph, GetEdgesServlet.Side.RIGHT)), "/userTweetGraphEdges/tweets"); context.addServlet(new ServletHolder(new GetEdgesServlet(tweetHashtagBigraph, GetEdgesServlet.Side.LEFT)), "/tweetHashtagGraphEdges/tweets"); context.addServlet(new ServletHolder(new GetEdgesServlet(tweetHashtagBigraph, GetEdgesServlet.Side.RIGHT)), "/tweetHashtagGraphEdges/hashtags"); context.addServlet(new ServletHolder(new GetSimilarHashtagsServlet(tweetHashtagBigraph, hashtags)), "/similarHashtags"); System.out.println(String.format("%tc: Starting service on port %d", new Date(), args.port)); try { jettyServer.start(); jettyServer.join(); } finally { jettyServer.destroy(); } }
From source file:com.twitter.storm.hashtag2.TwitterSampleSpout.java
License:Apache License
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 . co m*/ StatusListener listener = new StatusListener() { public void onStatus(Status status) { queue.offer(status); } public void onDeletionNotice(StatusDeletionNotice sdn) { } public void onTrackLimitationNotice(int i) { } public void onScrubGeo(long l, long l1) { } public void onException(Exception ex) { } public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret); _twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); _twitterStream.addListener(listener); if (keyWords.length == 0) { _twitterStream.sample(); } else { FilterQuery query = new FilterQuery().track(keyWords); _twitterStream.filter(query); } }
From source file:com.utad.flume.source.TwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets.//from w ww . ja va 2 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); List<Event> events = Arrays.asList(event); channel.processEventBatch(events); } // 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), twitterStream.addListener(listener); // 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.yahoo.labs.samoa.sentinel.model.TwitterStreamAPIReader.java
License:Apache License
private void setUpStreamListener() { listener = new StatusListener() { public void onStatus(Status status) { add(status);/* www .j a va 2 s.c o m*/ } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onException(Exception ex) { ex.printStackTrace(); } @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); } }; twitterStream.addListener(listener); }