List of usage examples for twitter4j TwitterStream filter
TwitterStream filter(final String... track);
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 a va 2s . 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. ja va 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.producer.TwitterProducer.java
License:Apache License
public static void main(String[] args) { if (args.length == 0) { System.out.println(/*from w w w.j av a 2 s. com*/ "SimpleCounter {broker-list} {topic} {type old/new} {type sync/async} {delay (ms)} {count}"); return; } /* get arguments */ String brokerList = args[0]; String topic = args[1]; String age = args[2]; String sync = args[3]; /* * In order to create the spout, you need to get twitter credentials * If you need to use Twitter firehose/Tweet stream for your idea, * create a set of credentials by following the instructions at * * https://dev.twitter.com/discussions/631 * */ String custkey = "WXDgVgeJMwHEn0Z9VHDx5j93h"; String custsecret = "DgP9CsaPtG87urpNU14fZySXOjNX4j4v2PqmeTndcjjYBgLldy"; String accesstoken = "3243813491-ixCQ3HWWeMsthKQvj5MiBvNw3dSNAuAd3IfoDUw"; String accesssecret = "aHOXUB4nbhZv2vbAeV15ZyTAD0lPPCptCr32N0PX7OaMe"; producer = new DemoProducerOld(topic); /* start a producer */ producer.configure(brokerList, sync); producer.start(); //long startTime = System.currentTimeMillis(); System.out.println("Starting..."); producer.produce("Starting..."); ConfigurationBuilder config = new ConfigurationBuilder().setOAuthConsumerKey(custkey) .setOAuthConsumerSecret(custsecret).setOAuthAccessToken(accesstoken) .setOAuthAccessTokenSecret(accesssecret); // create the twitter stream factory with the config TwitterStream twitterStream; TwitterStreamFactory fact = new TwitterStreamFactory(config.build()); // get an instance of twitter stream twitterStream = fact.getInstance(); // message to kafka Map<String, String> headers = new HashMap<String, String>(); //filter non-english tweets FilterQuery tweetFilterQuery = new FilterQuery(); tweetFilterQuery.language(new String[] { "en" }); // tweetFilterQuery.locations(new double[][] { { -180, -90 }, { 180, 90 } }); // provide the handler for twitter stream twitterStream.addListener(new TweetListener()); twitterStream.filter(tweetFilterQuery); // start the sampling of tweets twitterStream.sample(); // TODO ADD timestamp //long endTime = System.currentTimeMillis(); //System.out.println("... and we are done. This took " + (endTime - startTime) + " ms."); //producer.produce("... and we are done. This took " + (endTime - startTime) + " ms."); /* close shop and leave */ //producer.close(); //System.exit(0); }
From source file:com.storm.demo.TwitterSampleSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<String>(1000); _collector = collector;/* ww w .j av a 2 s . com*/ // StatusListener listener = new StatusListener() { // // @Override // public void onStatus(Status status) { // System.out.println(DataObjectFactory.getRawJSON(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 // // } // // }; RawStreamListener rawListener = new RawStreamListener() { @Override public void onMessage(String rawJSON) { //System.out.println(rawJSON); queue.offer(rawJSON); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); //twitterStream.addListener(listener); twitterStream.addListener(rawListener); 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.tilab.ca.sda.ctw.connector.TwitterReceiver.java
@Override public void onStart() { try {// w ww .ja va 2s . c om 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.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/* 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) { 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 . ja v a 2 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) { 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/*from w w w .j a va2 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) { 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.tokyo.kucho.daemon.Daemon.java
License:Apache License
public static void main(String[] args) { TwitterStream stream = TwitterStreamFactory.getSingleton(); SeatingList seatingList = new SeatingList(); stream.addListener(new KuchoController(EHillsImpl.getInstance(), seatingList)); String[] trackTags = new String[COLD.length + HOT.length]; System.arraycopy(COLD, 0, trackTags, 0, COLD.length); System.arraycopy(HOT, 0, trackTags, COLD.length, HOT.length); FilterQuery query = new FilterQuery().track(trackTags); logger.info("Starting."); stream.filter(query); logger.info("Started."); while (true) { try {//w w w .j a va 2s. c o m Thread.sleep(1000000); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }
From source file:com.waves_rsp.ikb4stream.datasource.twitter.TwitterProducerConnector.java
License:Open Source License
/** * Listen tweets from twitter with a bounding box and load them with the data producer object * * @param dataProducer {@link IDataProducer} contains the data queue * @see TwitterProducerConnector#confBuilder * @see TwitterProducerConnector#boundingBox *//* ww w . j a va 2s. c om*/ @Override public void load(IDataProducer dataProducer) { Objects.requireNonNull(dataProducer); TwitterStream twitterStream = null; try { TwitterStreamListener streamListener = new TwitterStreamListener(dataProducer); twitterStream = new TwitterStreamFactory(confBuilder.build()).getInstance(); FilterQuery filterQuery = new FilterQuery(); filterQuery.locations(boundingBox); twitterStream.addListener(streamListener); twitterStream.filter(filterQuery); Thread.currentThread().join(); } catch (IllegalArgumentException | IllegalStateException err) { LOGGER.error("Error loading : " + err.getMessage()); throw new IllegalStateException(err.getMessage()); } catch (InterruptedException e) { LOGGER.info("Close twitter"); Thread.currentThread().interrupt(); } finally { if (twitterStream != null) { twitterStream.cleanUp(); twitterStream.shutdown(); } Thread.currentThread().interrupt(); } }