Example usage for twitter4j FilterQuery locations

List of usage examples for twitter4j FilterQuery locations

Introduction

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

Prototype

null locations

To view the source code for twitter4j FilterQuery locations.

Click Source Link

Usage

From source file:edu.uci.ics.asterix.external.util.TwitterUtil.java

License:Apache License

public static FilterQuery getFilterQuery(Map<String, String> configuration) throws AsterixException {
    String locationValue = configuration.get(ConfigurationConstants.KEY_LOCATION);
    double[][] locations = null;
    if (locationValue != null) {
        if (locationValue.contains(",")) {
            String[] coordinatesString = locationValue.trim().split(",");
            locations = new double[2][2];
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    try {
                        locations[i][j] = Double.parseDouble(coordinatesString[2 * i + j]);
                    } catch (NumberFormatException ne) {
                        throw new AsterixException(
                                "Incorrect coordinate value " + coordinatesString[2 * i + j]);
                    }/*from   w w w  . j a  v  a  2  s  . com*/
                }
            }
        } else {
            locations = GeoConstants.boundingBoxes.get(locationValue);
        }
        if (locations != null) {
            FilterQuery filterQuery = new FilterQuery();
            filterQuery.locations(locations);
            return filterQuery;
        }
    }
    return null;

}

From source file:kafka.producer.PrintGeoStream.java

License:Apache License

/**
 * Main entry of this application.//from w w w  .j  a v a  2s .c  o  m
 *
 * @param args <kafka host> <kafka topic>
 * @throws twitter4j.TwitterException
 */
public static void main(String[] args) throws TwitterException {

    //        if (args.length != 2) {
    //            System.out.println("Usage: java twitter4j.examples.PrintFilterStream  <kafka host>  <kafka topic>");
    //            System.exit(-1);
    //        }

    //        final String kafkaZKHost = args[0];
    //        final String kafkaTopic = args[1];

    final String kafkaZKHost = "localhost";
    final String kafkaTopic = "flightDelay";

    BasicConfigurator.configure();

    StatusListener listener = new StatusListener() {

        Gson gson = new Gson();

        Properties kafkaProps = new Properties();
        //kafkaProps.put("serializer.class", "kafka.serializer.StringEncoder");
        //kafkaProps.put("zk.connect", "localhost:2181");
        Producer<Integer, String> kafkaProducer = null;
        int tweetCount = 0;
        int interimCount = 0;

        @Override
        public void onStatus(Status status) {
            if (kafkaProducer == null) {
                kafkaProps.put("serializer.class", "kafka.serializer.StringEncoder");
                //kafkaProps.put("zk.connect", "localhost:2181");
                kafkaProps.put("zk.connect", kafkaZKHost.concat(":2181"));
                kafkaProducer = new Producer<Integer, String>(new ProducerConfig(kafkaProps));

            }
            tweetCount++;
            interimCount++;
            if (interimCount == 100) {
                LOG.info(" total tweets received: " + tweetCount);
                interimCount = 0;
            }

            //System.out.println("@" + status.getUser().getScreenName() + " - "); // + status.getText());
            //if ( status.getPlace() != null ) {
            //    System.out.println("coordinates = " + status.getPlace().getGeometryCoordinates());
            //}

            TweetInfo tweetInfo = new TweetInfo();
            tweetInfo.populate(status);
            String message = gson.toJson(tweetInfo, TweetInfo.class);

            kafkaProducer.send(new ProducerData<Integer, String>(kafkaTopic, message));
            //kafkaProducer.send(new ProducerData<Integer, String>("live_tweets", message));
        }

        @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();
        }
    };
    /*
     * - use Redis to pass filter messages in - or ZK?
     * - Read in filter JSON with parms for:
     *     - lat/lon boxj
     *     - screen names to follow
     *     - hashtags to track
     * - Convert screen names to userIds
     *     - Twitter twitter = new TwitterFactory().getInstance();
     *       ResponseList<User> users = twitter.lookupUsers(args[0].split(","));
     * - Kick off filter with new FilterQuery
     *     - periodically check, and if filter updated, restart the TwitterStream:
     *         .shutdown()
     *         new
     *         .addListener
     * 
     * 
     */

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    //        twitterStream.setOAuthAccessToken(new AccessToken("14098069-IgutahzmbaCubdoMU0MxsfsJT2iNVpRnoohCbV67k", "d1kFKqRSnrlA4bP1XWWLiHNsHfYDZQPWDHRVpGP4hM"));
    //        twitterStream.setOAuthConsumer("G0SKTmMXjfMsunBh3Furg", "iVTYNLqVaVdUZz4xOn7H8ywylTaTuW3kH1dT21NUy4");
    twitterStream.addListener(listener);
    /*
     * For CONUS only: 
     *  "lowerLat": "22.0",
     *  "lowerLon": "-127.0",
     *  "upperLat": "47.5",
     *  "upperLon": "-67.0"
     */
    FilterQuery fQuery = new FilterQuery();
    String[] topics = new String[2];
    topics[0] = "delayed flight";
    topics[1] = "flight delay";

    //        double[][] us = {{-180, 24}, {-66, 50}};
    double[][] lax = { { -118.5, 33.8 }, { -118.3, 34.01 } };
    fQuery.locations(lax);
    fQuery.track(topics);
    twitterStream.filter(fQuery);

}

