Search in sources :

Example 1 with ClientBuilder

use of com.twitter.hbc.ClientBuilder in project nifi by apache.

the class GetTwitter method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) throws MalformedURLException {
    final String endpointName = context.getProperty(ENDPOINT).getValue();
    final Authentication oauth = new OAuth1(context.getProperty(CONSUMER_KEY).getValue(), context.getProperty(CONSUMER_SECRET).getValue(), context.getProperty(ACCESS_TOKEN).getValue(), context.getProperty(ACCESS_TOKEN_SECRET).getValue());
    final ClientBuilder clientBuilder = new ClientBuilder();
    clientBuilder.name("GetTwitter[id=" + getIdentifier() + "]").authentication(oauth).eventMessageQueue(eventQueue).processor(new StringDelimitedProcessor(messageQueue));
    final String languageString = context.getProperty(LANGUAGES).getValue();
    final List<String> languages;
    if (languageString == null) {
        languages = null;
    } else {
        languages = new ArrayList<>();
        for (final String language : context.getProperty(LANGUAGES).getValue().split(",")) {
            languages.add(language.trim());
        }
    }
    final String host;
    final StreamingEndpoint streamingEndpoint;
    if (ENDPOINT_SAMPLE.getValue().equals(endpointName)) {
        host = Constants.STREAM_HOST;
        final StatusesSampleEndpoint sse = new StatusesSampleEndpoint();
        streamingEndpoint = sse;
        if (languages != null) {
            sse.languages(languages);
        }
    } else if (ENDPOINT_FIREHOSE.getValue().equals(endpointName)) {
        host = Constants.STREAM_HOST;
        final StatusesFirehoseEndpoint firehoseEndpoint = new StatusesFirehoseEndpoint();
        streamingEndpoint = firehoseEndpoint;
        if (languages != null) {
            firehoseEndpoint.languages(languages);
        }
    } else if (ENDPOINT_FILTER.getValue().equals(endpointName)) {
        host = Constants.STREAM_HOST;
        final StatusesFilterEndpoint filterEndpoint = new StatusesFilterEndpoint();
        final String followingString = context.getProperty(FOLLOWING).getValue();
        final List<Long> followingIds;
        if (followingString == null) {
            followingIds = Collections.emptyList();
        } else {
            followingIds = new ArrayList<>();
            for (final String split : followingString.split(",")) {
                final Long id = Long.parseLong(split.trim());
                followingIds.add(id);
            }
        }
        final String termString = context.getProperty(TERMS).getValue();
        final List<String> terms;
        if (termString == null) {
            terms = Collections.emptyList();
        } else {
            terms = new ArrayList<>();
            for (final String split : termString.split(",")) {
                terms.add(split.trim());
            }
        }
        if (!terms.isEmpty()) {
            filterEndpoint.trackTerms(terms);
        }
        if (!followingIds.isEmpty()) {
            filterEndpoint.followings(followingIds);
        }
        if (languages != null) {
            filterEndpoint.languages(languages);
        }
        final String locationString = context.getProperty(LOCATIONS).getValue();
        final List<Location> locations;
        if (locationString == null) {
            locations = Collections.emptyList();
        } else {
            locations = LocationUtil.parseLocations(locationString);
        }
        if (!locations.isEmpty()) {
            filterEndpoint.locations(locations);
        }
        streamingEndpoint = filterEndpoint;
    } else {
        throw new AssertionError("Endpoint was invalid value: " + endpointName);
    }
    clientBuilder.hosts(host).endpoint(streamingEndpoint);
    client = clientBuilder.build();
    client.connect();
}
Also used : OAuth1(com.twitter.hbc.httpclient.auth.OAuth1) StreamingEndpoint(com.twitter.hbc.core.endpoint.StreamingEndpoint) Authentication(com.twitter.hbc.httpclient.auth.Authentication) StatusesSampleEndpoint(com.twitter.hbc.core.endpoint.StatusesSampleEndpoint) StatusesFirehoseEndpoint(com.twitter.hbc.core.endpoint.StatusesFirehoseEndpoint) StringDelimitedProcessor(com.twitter.hbc.core.processor.StringDelimitedProcessor) StatusesFilterEndpoint(com.twitter.hbc.core.endpoint.StatusesFilterEndpoint) ClientBuilder(com.twitter.hbc.ClientBuilder) Location(com.twitter.hbc.core.endpoint.Location) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 2 with ClientBuilder

