List of usage examples for twitter4j FilterQuery FilterQuery
public FilterQuery()
From source file:cloudcomputebot.CloudComputeBot.java
License:Open Source License
public static void main(String[] args) { TwitterLib.init();/*from w w w . j a v a2 s . c o m*/ t = TwitterLib.t; TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(t.getConfiguration()); TwitterStream twitterStream = twitterStreamFactory.getInstance(); FilterQuery filterQuery = new FilterQuery(); filterQuery.follow(new long[] { 4741197613L }); twitterStream.addListener(new MentionListener()); twitterStream.filter(filterQuery); }
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;//from w w w. ja v a 2s .c om } 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:ColourUs.Stream.java
public Stream(Configuration config, Trending trending) { this.trending = trending; TwitterStreamFactory tsf = new TwitterStreamFactory(config); twitterStream = tsf.getInstance();//from w ww . ja v a 2 s.c o m twitterStream.addListener(this); filter = new FilterQuery(); filter.track(new String[] { "I", "you", "he", "she", "them", "they", "us", "our", "her", "him", "they're", "I'm", "my", "your", "is", "was", "are", "were" }); }
From source file:com.aremaitch.codestock2010.library.TwitterLib.java
License:Apache License
public void startMonitoringStream(String[] hashTags, long[] userIds, StatusListener statusListener, ConnectionLifeCycleListener connectionLifeCycleListener) throws TwitterException { if (statusListener != null) tStream.addListener(statusListener); if (connectionLifeCycleListener != null) tStream.addConnectionLifeCycleListener(connectionLifeCycleListener); FilterQuery fq = new FilterQuery(); fq.track(hashTags);/*www . j a v a 2s.co m*/ if (userIds != null) fq.follow(userIds); tStream.filter(fq); }
From source file:com.babatunde.twittergoogle.Utility.java
public void postToGoogle(PlusDomains s, String id, String hashtag) { try {/*from w ww .j a v a 2s. 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.cloudera.flume.source.TwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets.//www . j a v a 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); 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.esri.geoevent.transport.twitter.TwitterInboundTransport.java
License:Apache License
private void receiveData() { try {//from ww w .ja v a2 s . c o m 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.github.jcustenborder.kafka.connect.twitter.TwitterSourceTask.java
License:Apache License
@Override public void start(Map<String, String> map) { this.config = new TwitterSourceConnectorConfig(map); TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(this.config.configuration()); this.twitterStream = twitterStreamFactory.getInstance(); String[] keywords = this.config.filterKeywords(); if (log.isInfoEnabled()) { log.info("Setting up filters. Keywords = {}", Joiner.on(", ").join(keywords)); }/*from ww w .ja va 2s . c o m*/ FilterQuery filterQuery = new FilterQuery(); filterQuery.track(keywords); if (log.isInfoEnabled()) { log.info("Starting the twitter stream."); } twitterStream.addListener(this); twitterStream.filter(filterQuery); }
From source file:com.github.jiloc.USTweetsAnalyzer.Streamer.java
public void startListening() { // sample() method internally creates a thread which manipulates // TwitterStream // and calls these adequate listener methods continuously. this.twitterStream.addListener(listener); System.out.println("Opening stream. Start listening.."); //this.twitterStream.sample(); // 1 % of the public stream FilterQuery fq = new FilterQuery();// the filter object fq.locations(LOCATIONS);/*from www .ja v a2 s . c o m*/ // fq.language(language); // fq.track(query); //the query to track from the stream this.twitterStream.filter(fq); }
From source file:com.hortonworks.amuise.cdrstorm.kafka.producers.CDRTestDataProducer.java
private void start() { /**/*from w w w . j av a 2s . c o m*/ * 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(); }