Example usage for twitter4j FilterQuery FilterQuery

List of usage examples for twitter4j FilterQuery FilterQuery

Introduction

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

Prototype

public FilterQuery(int count, long[] follow, String[] track) 

Source Link

Document

Creates a new FilterQuery

Usage

From source file:gov.nasa.jpl.memex.elwha.impl.ElwhaResource.java

License:Apache License

/**
 * Main entry of this application./* w  ww .  j a  va  2  s  . c  o m*/
 *
 * @param userIds follow(comma separated user ids) track(comma separated filter terms)
 * @throws TwitterException when Twitter service or network is unavailable
 * @return
 */
@Override
public Elwha get(String userIds) {
    //return new Elwha().setMessage("Hello, Rest.li!");
    // }

    //if (userIds.length < 1) {
    //  System.out.println("Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]");
    //  System.exit(-1);
    //}

    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }

        @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 twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    ArrayList<Long> follow = new ArrayList<Long>();
    ArrayList<String> track = new ArrayList<String>();
    if (isNumericalArgument(userIds)) {
        for (String id : userIds.split(",")) {
            follow.add(Long.parseLong(id));
        }
    } else {
        track.addAll(Arrays.asList(userIds.split(",")));
    }
    long[] followArray = new long[follow.size()];
    for (int i = 0; i < follow.size(); i++) {
        followArray[i] = follow.get(i);
    }
    String[] trackArray = track.toArray(new String[track.size()]);

    // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.filter(new FilterQuery(0, followArray, trackArray));

    return new Elwha().setUserIds("Mock stub!");
}

From source file:org.jwebsocket.plugins.twitter.TwitterPlugIn.java

License:Apache License

private void mUpdateStream(Token aToken) {
    try {//from w ww  . j  a va 2  s.c  o  m
        String[] lKeywordArray = new String[mKeywords.size()];
        int lIdx = 0;
        for (String lKeyword : mKeywords.keySet()) {
            lKeywordArray[lIdx] = lKeyword;
            lIdx++;
            if (lIdx >= MAX_STREAM_KEYWORDS_TOTAL) {
                break;
            }
        }
        if (lIdx > mStatsMaxKeywords) {
            mStatsMaxKeywords = lIdx;
        }

        FilterQuery lFilter = new FilterQuery(0, new long[] {}, lKeywordArray);
        // if no TwitterStream object created up to now, create one...
        if (mTwitterStream == null) {
            mTwitterStream = new TwitterStreamFactory().getInstance();
            mTwitterStream.addListener(mTwitterStreamListener);
            mTwitterStream.setOAuthConsumer(mSettings.getConsumerKey(), mSettings.getConsumerSecret());
            AccessToken lAccessToken = new AccessToken(mSettings.getAccessKey(), mSettings.getAccessSecret());
            mTwitterStream.setOAuthAccessToken(lAccessToken);
        }
        // apply the filter to the stream object
        mTwitterStream.filter(lFilter);
    } catch (Exception lEx) {
        String lMsg = lEx.getClass().getSimpleName() + ": " + lEx.getMessage();
        mLog.error(lMsg);
        aToken.setInteger("code", -1);
        aToken.setString("msg", lMsg);
    }
}

From source file:org.mule.twitter.TwitterConnector.java

License:Open Source License

/**
 * Asynchronously retrieves public statuses that match one or more filter predicates.
 * <p/>//ww w  .  ja  v a  2 s .c  o  m
 * At least a keyword or userId must be specified. Multiple parameters may be
 * specified.
 * <p/>
 * Placing long parameters in the URL may cause the request to be rejected for excessive URL length.
 * <p/>
 * The default access level allows up to 200 track keywords and 400 follow userids.
 * <p/>
 * Only one Twitter stream can be consumed using the same credentials. As a consequence,
 * only one twitter stream can be consumed per connector instance.
 * <p/>
 * {@sample.xml ../../../doc/twitter-connector.xml.sample twitter:filteredStream}
 *
 * @param count    the number of previous statuses to stream before transitioning to the live stream.
 * @param userIds  the user ids to follow
 * @param keywords the keywords to track
 * @param callback the {@link SourceCallback} used to dispatch messages when a response is received
 */
@Source
public void filteredStream(@Optional @Default("0") int count,
        @Placement(group = "User Ids to Follow") @Optional List<String> userIds,
        @Placement(group = "Keywords to Track") @Optional List<String> keywords,
        final SourceCallback callback) {
    listenToStatues(callback).filter(new FilterQuery(count, toLongArray(userIds), toStringArray(keywords)));
}

From source file:org.socialsketch.tool.rubbish.twitterstream.PrintFilterStream.java

License:Apache License

/**
 * Main entry of this application.// ww w.j  av a  2 s  .  co  m
 *
 * @param args follow(comma separated user ids) track(comma separated filter terms)
 * @throws twitter4j.TwitterException
 */
public static void main(String[] args) throws TwitterException {
    //        if (args.length < 1) {
    //            System.out.println("Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]");
    //            System.exit(-1);
    //        }

    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }

        @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 twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);

    ArrayList<Long> follow = new ArrayList<Long>();
    ArrayList<String> track = new ArrayList<String>();

    //        for (String arg : args) {
    //            if (isNumericalArgument(arg)) {
    //                for (String id : arg.split(",")) {
    //                    follow.add(Long.parseLong(id));
    //                }
    //            } else {
    //                track.addAll(Arrays.asList(arg.split(",")));
    //            }
    //        }
    track.add("void setup draw");
    track.add("void size");

    long[] followArray = new long[follow.size()];
    for (int i = 0; i < follow.size(); i++) {
        followArray[i] = follow.get(i);
    }

    String[] trackArray = track.toArray(new String[track.size()]);

    // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.filter(new FilterQuery(0, followArray, trackArray));
}

From source file:twitter4j.examples.stream.PrintFilterStream.java

License:Apache License

/**
 * Main entry of this application.// w  ww  .j a  v  a2 s  .  c  o  m
 *
 * @param args follow(comma separated user ids) track(comma separated filter terms)
 * @throws TwitterException when Twitter service or network is unavailable
 */
public static void main(String[] args) throws TwitterException {
    if (args.length < 1) {
        System.out.println(
                "Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]");
        System.exit(-1);
    }

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance().addListener(new StatusListener() {
        @Override
        public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }

        @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();
        }
    });

    ArrayList<Long> follow = new ArrayList<Long>();
    ArrayList<String> track = new ArrayList<String>();
    for (String arg : args) {
        if (isNumericalArgument(arg)) {
            for (String id : arg.split(",")) {
                follow.add(Long.parseLong(id));
            }
        } else {
            track.addAll(Arrays.asList(arg.split(",")));
        }
    }
    long[] followArray = new long[follow.size()];
    for (int i = 0; i < follow.size(); i++) {
        followArray[i] = follow.get(i);
    }
    String[] trackArray = track.toArray(new String[track.size()]);

    // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.filter(new FilterQuery(0, followArray, trackArray));
}