List of usage examples for twitter4j StatusListener StatusListener
StatusListener
From source file:be.ugent.tiwi.sleroux.newsrec.twittertest.StreamReaderService.java
public void readTwitterFeed() { TwitterStream stream = TwitterStreamBuilderUtil.getStream(); StatusListener listener = new StatusListener() { @Override/*w ww. j a v a 2 s . c o m*/ public void onException(Exception e) { } @Override public void onTrackLimitationNotice(int n) { } @Override public void onStatus(Status status) { if (status.getLang().equals("en")) { System.out.println(status.getText()); } } @Override public void onStallWarning(StallWarning arg0) { } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } }; stream.addListener(listener); FilterQuery f = new FilterQuery(); f.language(new String[] { "en" }); f.follow(new long[] { 816653 }); stream.filter(f); try { Thread.sleep(60000); } catch (InterruptedException ex) { Logger.getLogger(StreamReaderService.class.getName()).log(Level.SEVERE, null, ex); } stream.shutdown(); }
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 . ja va 2 s . c o m*/ 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: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);/*from www. j a va 2s . c o m*/ 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:co.cask.cdap.template.etl.realtime.source.TwitterSource.java
License:Apache License
@Override public void initialize(RealtimeContext context) throws Exception { super.initialize(context); // Disable chatty logging from twitter4j. System.setProperty("twitter4j.loggerFactory", "twitter4j.NullLoggerFactory"); Schema.Field idField = Schema.Field.of(ID, Schema.of(Schema.Type.LONG)); Schema.Field msgField = Schema.Field.of(MSG, Schema.of(Schema.Type.STRING)); Schema.Field langField = Schema.Field.of(LANG, Schema.nullableOf(Schema.of(Schema.Type.STRING))); Schema.Field timeField = Schema.Field.of(TIME, Schema.nullableOf(Schema.of(Schema.Type.LONG))); Schema.Field favCount = Schema.Field.of(FAVC, Schema.of(Schema.Type.INT)); Schema.Field rtCount = Schema.Field.of(RTC, Schema.of(Schema.Type.INT)); Schema.Field sourceField = Schema.Field.of(SRC, Schema.nullableOf(Schema.of(Schema.Type.STRING))); Schema.Field geoLatField = Schema.Field.of(GLAT, Schema.nullableOf(Schema.of(Schema.Type.DOUBLE))); Schema.Field geoLongField = Schema.Field.of(GLNG, Schema.nullableOf(Schema.of(Schema.Type.DOUBLE))); Schema.Field reTweetField = Schema.Field.of(ISRT, Schema.of(Schema.Type.BOOLEAN)); schema = Schema.recordOf("tweet", idField, msgField, langField, timeField, favCount, rtCount, sourceField, geoLatField, geoLongField, reTweetField); statusListener = new StatusListener() { @Override//from w w w . jav a2 s. c om public void onStatus(Status status) { tweetQ.add(status); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // No-op } @Override public void onTrackLimitationNotice(int i) { // No-op } @Override public void onScrubGeo(long l, long l1) { // No-op } @Override public void onStallWarning(StallWarning stallWarning) { // No-op } @Override public void onException(Exception e) { // No-op } }; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setDebugEnabled(false).setOAuthConsumerKey(twitterConfig.consumerKey) .setOAuthConsumerSecret(twitterConfig.consumeSecret).setOAuthAccessToken(twitterConfig.accessToken) .setOAuthAccessTokenSecret(twitterConfig.accessTokenSecret); twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(); twitterStream.addListener(statusListener); twitterStream.sample(); }
From source file:co.cask.hydrator.plugin.realtime.source.TwitterSource.java
License:Apache License
@Override public void initialize(RealtimeContext context) throws Exception { super.initialize(context); // Disable chatty logging from twitter4j. System.setProperty("twitter4j.loggerFactory", "twitter4j.NullLoggerFactory"); statusListener = new StatusListener() { @Override/*from w w w.ja va 2s . c o m*/ public void onStatus(Status status) { tweetQ.add(status); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // No-op } @Override public void onTrackLimitationNotice(int i) { // No-op } @Override public void onScrubGeo(long l, long l1) { // No-op } @Override public void onStallWarning(StallWarning stallWarning) { // No-op } @Override public void onException(Exception e) { // No-op } }; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setDebugEnabled(false).setOAuthConsumerKey(twitterConfig.consumerKey) .setOAuthConsumerSecret(twitterConfig.consumeSecret).setOAuthAccessToken(twitterConfig.accessToken) .setOAuthAccessTokenSecret(twitterConfig.accessTokenSecret); twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(); twitterStream.addListener(statusListener); twitterStream.sample(); }
From source file:CollectionOfMentions.ControllerCollectionOfMentions.java
License:Open Source License
public void run() { startDateTime = new DateTime(); //checks on dates to make sure it's not abobe 7 days if (numberOfMinutes < 0) { numberOfMinutes = 0;/* w ww . j ava 2 s .co m*/ } if (numberOfMinutes > 59) { numberOfMinutes = 59; } if (numberOfHours > 24) { numberOfHours = 24; } if (numberOfHours < 0) { numberOfHours = 0; } if (numberOfDays > 7) { numberOfDays = 7; } if (numberOfDays < 0) { numberOfDays = 0; } stopTime = startDateTime.getMillis() + numberOfMinutes * 60000 + numberOfHours * 3600000 + numberOfDays * 3600000 * 24; if (stopTime - startDateTime.getMillis() > 3600000 * 24 * 7) { stopTime = startDateTime.getMillis() + 3600000 * 24 * 7; } //registers actual start time in the status field opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("status", String.valueOf(startDateTime.getMillis())); dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED); //registers actual end time in the end field opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("end", String.valueOf(stopTime)); dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED); final Object lock = new Object(); StatusListener listener; listener = new StatusListener() { @Override public void onStatus(Status status) { nbTweets++; if (System.currentTimeMillis() > stopTime) { //updating the job a last time; //************************************** //saving statuses to the db. // if (!twitterStatuses.isEmpty()) { // opsJob = dsJobs.createUpdateOperations(Job.class).addAll("statuses", statusesIds, true); // dsJobs.update(updateQueryJob, opsJob); // // dsTweets.save(twitterStatuses); // } // 91 is the code for twitter stream has stopped collecting. progress = 91; //recording the progress, nbTweets and end time of the job opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("progress", progress) .set("nbTweets", nbTweets).set("end", System.currentTimeMillis()); dsJobsInfo.update(updateQueryJobInfo, opsJobInfo); synchronized (lock) { lock.notify(); } } else { tweet = new Tweet(); tweet.setStatus(TwitterObjectFactory.getRawJSON(status)); tweet.setIdTweet(nbTweets); tweet.setJobId(jobUUID); //// System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); // twitterStatus = convertStatus.convertOneToTwitterStatus(status); // twitterStatus.setJobId(jobUUID); // twitterStatuses.add(twitterStatus); // // // // statusesIds.add(status.getId()); // timeSinceLastStatus = System.currentTimeMillis() - timeLastStatus; // // //************************************** // //adjusting the frequency of saves to DB, function of number of statuses received per second // if (timeSinceLastStatus < 200) { // sizeBatch = 100; // } else { // sizeBatch = 25; // } // timeLastStatus = System.currentTimeMillis(); // progressLong = (Long) ((System.currentTimeMillis() - startDateTime.getMillis()) * 98 / (stopTime - startDateTime.getMillis())); // if (statusesIds.size() > sizeBatch || progressLong.intValue() > progress) { //************************************** //saving statuses to the db. try { dsTweets.save(tweet, WriteConcern.UNACKNOWLEDGED); opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("nbTweets", nbTweets); dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED); } catch (MongoException m) { System.out.println("saving of statuses to the db failed"); } // twitterStatuses = new ArrayList(); // // //************************************** // //updating list of status ids of the job. // opsJob = dsJobs.createUpdateOperations(Job.class).addAll("statuses", statusesIds, true); // dsJobs.update(updateQueryJob, opsJob); // statusesIds = new ArrayList(); // // //updating progress. // System.out.println("progress: " + progressLong); // progress = progressLong.intValue(); // opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("progress", progress).set("nbTweets", nbTweets); // dsJobsInfo.update(updateQueryJobInfo, opsJobInfo); //************************************** // } } } @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 onException(Exception ex) { System.out.println("Exception: " + ex); } @Override public void onStallWarning(StallWarning sw) { System.out.println("Got stall warning:" + sw.getMessage()); } }; twitterStream.addListener(listener); FilterQuery fq = new FilterQuery(); String[] mentions = mention.split(","); fq.track(mentions); // twitterStream.filter(new FilterQuery(0, users, keywords)); twitterStream.filter(fq); try { synchronized (lock) { lock.wait(); } } catch (InterruptedException e) { } try { twitterStream.shutdown(); } catch (Exception e) { System.out.println("exception when shutdown of twitter stream"); System.out.println("error: " + e.getMessage()); } System.out.println("shutdown of twitter stream was successful"); }
From source file:com.babatunde.twittergoogle.Utility.java
public void postToGoogle(PlusDomains s, String id, String hashtag) { try {/*w w w. ja va2 s . c o m*/ final PlusDomains serve = s; final String circleID = id; listener = new StatusListener() { @Override public void onStatus(Status status) { String msg = status.getUser().getName() + " - " + "@" + status.getUser().getScreenName() + " - " + status.getText(); System.out.println(msg); //Create a list of ACL entries if (serve != null && (!circleID.isEmpty() || (circleID != null))) { PlusDomainsAclentryResource resource = new PlusDomainsAclentryResource(); resource.setType("domain").setType("circle").setId(circleID); //Get Emails of people in the circle. List<PlusDomainsAclentryResource> aclEntries = new ArrayList<PlusDomainsAclentryResource>(); aclEntries.add(resource); Acl acl = new Acl(); acl.setItems(aclEntries); acl.setDomainRestricted(true); // Required, this does the domain restriction // Create a new activity object Activity activity = new Activity() .setObject(new Activity.PlusDomainsObject().setOriginalContent(msg)).setAccess(acl); try { // Execute the API request, which calls `activities.insert` for the logged in user activity = serve.activities().insert("me", activity).execute(); } catch (IOException ex) { Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex); } } } @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(); } }; } catch (Exception e) { } TwitterStream twitterStream = new TwitterStreamFactory(configuration).getInstance(); twitterStream.addListener(listener); String str[] = { hashtag }; twitterStream.shutdown(); try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex); } twitterStream.filter(new FilterQuery().track(str)); }
From source file:com.cask.twitter.TweetStream.java
License:Apache License
@Override public void initialize(FlowletContext context) { statusListener = new StatusListener() { @Override// w w w . j ava2s. c o m public void onStatus(Status status) { for (HashtagEntity hash : status.getHashtagEntities()) { tweetQ.add(hash.getText()); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onTrackLimitationNotice(int i) { } @Override public void onScrubGeo(long l, long l2) { } @Override public void onStallWarning(StallWarning stallWarning) { } @Override public void onException(Exception e) { LOG.error(e.getMessage()); } }; cb = new ConfigurationBuilder(); cb.setDebugEnabled(false) //Provide Twitter Credentials through Runtime Arguments. .setOAuthConsumerKey(context.getRuntimeArguments().get("ConsumerKey")) .setOAuthConsumerSecret(context.getRuntimeArguments().get("ConsumerSecret")) .setOAuthAccessToken(context.getRuntimeArguments().get("AccessToken")) .setOAuthAccessTokenSecret(context.getRuntimeArguments().get("AccessTokenSecret")); tStream = new TwitterStreamFactory(cb.build()).getInstance(); //For testing, we can disable Twitter Stream and use testStream to send sample hashTags. if (!context.getRuntimeArguments().containsKey("disableLiveStream")) { tStream.addListener(statusListener); tStream.sample(); } }
From source file:com.cloudera.flume.source.TwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets.//from www .j a va 2s .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 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), 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.ebay.pulsar.twittersample.channel.TwitterSampleChannel.java
License:GNU General Public License
@Override public void open() throws EventException { super.open(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(false);/*from ww w .ja v a2s.co m*/ twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long userId, long upToStatusId) { } @Override public void onStallWarning(StallWarning warning) { } @Override public void onStatus(Status status) { HashtagEntity[] hashtagEntities = status.getHashtagEntities(); JetstreamEvent event = new JetstreamEvent(); event.setEventType("TwitterSample"); Place place = status.getPlace(); if (place != null) { event.put("country", place.getCountry()); } event.put("ct", status.getCreatedAt().getTime()); event.put("text", status.getText()); event.put("lang", status.getLang()); event.put("user", status.getUser().getName()); if (hashtagEntities != null && hashtagEntities.length > 0) { StringBuilder s = new StringBuilder(); s.append(hashtagEntities[0].getText()); for (int i = 1; i < hashtagEntities.length; i++) { s.append(","); s.append(hashtagEntities[i].getText()); } event.put("hashtag", s.toString()); } fireSendEvent(event); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } }; twitterStream.addListener(listener); twitterStream.sample(); }