Search in sources :

Example 1 with FilterQuery

use of twitter4j.FilterQuery in project camel by apache.

the class FilterStreamingConsumer method createFilter.

private FilterQuery createFilter() {
    FilterQuery filterQuery = new FilterQuery();
    String allLocationsString = endpoint.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]);
        }
        filterQuery.locations(locations);
    }
    String keywords = endpoint.getProperties().getKeywords();
    if (keywords != null && keywords.length() > 0) {
        filterQuery.track(keywords.split(","));
    }
    String userIds = endpoint.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;
}
Also used : FilterQuery(twitter4j.FilterQuery) TwitterEndpoint(org.apache.camel.component.twitter.TwitterEndpoint)

Example 2 with FilterQuery

use of twitter4j.FilterQuery in project twitter4j by yusuke.

the class TwitterStreamLambda method oldTraditionalDullBoringImplementation.

public static void oldTraditionalDullBoringImplementation(String... dummy) {
    // Twitter4J 4.0.3 or earlier
    TwitterStream stream = TwitterStreamFactory.getSingleton();
    stream.addListener(new StatusAdapter() {

        @Override
        public void onStatus(Status status) {
            String.format("@%s %s", status.getUser().getScreenName(), status.getText());
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    });
    stream.filter(new FilterQuery(new String[] { "twitter4j", "#twitter4j" }));
}
Also used : Status(twitter4j.Status) StatusAdapter(twitter4j.StatusAdapter) TwitterStream(twitter4j.TwitterStream) FilterQuery(twitter4j.FilterQuery)

Example 3 with FilterQuery

use of twitter4j.FilterQuery in project ud381 by udacity.

the class TweetSpout method open.

@Override
public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
    // create the buffer to block tweets
    queue = new LinkedBlockingQueue<String>(1000);
    SentimentAnalyzer.init();
    // save the output collector for emitting tuples
    collector = spoutOutputCollector;
    // build the config with credentials for twitter 4j
    ConfigurationBuilder config = new ConfigurationBuilder().setOAuthConsumerKey(custkey).setOAuthConsumerSecret(custsecret).setOAuthAccessToken(accesstoken).setOAuthAccessTokenSecret(accesssecret);
    // create the twitter stream factory with the config
    TwitterStreamFactory fact = new TwitterStreamFactory(config.build());
    // get an instance of twitter stream
    twitterStream = fact.getInstance();
    // See 
    FilterQuery tweetFilterQuery = new FilterQuery();
    tweetFilterQuery.locations(new double[][] { new double[] { -124.848974, 24.396308 }, new double[] { -66.885444, 49.384358 } });
    tweetFilterQuery.language(new String[] { "en" });
    // provide the handler for twitter stream
    twitterStream.addListener(new TweetListener());
    twitterStream.filter(tweetFilterQuery);
    // start the sampling of tweets
    twitterStream.sample();
}
Also used : ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) TwitterStreamFactory(twitter4j.TwitterStreamFactory) FilterQuery(twitter4j.FilterQuery)

Example 4 with FilterQuery

use of twitter4j.FilterQuery in project asterixdb by apache.

the class TwitterRecordReaderFactory method createRecordReader.

@Override
public IRecordReader<? extends String> createRecordReader(IHyracksTaskContext ctx, int partition) throws HyracksDataException {
    IRecordReader<? extends String> recordReader;
    switch(configuration.get(ExternalDataConstants.KEY_READER)) {
        case ExternalDataConstants.READER_PULL_TWITTER:
            recordReader = new TwitterPullRecordReader(TwitterUtil.getTwitterService(configuration), configuration.get(SearchAPIConstants.QUERY), Integer.parseInt(configuration.get(SearchAPIConstants.INTERVAL)));
            break;
        case ExternalDataConstants.READER_PUSH_TWITTER:
            FilterQuery query;
            try {
                query = TwitterUtil.getFilterQuery(configuration);
                recordReader = (query == null) ? new TwitterPushRecordReader(TwitterUtil.getTwitterStream(configuration), TwitterUtil.getTweetListener()) : new TwitterPushRecordReader(TwitterUtil.getTwitterStream(configuration), TwitterUtil.getTweetListener(), query);
            } catch (AsterixException e) {
                throw new HyracksDataException(e);
            }
            break;
        case ExternalDataConstants.READER_USER_STREAM_TWITTER:
            recordReader = new TwitterPushRecordReader(TwitterUtil.getTwitterStream(configuration), TwitterUtil.getUserTweetsListener());
            break;
        default:
            throw new HyracksDataException("No Record reader found!");
    }
    return recordReader;
}
Also used : AsterixException(org.apache.asterix.common.exceptions.AsterixException) FilterQuery(twitter4j.FilterQuery) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 5 with FilterQuery

use of twitter4j.FilterQuery in project asterixdb by apache.

the class TwitterUtil method getFilterQuery.

public static FilterQuery getFilterQuery(Map<String, String> configuration) throws AsterixException {
    String locationValue = null;
    // 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;
}
Also used : FilterQuery(twitter4j.FilterQuery)

Aggregations

FilterQuery (twitter4j.FilterQuery)5 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1 TwitterEndpoint (org.apache.camel.component.twitter.TwitterEndpoint)1 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)1 Status (twitter4j.Status)1 StatusAdapter (twitter4j.StatusAdapter)1 TwitterStream (twitter4j.TwitterStream)1 TwitterStreamFactory (twitter4j.TwitterStreamFactory)1 ConfigurationBuilder (twitter4j.conf.ConfigurationBuilder)1