use of com.twitter.hbc.ClientBuilder in project flink by apache.

the class TwitterSource method run.

@Override
public void run(final SourceContext<String> ctx) throws Exception {
    LOG.info("Initializing Twitter Streaming API connection");
    StreamingEndpoint endpoint = initializer.createEndpoint();
    Authentication auth = new OAuth1(properties.getProperty(CONSUMER_KEY), properties.getProperty(CONSUMER_SECRET), properties.getProperty(TOKEN), properties.getProperty(TOKEN_SECRET));
    client = new ClientBuilder().name(properties.getProperty(CLIENT_NAME, "flink-twitter-source")).hosts(properties.getProperty(CLIENT_HOSTS, Constants.STREAM_HOST)).endpoint(endpoint).authentication(auth).processor(new HosebirdMessageProcessor() {

        public DelimitedStreamReader reader;

        @Override
        public void setup(InputStream input) {
            reader = new DelimitedStreamReader(input, Constants.DEFAULT_CHARSET, Integer.parseInt(properties.getProperty(CLIENT_BUFFER_SIZE, "50000")));
        }

        @Override
        public boolean process() throws IOException, InterruptedException {
            String line = reader.readLine();
            ctx.collect(line);
            return true;
        }
    }).build();
    client.connect();
    running = true;
    LOG.info("Twitter Streaming API connection established successfully");
    // just wait now
    while (running) {
        synchronized (waitLock) {
            waitLock.wait(100L);
        }
    }
}
Also used : OAuth1(com.twitter.hbc.httpclient.auth.OAuth1) Authentication(com.twitter.hbc.httpclient.auth.Authentication) InputStream(java.io.InputStream) StreamingEndpoint(com.twitter.hbc.core.endpoint.StreamingEndpoint) DelimitedStreamReader(com.twitter.hbc.common.DelimitedStreamReader) IOException(java.io.IOException) HosebirdMessageProcessor(com.twitter.hbc.core.processor.HosebirdMessageProcessor) ClientBuilder(com.twitter.hbc.ClientBuilder)

Example 3 with ClientBuilder

use of com.twitter.hbc.ClientBuilder in project ignite by apache.

the class TwitterStreamer method buildClient.

/**
 * @param tweetQueue tweet Queue.
 * @param hosts Hostes.
 * @param endpoint Endpoint.
 * @return Client.
 */
protected Client buildClient(BlockingQueue<String> tweetQueue, HttpHosts hosts, StreamingEndpoint endpoint) {
    Authentication authentication = new OAuth1(oAuthSettings.getConsumerKey(), oAuthSettings.getConsumerSecret(), oAuthSettings.getAccessToken(), oAuthSettings.getAccessTokenSecret());
    ClientBuilder builder = new ClientBuilder().name("Ignite-Twitter-Client").hosts(hosts).authentication(authentication).endpoint(endpoint).processor(new StringDelimitedProcessor(tweetQueue));
    return builder.build();
}
Also used : OAuth1(com.twitter.hbc.httpclient.auth.OAuth1) Authentication(com.twitter.hbc.httpclient.auth.Authentication) StringDelimitedProcessor(com.twitter.hbc.core.processor.StringDelimitedProcessor) ClientBuilder(com.twitter.hbc.ClientBuilder)

Aggregations

ClientBuilder (com.twitter.hbc.ClientBuilder)3 Authentication (com.twitter.hbc.httpclient.auth.Authentication)3 OAuth1 (com.twitter.hbc.httpclient.auth.OAuth1)3 StreamingEndpoint (com.twitter.hbc.core.endpoint.StreamingEndpoint)2 StringDelimitedProcessor (com.twitter.hbc.core.processor.StringDelimitedProcessor)2 DelimitedStreamReader (com.twitter.hbc.common.DelimitedStreamReader)1 Location (com.twitter.hbc.core.endpoint.Location)1 StatusesFilterEndpoint (com.twitter.hbc.core.endpoint.StatusesFilterEndpoint)1 StatusesFirehoseEndpoint (com.twitter.hbc.core.endpoint.StatusesFirehoseEndpoint)1 StatusesSampleEndpoint (com.twitter.hbc.core.endpoint.StatusesSampleEndpoint)1 HosebirdMessageProcessor (com.twitter.hbc.core.processor.HosebirdMessageProcessor)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)1