From source file:org.apache.asterix.external.util.TwitterUtil.java

License:Apache License

public static FilterQuery getFilterQuery(Map<String, String> configuration) throws AsterixException {
    String locationValue = null;//  w w  w .j av a 2s .  co m

    // For backward compatibility
    if (configuration.containsKey(ConfigurationConstants.KEY_LOCATIONS)) {
        locationValue = configuration.get(ConfigurationConstants.KEY_LOCATIONS);
    } else {
        locationValue = configuration.get(ConfigurationConstants.KEY_LOCATION);
    }
    String langValue = configuration.get(ConfigurationConstants.LANGUAGES);
    String trackValue = configuration.get(ConfigurationConstants.TRACK);

    FilterQuery filterQuery = null;

    // Terms to track
    if (trackValue != null) {
        String keywords[] = null;
        filterQuery = new FilterQuery();
        if (trackValue.contains(",")) {
            keywords = trackValue.trim().split(",\\s*");
        } else {
            keywords = new String[] { trackValue };
        }
        filterQuery = filterQuery.track(keywords);
    }

    // Language filtering parameter
    if (langValue != null) {
        if (filterQuery == null) {
            filterQuery = new FilterQuery();
        }
        String languages[];
        if (langValue.contains(",")) {
            languages = langValue.trim().split(",\\s*");
        } else {
            languages = new String[] { langValue };
        }
        filterQuery = filterQuery.language(languages);
    }

    // Location filtering parameter
    if (locationValue != null) {
        double[][] locations = getBoundingBoxes(locationValue);
        if (locations != null) {
            if (filterQuery == null) {
                filterQuery = new FilterQuery();
            }
            filterQuery = filterQuery.locations(locations);
        }
    }

    // Filtering level: none, low or medium (defaul=none)
    if (filterQuery != null) {
        String filterValue = configuration.get(ConfigurationConstants.FILTER_LEVEL);
        if (filterValue != null) {
            filterQuery = filterQuery.filterLevel(filterValue);
        }
    }

    return filterQuery;

}

From source file:org.apache.camel.component.twitter.consumer.streaming.FilterConsumer.java

License:Apache License

