Example usage for twitter4j TwitterStreamFactory TwitterStreamFactory

List of usage examples for twitter4j TwitterStreamFactory TwitterStreamFactory

Introduction

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

Prototype

public TwitterStreamFactory(String configTreePath) 

Source Link

Document

Creates a TwitterStreamFactory with a specified config tree.

Usage

From source file:public_streaming.UserIDStream.java

License:Apache License

public static void main(String[] args) throws Exception {
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();

    TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance();
    twStream.addListener(new MyStatusListener());
    ////from w w w  .  j a  v  a 2s.  c o m
    long[] follow = { 1598997848 };//anondroid3?id:1598997848
    FilterQuery filter = new FilterQuery(follow);//?userID?????
    twStream.filter(filter);
}

From source file:SentimentAnalyses.PrintSampleStream.java

License:Apache License

public static void main(String[] args) throws TwitterException {

    final PrintSampleStream pr = new PrintSampleStream();

    try {/*from   w  w  w.j av  a2 s.c om*/
        pr.LinkMongodb();
    } catch (Exception e) {
        e.printStackTrace();
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Em3WTI7jc90HcvKzPkTLQ")
            .setOAuthConsumerSecret("vg4p6rOF32bmffqRR8m0jAUClrxvtGiMB5PrSr3Zsw")
            .setOAuthAccessToken("1681973072-1q0zI0VPjHD3ttNuaBOL94frzCI9sXInxAcDK0w")
            .setOAuthAccessTokenSecret("ZRLkOyjmhHBkU1iNyEVNyIgIBsKrl0DUDKOcOMneYFYEM");
    cb.setJSONStoreEnabled(true);

    TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
    TwitterStream twitterStream = tf.getInstance();
    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            //System.out.println(status);
            String str = DataObjectFactory.getRawJSON(status);
            try {
                //JSONObject nnstr = new JSONObject(newstr);
                DBObject dbObject = (DBObject) JSON.parse(str);
                //                    System.out.println(dbObject);
                pr.collection.insert(dbObject);
                //System.out.println(dbObject);
                pr.count++;
                if (pr.count % 1000 == 0)
                    System.out.println(pr.count);
                if (pr.count > 100000) {
                    pr.mongo.close();
                    System.exit(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @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();
        }
    };
    twitterStream.addListener(listener);

    String[] trackArray;
    String[] Track = { "Malaysia Airlines", "Flight MH370", "Boeing-777", "Kuala Lumpur", "Bei jing" };
    //trackArray[0] = "Obama";
    //trackArray[1] = "Romney";

    FilterQuery filter = new FilterQuery();
    filter.track(Track);
    String[] lang = { "en" };
    filter.language(lang);
    twitterStream.filter(filter);
    //pr.mongo.close();
}

From source file:site_streaming.SiteStream.java

License:Apache License

public static void main(String[] args) throws Exception {
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET)
            .setSiteStreamBaseURL("https://sitestream.twitter.com/1.1/site.json").build();

    TwitterStream SiteStream = new TwitterStreamFactory(configuration).getInstance();
    SiteStream.addListener(new MyStatusListener());

    long[] follow = { 1598997848 };//anondroid3?id:1598997848
    FilterQuery filter = new FilterQuery(follow);//?userID?????
    SiteStream.filter(filter);/*from   w w w .  ja  va  2 s .co  m*/
}

From source file:socialImport.twitter.TwitterImportFactory.java

License:Open Source License

/**
 * Initializes the TwitterImport-Module with the given parameters
 * /*from  w  w  w.jav a 2s  . c  o  m*/
 * @param oAuthConsumerKey The oAuthConsumerKey as generated by Twitter.
 * @param oAuthConsumerSecret The oAuthConsumerSecret as generated by Twitter.
 * @param oAuthAccessToken The oAuthAccessToken as generated by Twitter.
 * @param oAuthAccessTokenSecret The oAuthAccessTokenSecret as generated by Twitter.
 * @param debugEnabled Whether the Twitter4J-Library should print debugMessages to the console or not.
 * @param dataModule A reference to the systems Data-Module.
 * 
 * */
public ITwitterImport getInstance(String oAuthConsumerKey, String oAuthConsumerSecret, String oAuthAccessToken,
        String oAuthAccessTokenSecret, boolean debugEnabled, IData dataModule) {

    ConfigurationBuilder configBuilder = new ConfigurationBuilder();
    configBuilder.setDebugEnabled(debugEnabled).setOAuthConsumerKey(oAuthConsumerKey)
            .setOAuthConsumerSecret(oAuthConsumerSecret).setOAuthAccessToken(oAuthAccessToken)
            .setOAuthAccessTokenSecret(oAuthAccessTokenSecret).setJSONStoreEnabled(true);

    if (debugEnabled) {
        System.setProperty("twitter4j.loggerFactory", "twitter4j.internal.logging.StdOutLoggerFactory");
    } else {
        System.setProperty("twitter4j.loggerFactory", "twitter4j.internal.logging.NullLoggerFactory");
    }

    TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(configBuilder.build());

    return new TwitterImport(twitterStreamFactory, dataModule);
}

From source file:soporte.GetTweets.java

public GetTweets() {
    ConfigTwitterStream configuracion = new ConfigTwitterStream();
    this.twitterStream = new TwitterStreamFactory(configuracion.getCb().build()).getInstance();
}

From source file:source.TwitterSource.java

License:Apache License

/**
 * The initialization method for the Source. The context contains all the
 * Flume configuration info, and can be used to retrieve any configuration
 * values necessary to set up the Source.
 *
 * @param context Key-value store used to pass configuration information
 * throughout the system.//from  w  ww  .j  av a 2 s  .  c  o m
 */
@Override
public void configure(Context context) {

    consumerKey = context.getString(TwitterSourceConstants.CONSUMER_KEY);
    consumerSecret = context.getString(TwitterSourceConstants.CONSUMER_SECRET);
    accessToken = context.getString(TwitterSourceConstants.ACCESS_TOKEN);
    accessTokenSecret = context.getString(TwitterSourceConstants.ACCESS_TOKEN_SECRET);

    String swString = context.getString(TwitterSourceConstants.SW_LNG_LAT);
    String neString = context.getString(TwitterSourceConstants.NE_LNG_LAT);
    if (swString != null && neString != null) {
        String[] sw = swString.split(",");
        String[] ne = neString.split(",");
        if (sw.length == 2 && ne.length == 2) {
            for (int i = 0; i < 2; i++) {
                locations[0][i] = Double.parseDouble(sw[i].trim());
                locations[1][i] = Double.parseDouble(ne[i].trim());
            }
        } else {
            locations = null;
        }
    } else {
        locations = null;
    }

    String keywordString = context.getString(TwitterSourceConstants.KEYWORDS);
    if (keywordString != null) {
        keywords = keywordString.split(",");
        for (int i = 0; i < keywords.length; i++) {
            keywords[i] = keywords[i].trim();
        }
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(consumerKey);
    cb.setOAuthConsumerSecret(consumerSecret);
    cb.setOAuthAccessToken(accessToken);
    cb.setOAuthAccessTokenSecret(accessTokenSecret);
    cb.setJSONStoreEnabled(true);
    cb.setIncludeEntitiesEnabled(true);
    cb.setIncludeRTsEnabled(true);

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

}

From source file:storm.starter.spout.Q2FetchTweetSpout.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  ava 2 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);

    twitterStream.sample();
}

From source file:storm.starter.spout.Q2SeqTwitterSpout.java

License:Apache License

@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    queue = new LinkedBlockingQueue<Status>(1000);
    _collector = collector;//  w w w .j a va  2 s. com

    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);

    FilterQuery query = new FilterQuery();
    query.track(keyWords);
    query.language(new String[] { "en" });
    twitterStream.filter(query);
}

From source file:storm.starter.spout.TwitterKeywordsSpout.java

License:Apache License

@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    queue = new LinkedBlockingQueue<Status>(1000);
    _collector = collector;/*w w  w  .  jav a  2 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);
    _twitterStream = twitterStream;

    if (keyWords.length == 0) {

        twitterStream.sample();
    }

    else {
        // TODO: Adjust the query below to also track locations and languages.
        FilterQuery query = new FilterQuery();
        query.track(keyWords);
        query.language(new String[] { "en" });

        twitterStream.filter(query);

    }

}

From source file:storm.starter.spout.TwitterNoKeywordSpout.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 v a 2  s .co  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);

    twitterStream.sample();

    FilterQuery query = new FilterQuery();
    //query.count(1000000);
    query.language(new String[] { "en" });
    twitterStream.filter(query);
}