Example usage for twitter4j TwitterStream setOAuthConsumer

List of usage examples for twitter4j TwitterStream setOAuthConsumer

Introduction

In this page you can find the example usage for twitter4j TwitterStream setOAuthConsumer.

Prototype

void setOAuthConsumer(String consumerKey, String consumerSecret);

Source Link

Document

sets the OAuth consumer key and consumer secret

Usage

From source file:at.illecker.sentistorm.spout.TwitterStreamSpout.java

License:Apache License

@Override
public void open(Map config, TopologyContext context, SpoutOutputCollector collector) {
    m_collector = collector;/*from   w w  w  .  j a  v a  2 s  .  c  o m*/
    m_tweetsQueue = new LinkedBlockingQueue<Status>(1000);

    // Optional startup sleep to finish bolt preparation
    // before spout starts emitting
    if (config.get(CONF_STARTUP_SLEEP_MS) != null) {
        long startupSleepMillis = (Long) config.get(CONF_STARTUP_SLEEP_MS);
        TimeUtils.sleepMillis(startupSleepMillis);
    }

    TwitterStream twitterStream = new TwitterStreamFactory(
            new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance();

    // Set Listener
    twitterStream.addListener(new StatusListener() {
        @Override
        public void onStatus(Status status) {
            m_tweetsQueue.offer(status); // add tweet into queue
        }

        @Override
        public void onException(Exception arg0) {
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
        }
    });

    // Set credentials
    twitterStream.setOAuthConsumer(m_consumerKey, m_consumerSecret);
    AccessToken token = new AccessToken(m_accessToken, m_accessTokenSecret);
    twitterStream.setOAuthAccessToken(token);

    // Filter twitter stream
    FilterQuery tweetFilterQuery = new FilterQuery();
    if (m_keyWords != null) {
        tweetFilterQuery.track(m_keyWords);
    }

    // Filter location
    // https://dev.twitter.com/docs/streaming-apis/parameters#locations
    tweetFilterQuery.locations(new double[][] { new double[] { -180, -90, }, new double[] { 180, 90 } }); // any geotagged tweet

    // Filter language
    tweetFilterQuery.language(new String[] { m_filterLanguage });

    twitterStream.filter(tweetFilterQuery);
}

From source file:at.storm.spout.TwitterStreamSpout.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override/* w w w . j a va  2 s .  co  m*/
public void open(Map config, TopologyContext context, SpoutOutputCollector collector) {
    m_collector = collector;
    m_tweetsQueue = new LinkedBlockingQueue<Status>(1000);

    // Optional startup sleep to finish bolt preparation
    // before spout starts emitting
    if (config.get(CONF_STARTUP_SLEEP_MS) != null) {
        long startupSleepMillis = (Long) config.get(CONF_STARTUP_SLEEP_MS);
        TimeUtils.sleepMillis(startupSleepMillis);
    }

    TwitterStream twitterStream = new TwitterStreamFactory(
            new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance();

    // Set Listener
    twitterStream.addListener(new StatusListener() {
        @Override
        public void onStatus(Status status) {
            m_tweetsQueue.offer(status); // add tweet into queue
        }

        @Override
        public void onException(Exception arg0) {
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
        }
    });

    // Set credentials
    twitterStream.setOAuthConsumer(m_consumerKey, m_consumerSecret);
    AccessToken token = new AccessToken(m_accessToken, m_accessTokenSecret);
    twitterStream.setOAuthAccessToken(token);

    // Filter twitter stream
    FilterQuery tweetFilterQuery = new FilterQuery();
    if (m_keyWords != null) {
        tweetFilterQuery.track(m_keyWords);
    }

    // Filter location
    // https://dev.twitter.com/docs/streaming-apis/parameters#locations
    tweetFilterQuery.locations(new double[][] { new double[] { -180, -90, }, new double[] { 180, 90 } }); // any
    // geotagged
    // tweet

    // Filter language
    tweetFilterQuery.language(new String[] { m_filterLanguage });

    twitterStream.filter(tweetFilterQuery);
}

From source file:com.accumulobook.designs.graph.TwitterGraph.java

License:Apache License

public static void ingest(final String[] args, final BatchWriter writer, int seconds) throws Exception {

    String token = args[0];//  w w w. j  av  a2  s. co  m
    String tokenSecret = args[1];
    String consumerKey = args[2];
    String consumerSecret = args[3];

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(new TGStatusListener(writer));

    AccessToken accessToken = new AccessToken(token, tokenSecret);

    twitterStream.setOAuthConsumer(consumerKey, consumerSecret);
    twitterStream.setOAuthAccessToken(accessToken);

    System.out.println("processing twitter stream ...");
    twitterStream.sample();

    Thread.sleep(seconds * 1000);

    System.out.println("shutting down twitter stream.");
    twitterStream.shutdown();

    writer.flush();
}

From source file:com.cafeform.iumfs.twitterfs.TwitterFactoryAdapter.java

License:Apache License

public static void setUserStreamListener(String username, UserStreamListener listener) {
    // Invokde timeline retrieving thread. 
    // Stream API (only home)
    // Thread created in TwitterStream.sample()
    TwitterStream twitterStream = streamFactory.getInstance();
    twitterStream.setOAuthConsumer(Prefs.get("OAuthConsumerKey"), Prefs.get("consumerSecret"));
    twitterStream.setOAuthAccessToken(TwitterFactoryAdapter.getAccessToken(username));
    twitterStream.addListener(listener);
    twitterStream.user();//from  w  ww.  java2s  .co  m
}

From source file:com.freshdigitable.udonroad.module.TwitterApiModule.java

License:Apache License

@Singleton
@Provides//from  w ww .  ja  v a  2 s.  c om
public TwitterStream provideTwitterStream() {
    final TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory();
    final TwitterStream twitterStream = twitterStreamFactory.getInstance();
    final String key = context.getString(R.string.consumer_key);
    final String secret = context.getString(R.string.consumer_secret);
    twitterStream.setOAuthConsumer(key, secret);
    return twitterStream;
}

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  ww w  .  j a v a2  s  .  c o m*/

    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.storm.demo.TwitterSampleSpout.java

License:Apache License

@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    queue = new LinkedBlockingQueue<String>(1000);
    _collector = collector;//from w ww  .  j  ava 2s  .  c o m

    //        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.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/*from w  w  w .ja v a 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//from   w  w  w  .  ja  v  a 2s  .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/*from w  w w .java2  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);
}