private FilterQuery createFilter(TwitterEndpoint te) {
    FilterQuery filterQuery = new FilterQuery();
    String allLocationsString = te.getProperties().getLocations();
    if (allLocationsString != null) {
        String[] locationStrings = allLocationsString.split(";");
        double[][] locations = new double[locationStrings.length][2];
        for (int i = 0; i < locationStrings.length; i++) {
            String[] coords = locationStrings[i].split(",");
            locations[i][0] = Double.valueOf(coords[0]);
            locations[i][1] = Double.valueOf(coords[1]);
        }//from  w  w  w  .  ja v  a  2s .  co  m
        filterQuery.locations(locations);
    }

    String keywords = te.getProperties().getKeywords();
    if (keywords != null && keywords.length() > 0) {
        filterQuery.track(keywords.split(","));
    }

    String userIds = te.getProperties().getUserIds();
    if (userIds != null) {
        String[] stringUserIds = userIds.split(",");
        long[] longUserIds = new long[stringUserIds.length];
        for (int i = 0; i < stringUserIds.length; i++) {
            longUserIds[i] = Long.valueOf(stringUserIds[i]);
        }
        filterQuery.follow(longUserIds);
    }

    if (allLocationsString == null && keywords == null && userIds == null) {
        throw new IllegalArgumentException("At least one filter parameter is required");
    }

    return filterQuery;
}

From source file:org.threeriverdev.twitterreporter.TwitterBot.java

License:Apache License

public static void main(String... args) throws Exception {
    FilterQuery query = new FilterQuery();
    List<double[]> locations = new ArrayList<double[]>();

    // Create a grid over the continental US.
    // TODO: Originally (2009ish), these were recommended by Twitter.  Not sure if it's relevant now or not --
    // could we achieve the same results with 1 big block?
    for (double swLat = 25.0; swLat <= 49.0; swLat = swLat + GRIDSIZE) {
        for (double swLon = -125.0; swLon <= -67.0; swLon = swLon + GRIDSIZE) {
            double neLat = swLat + GRIDSIZE;
            double neLon = swLon + GRIDSIZE;

            double[] swLocation = new double[2];
            swLocation[0] = swLon;//from   ww w .j a v a  2 s . com
            swLocation[1] = swLat;
            locations.add(swLocation);

            double[] neLocation = new double[2];
            neLocation[0] = neLon;
            neLocation[1] = neLat;
            locations.add(neLocation);
        }
    }

    query.locations(locations.toArray(new double[0][0]));

    try {
        URL url = TwitterBot.class.getResource("/oauth.properties");
        Properties p = new Properties();
        InputStream inStream = url.openStream();
        p.load(inStream);

        ConfigurationBuilder confBuilder = new ConfigurationBuilder();
        confBuilder.setOAuthConsumerKey(p.getProperty("consumer.key"));
        confBuilder.setOAuthConsumerSecret(p.getProperty("consumer.secret"));
        confBuilder.setOAuthAccessToken(p.getProperty("access.token"));
        confBuilder.setOAuthAccessTokenSecret(p.getProperty("access.token.secret"));
        Configuration conf = confBuilder.build();

        TwitterStream twitterStream = new TwitterStreamFactory(conf).getInstance();
        twitterStream.addListener(new TweetProcessor());
        twitterStream.filter(query);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

/**
 * Setting up a connection with Twitter Stream API with the given
 * credentials//from ww  w  .  j  a  v  a2s .  co  m
 *
 * @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).setJSONStoreEnabled(true).setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecret);
    StatusListener statusStreamsListener;
    UserStreamListener userStreamListener;
    SiteStreamsListener siteStreamslistener;
    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)
            || twitterOperation.equals(TwitterConstant.RETWEET_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 (locations != null) {
            query.locations(locations);
        }
        if (filterLevel != null) {
            query.filterLevel(filterLevel);
        }
        if (follow == null & tracks == null & locations == null) {
            handleException("At least follow, locations, or track must be specified.");
        }
        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");
            }
        } else {
            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);
        } else {
            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: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());

    // //from  w  ww .  ja v  a  2  s.  c  om
    FilterQuery filter = new FilterQuery();
    double[][] locations = { { -180.0d, -90.0d }, { 180.0d, 90.0d } };//??(???????)
    filter.locations(locations);
    twStream.filter(filter);
}

From source file:source.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets.//  ww w  . j  av a  2s .c o m
 */
@Override
public void start() {
    // The channel is the piece of Flume that sits between the Source and
    // Sink, and is used to process events
    final ChannelProcessor channel = getChannelProcessor();

    final Map<String, String> headers = new HashMap<String, String>();

    // The StatusListener is a twitter4j API, which can be added to a 
    // Twitter stream, and will execute methods every time a message comes 
    // in through the stream
    StatusListener listener = new StatusListener() {

        // The onStatus method is executed every time a new tweet comes in
        public void onStatus(Status status) {
            // The EventBuilder is used to build an event using the headers 
            // and the raw JSON of a tweet
            logger.debug(status.getUser().getScreenName() + ": " + status.getText());

            headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime()));
            Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers);

            channel.processEvent(event);
        }

        // This listener will ignore everything except for new tweets
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onScrubGeo(long userId, long upToStatusId) {
        }

        public void onException(Exception ex) {
        }

        public void onStallWarning(StallWarning warning) {
        }

    };

    logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}",
            new String[] { consumerKey, accessToken });
    // Set up the stream's listener (defined above), and set any necessary 
    // security information
    twitterStream.addListener(listener);

    // Set up a filter to pull out industry-relevant tweets
    logger.debug("Starting up Twitter filtering...");
    FilterQuery query = new FilterQuery().count(0);

    if (locations == null) {
        logger.debug("No locations specified");
    } else {
        String debugString = "Locations specified: ";
        debugString += "SW={" + locations[0][0] + ", " + locations[0][1] + "}, ";
        debugString += "NE={" + locations[1][0] + ", " + locations[1][1] + "}";
        logger.debug(debugString);
        query.locations(locations);
    }

    if (keywords == null) {
        logger.debug("No keywords specified");
    } else {
        String debugString = keywords.length + " keywords specified: ";
        for (int i = 0; i < keywords.length; i++) {
            debugString += keywords[i];
            if (i != keywords.length - 1) {
                debugString += ", ";
            }
        }
        logger.debug(debugString);
        query.track(keywords);
    }

    twitterStream.filter(query);

    super.start();

}

