Example usage for twitter4j TwitterStream filter

List of usage examples for twitter4j TwitterStream filter

Introduction

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

Prototype

TwitterStream filter(final String... track);

Source Link

Document

Start consuming public statuses that match the filter predicate.

Usage

From source file:org.todoke.countstream.Main.java

License:Apache License

public static void main(String[] args) {
    if (args.length != 1) {
        System.out.println("usage: java org.todoke.hashcount.Main [comma separated terms to track]");
        System.exit(-1);//from   w ww . ja  v  a  2 s.  c o m
    }
    logger.info("terms to track: " + args[0]);
    String[] terms = args[0].split(",");
    FilterQuery query = new FilterQuery().track(terms);
    TwitterStream stream = new TwitterStreamFactory().getInstance();
    StringBuffer path = new StringBuffer(args[0].length());
    for (String term : terms) {
        if (0 != path.length()) {
            path.append("-");
        }
        path.append(term.replaceAll("#", ""));
    }
    Callback callback = new Callback(terms);
    Counter counter = new Counter(callback);

    stream.addListener(counter);
    stream.filter(query);
}

From source file:org.wso2.carbon.inbound.custom.poll.TwitterStreamData.java

License:Open Source License

/**
 * Setting up a connection with Twitter Stream API with the given
 * credentials/*from   w w  w .j ava 2  s  .  com*/
 *
 * @throws TwitterException
 */
private void setupConnection() throws TwitterException {
    if (log.isDebugEnabled()) {
        log.debug("Starting to setup the connection with the twitter streaming endpoint");
    }
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecret);
    StatusListener statusStreamsListener;
    UserStreamListener userStreamListener;
    SiteStreamsListener siteStreamslistener;
    TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
    String twitterOperation = properties.getProperty(TwitterConstant.TWITTER_OPERATION);
    if (twitterOperation.equals(TwitterConstant.FILTER_STREAM_OPERATION)
            || twitterOperation.equals(TwitterConstant.FIREHOSE_STREAM_OPERATION)
            || twitterOperation.equals(TwitterConstant.LINK_STREAM_OPERATION)
            || twitterOperation.equals(TwitterConstant.SAMPLE_STREAM_OPERATION)) {
        statusStreamsListener = new StatusListenerImpl();
        twitterStream.addListener(statusStreamsListener);
    } else if (twitterOperation.equals(TwitterConstant.USER_STREAM_OPERATION)) {
        userStreamListener = new UserStreamListenerImpl();
        twitterStream.addListener(userStreamListener);
    } else if (twitterOperation.equals(TwitterConstant.SITE_STREAM_OPERATION)) {
        siteStreamslistener = new siteStreamsListenerImpl();
        twitterStream.addListener(siteStreamslistener);
    } else {
        handleException("The operation :" + twitterOperation + " not found");
    }

    /* Synchronously retrieves public statuses that match one or more filter predicates.*/
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.FILTER_STREAM_OPERATION)) {
        FilterQuery query = new FilterQuery();
        if (languages != null) {
            query.language(languages);
        }
        if (tracks != null) {
            query.track(tracks);
        }
        if (follow != null) {
            query.follow(follow);
        }
        if (filterLevel != null) {
            query.filterLevel(filterLevel);
        }
        query.count(count);
        twitterStream.filter(query);
    }

    /* Returns a small random sample of all public statuses. */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.SAMPLE_STREAM_OPERATION)) {

        if (languages != null) {
            if (languages.length == 1) {
                twitterStream.sample(languages[1]);
            } else {
                handleException("A language can be used for the sample operation");
            }
        }
        twitterStream.sample();
    }
    /* Asynchronously retrieves all public statuses.*/
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.FIREHOSE_STREAM_OPERATION)) {
        if (countParam != null) {
            twitterStream.firehose(count);
        }
    }
    /*
     User Streams provide a stream of data and events specific to the
     authenticated user.This provides to access the Streams messages for a
     single user.
     */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.USER_STREAM_OPERATION)) {
        if (tracks != null) {
            twitterStream.user(tracks);
        }
        twitterStream.user();
    }
    /*
     * Link Streams provide asynchronously retrieves all statuses containing 'http:' and 'https:'.
     */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.LINK_STREAM_OPERATION)) {
        if (countParam != null) {
            twitterStream.links(count);
        }
    }
    /*
     * User Streams provide a stream of data and events specific to the
     * authenticated user.This provides to access the Streams messages for a
     * single user.
     */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.RETWEET_STREAM_OPERATION)) {
        twitterStream.retweet();
    }
    /*
     * Site Streams allows services, such as web sites or mobile push
     * services, to receive real-time updates for a large number of users.
     * Events may be streamed for any user who has granted OAuth access to
     * your application. Desktop applications or applications with few users
     * should use user streams.
     */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.SITE_STREAM_OPERATION)) {
        twitterStream.site(withFollowings, follow);
    }

}