From source file:trendulo.ingest.twitter.TwitterStreamingStringSequenceSource.java

License:Apache License

public TwitterStreamingStringSequenceSource(String username, String password, String archiveTwitterFilePath,
        StatusFilter statusFilter) {//from   www.java 2s. c  o m

    this.statusFilter = statusFilter;
    statusQueue = new ArrayBlockingQueue<Status>(DEFAULT_BUFFER_SIZE);

    // If the user wants status messages archived, create an OutputStream
    if (archiveTwitterFilePath != null) {
        try {
            // Verify that the file doesn't already exist so we don't clobber the archive
            if (new File(archiveTwitterFilePath).exists()) {
                throw new RuntimeException(
                        "Archive File Already Exists. Please move/rename before starting Ingest: "
                                + archiveTwitterFilePath);
            }

            FileOutputStream fos = new FileOutputStream(archiveTwitterFilePath);
            if (archiveTwitterFilePath.endsWith("gz")) {
                archiveOutputStream = new GZIPOutputStream(new BufferedOutputStream(fos));
            } else {
                archiveOutputStream = fos;
            }
        } catch (IOException e) {
            log.error("Error creating archive file", e);
        }
    }

    // Configure the Twitter Streaming API client

    // this system property is required to access raw json
    System.setProperty("twitter4j.jsonStoreEnabled", "true");

    auth = new BasicAuthorization(username, password);
    twitterStream = new TwitterStreamFactory().getInstance(auth);
    twitterStream.addListener(this);

    // We only want status messages with a geo. This needs to be pulled
    // out so it is configurable
    // Right now just the lower 48 of the US
    FilterQuery filterQuery = new FilterQuery();
    double[][] locations = { { -126.0, 24.0 }, { -67.0, 49.0 } };
    filterQuery.locations(locations);
    twitterStream.filter(filterQuery);
}

From source file:twitter_app_p1.Twitter_app_p1.java

public static void GetTweets() {

    twitterStream.addListener(listener);

    FilterQuery tweet_filter = new FilterQuery();
    double[][] location = { { -124, 24 }, { -66, 49 } }; //continental US
    tweet_filter.locations(location);

    twitterStream.filter(tweet_filter);//from   w w w . ja va  2s.c om
}