From source file:org.xmlsh.twitter.stream.java

License:BSD License

@Override
public int run(List<XValue> args) throws Exception {

    Options opts = new Options(sCOMMON_OPTS + ",p=port:,track:,sample,json,sanitize",
            SerializeOpts.getOptionDefs());
    opts.parse(args);//ww w  .  ja  va  2s.co  m
    mSerializeOpts = this.getSerializeOpts(opts);
    final boolean bJson = opts.hasOpt("json");
    final boolean bSanitize = opts.hasOpt("sanitize");

    args = opts.getRemainingArgs();

    final OutputPort port = mShell.getEnv().getOutputPort(opts.getOptStringRequired("port"), true);

    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {
            try {
                if (bJson) {
                    String json = DataObjectFactory.getRawJSON(status);

                    PrintWriter writer = port.asPrintWriter(mSerializeOpts);
                    writer.println(json);
                    writer.close();

                } else {
                    TwitterWriter mWriter = new TwitterWriter(port.asXMLStreamWriter(mSerializeOpts),
                            bSanitize);
                    mWriter.startDocument();
                    mWriter.startElement(TwitterWriter.kTWITTER_NS, "twitter");
                    mWriter.writeDefaultNamespace();
                    mWriter.write("status", status);
                    mWriter.endElement();
                    mWriter.endDocument();
                    mWriter.closeWriter();
                    port.writeSequenceTerminator(mSerializeOpts);
                }

            } catch (Exception e) {
                onException(e);
            }

        }

        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 arg0) {
            // TODO Auto-generated method stub

        }

    };

    try {

        TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
        twitterStream.addListener(listener);

        // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.

        // FilterQuery filter = new FilterQuery().track(Util.toStringArray(args));

        // twitterStream.filter(filter);
        if (opts.hasOpt("sample"))
            twitterStream.sample();
        else
            twitterStream.filter(new FilterQuery().track(opts.getOptStringRequired("track").split(",")));

    } finally {

    }
    return 0;

}

From source file:public_streaming.GeoStream.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());

    // // w w  w  .  j a va 2 s  . com
    FilterQuery filter = new FilterQuery();
    double[][] locations = { { -180.0d, -90.0d }, { 180.0d, 90.0d } };//??(???????)
    filter.locations(locations);
    twStream.filter(filter);
}

From source file:public_streaming.HashtagStream.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());

    //set filter//from   w ww  . j av  a  2  s .c om
    FilterQuery filter = new FilterQuery();
    String[] track = { "#nhk" };//hashtag
    filter.track(track);
    twStream.filter(filter);

}

From source file:public_streaming.KeywordStream.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  a2  s.c o m
    FilterQuery filter = new FilterQuery();
    String[] keywords = { "android", "iphone" };//?public_timeline?
    filter.track(keywords);
    twStream.filter(filter);
}

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());
    ////  w w  w  . ja va 2 s  .  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  ww  w  . j  a va  2  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 source file:socialImport.twitter.TwitterImport.java

License:Open Source License

@Override
public void openFilterStream(StreamDescriptor streamDescriptor, int count, long[] follow,
        double[][] locations) {
    //If the stream ID is not in use yet
    if (!streams.keySet().contains(streamDescriptor)) {
        TwitterStream twitterStream = twitterStreamFactory.getInstance();

        twitterStream.addListener(new TweetToDatabaseImportListener(streamDescriptor, dataModule));
        FilterQuery filterQuery = new FilterQuery(count, follow, streamDescriptor.getTrackedTags(), locations);
        streams.put(streamDescriptor, twitterStream);
        twitterStream.filter(filterQuery);
    } else/*w  w w .ja  v  a2 s .  co  m*/
        throw new IllegalArgumentException(DUPLICATE_ID_ERROR_MESSAGE);
}