com.twitter.hbc.ClientBuilder Java Examples

The following examples show how to use com.twitter.hbc.ClientBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwitterSources.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
private TwitterStreamSourceContext(
        @Nonnull ILogger logger,
        @Nonnull Properties credentials,
        @Nonnull String host,
        @Nonnull SupplierEx<? extends StreamingEndpoint> endpointSupplier
) {
    this.logger = logger;
    checkTwitterCredentials(credentials);
    String consumerKey = credentials.getProperty("consumerKey");
    String consumerSecret = credentials.getProperty("consumerSecret");
    String token = credentials.getProperty("token");
    String tokenSecret = credentials.getProperty("tokenSecret");

    Authentication auth = new OAuth1(consumerKey, consumerSecret, token, tokenSecret);
    client = new ClientBuilder()
            .hosts(host)
            .endpoint(endpointSupplier.get())
            .authentication(auth)
            .processor(new StringDelimitedProcessor(queue))
            .build();
    client.connect();
}
 
Example #2
Source File: TwitterDataSource.java    From kafka-streams with Apache License 2.0 6 votes vote down vote up
private  Client getTwitterClient(Properties props, BlockingQueue<String> messageQueue) {

        String clientName = props.getProperty("clientName");
        String consumerKey = props.getProperty("consumerKey");
        String consumerSecret = props.getProperty("consumerSecret");
        String token = props.getProperty("token");
        String tokenSecret = props.getProperty("tokenSecret");
        List<String> searchTerms = Arrays.asList(props.getProperty("searchTerms").split(","));

        Authentication authentication = new OAuth1(consumerKey,consumerSecret,token,tokenSecret);
        Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
        StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();

        hosebirdEndpoint.trackTerms(searchTerms);

        ClientBuilder clientBuilder = new ClientBuilder();
        clientBuilder.name(clientName)
                .hosts(hosebirdHosts)
                .authentication(authentication)
                .endpoint(hosebirdEndpoint)
                .processor(new StringDelimitedProcessor(messageQueue));

          return clientBuilder.build();

    }
 
Example #3
Source File: TwitterStream.java    From AIBlueprints with MIT License 5 votes vote down vote up
public TwitterStream(SentimentDetector sentimentDetector,
                     Gson gson,
                     Properties props) {
    this.sentimentDetector = sentimentDetector;
    this.gson = gson;

    msgQueue = new LinkedBlockingQueue<String>(100000);

    Hosts hosts = new HttpHosts(Constants.STREAM_HOST);
    StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();

    List<String> terms = Lists.newArrayList(
            props.getProperty("twitter_terms")
                    .split("\\s*,\\s*"));
    endpoint.trackTerms(terms);

    Authentication auth = new OAuth1(
            props.getProperty("twitter_consumer_key"),
            props.getProperty("twitter_consumer_secret"),
            props.getProperty("twitter_token"),
            props.getProperty("twitter_token_secret"));
    ClientBuilder builder = new ClientBuilder()
            .name("SmartCode-Client-01")
            .hosts(hosts)
            .authentication(auth)
            .endpoint(endpoint)
            .processor(new StringDelimitedProcessor(msgQueue));
    client = builder.build();
    client.connect();
}
 
Example #4
Source File: TwitterStream.java    From AIBlueprints with MIT License 5 votes vote down vote up
public TwitterStream(Gson gson, Properties props, BlockingQueue<Map<String,Object>> imageQueue) {
    this.gson = gson;
    this.imageQueue = imageQueue;

    msgQueue = new LinkedBlockingQueue<String>(100000);

    Hosts hosts = new HttpHosts(Constants.STREAM_HOST);
    StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();

    List<String> terms = Lists.newArrayList(
            props.getProperty("twitter_terms")
                    .split("\\s*,\\s*"));
    endpoint.trackTerms(terms);

    Authentication auth = new OAuth1(
            props.getProperty("twitter_consumer_key"),
            props.getProperty("twitter_consumer_secret"),
            props.getProperty("twitter_token"),
            props.getProperty("twitter_token_secret"));
    ClientBuilder builder = new ClientBuilder()
            .name("SmartCode-Client-01")
            .hosts(hosts)
            .authentication(auth)
            .endpoint(endpoint)
            .processor(new StringDelimitedProcessor(msgQueue));
    client = builder.build();
    client.connect();
}
 
Example #5
Source File: SampleStreamExample.java    From Java-for-Data-Science with MIT License 5 votes vote down vote up
public static void streamTwitter(String consumerKey, String consumerSecret, String accessToken, String accessSecret) throws InterruptedException {

		BlockingQueue<String> statusQueue = new LinkedBlockingQueue<String>(10000);

		StatusesSampleEndpoint ending = new StatusesSampleEndpoint();
		ending.stallWarnings(false);

		Authentication twitterAuth = new OAuth1(consumerKey, consumerSecret, accessToken, accessSecret);

		BasicClient twitterClient = new ClientBuilder()
				.name("Twitter client")
				.hosts(Constants.STREAM_HOST)
				.endpoint(ending)
				.authentication(twitterAuth)
				.processor(new StringDelimitedProcessor(statusQueue))
				.build();


		twitterClient.connect();


		for (int msgRead = 0; msgRead < 1000; msgRead++) {
			if (twitterClient.isDone()) {
				System.out.println(twitterClient.getExitEvent().getMessage());
				break;
			}

			String msg = statusQueue.poll(10, TimeUnit.SECONDS);
			if (msg == null) {
				System.out.println("Waited 10 seconds - no message received");
			} else {
				System.out.println(msg);
			}
		}

		twitterClient.stop();

		System.out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());
	}
 
Example #6
Source File: EnterpriseStreamExample.java    From hbc with Apache License 2.0 5 votes vote down vote up
public static void run(String username,
                       String password,
                       String account,
                       String label,
                       String product) throws InterruptedException {
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);

  BasicAuth auth = new BasicAuth(username, password);

  RealTimeEnterpriseStreamingEndpoint endpoint = new RealTimeEnterpriseStreamingEndpoint(account, product, label);

  // Create a new BasicClient. By default gzip is enabled.
  Client client = new ClientBuilder()
          .name("PowerTrackClient-01")
          .hosts(Constants.ENTERPRISE_STREAM_HOST)
          .endpoint(endpoint)
          .authentication(auth)
          .processor(new LineStringProcessor(queue))
          .build();

  // Establish a connection
  client.connect();

  // Do whatever needs to be done with messages
  for (int msgRead = 0; msgRead < 1000; msgRead++) {
    String msg = queue.take();
    System.out.println(msg);
  }

  client.stop();
}
 
Example #7
Source File: Twitter4jSampleStreamExample.java    From hbc with Apache License 2.0 5 votes vote down vote up
public void oauth(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
  // Create an appropriately sized blocking queue
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);

  // Define our endpoint: By default, delimited=length is set (we need this for our processor)
  // and stall warnings are on.
  StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();

  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
  // Authentication auth = new BasicAuth(username, password);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
    .hosts(Constants.STREAM_HOST)
    .endpoint(endpoint)
    .authentication(auth)
    .processor(new StringDelimitedProcessor(queue))
    .build();

  // Create an executor service which will spawn threads to do the actual work of parsing the incoming messages and
  // calling the listeners on each message
  int numProcessingThreads = 4;
  ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);

  // Wrap our BasicClient with the twitter4j client
  Twitter4jStatusClient t4jClient = new Twitter4jStatusClient(
    client, queue, Lists.newArrayList(listener1, listener2), service);

  // Establish a connection
  t4jClient.connect();
  for (int threads = 0; threads < numProcessingThreads; threads++) {
    // This must be called once per processing thread
    t4jClient.process();
  }

  Thread.sleep(5000);

  client.stop();
}
 
Example #8
Source File: FilterStreamExample.java    From hbc with Apache License 2.0 5 votes vote down vote up
public static void run(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
  StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
  // add some track terms
  endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo"));

  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
  // Authentication auth = new BasicAuth(username, password);

  // Create a new BasicClient. By default gzip is enabled.
  Client client = new ClientBuilder()
          .hosts(Constants.STREAM_HOST)
          .endpoint(endpoint)
          .authentication(auth)
          .processor(new StringDelimitedProcessor(queue))
          .build();

  // Establish a connection
  client.connect();

  // Do whatever needs to be done with messages
  for (int msgRead = 0; msgRead < 1000; msgRead++) {
    String msg = queue.take();
    System.out.println(msg);
  }

  client.stop();

}
 
Example #9
Source File: TwitterHandler.java    From spring4ws-demos with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
	BlockingQueue<String> queue = new LinkedBlockingQueue<>(100);
	StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();

	endpoint.trackTerms(ImmutableList.of("ExtJS", "#extjs", "Sencha", "#java",
			"java8", "java9", "#websocket", "#SpringFramework", "html5", "javascript",
			"#kotlin", "kotlin"));
	endpoint.languages(ImmutableList.of("en", "de"));

	String consumerKey = this.environment.getProperty("twitter4j.oauth.consumerKey");
	String consumerSecret = this.environment
			.getProperty("twitter4j.oauth.consumerSecret");
	String accessToken = this.environment.getProperty("twitter4j.oauth.accessToken");
	String accessTokenSecret = this.environment
			.getProperty("twitter4j.oauth.accessTokenSecret");

	Authentication auth = new OAuth1(consumerKey, consumerSecret, accessToken,
			accessTokenSecret);

	this.client = new ClientBuilder().hosts(Constants.STREAM_HOST).endpoint(endpoint)
			.authentication(auth).processor(new StringDelimitedProcessor(queue))
			.build();

	this.executorService = Executors.newSingleThreadExecutor();

	TwitterStatusListener statusListener = new TwitterStatusListener(
			this.messagingTemplate, this.lastTweets);
	this.t4jClient = new Twitter4jStatusClient(this.client, queue,
			ImmutableList.of(statusListener), this.executorService);

	this.t4jClient.connect();
	this.t4jClient.process();
}
 
Example #10
Source File: TwitterStream.java    From Java-for-Data-Science with MIT License 4 votes vote down vote up
public Stream<TweetHandler> stream() {
        String myKey = "sl2WbCf4UnIr08xvHVitHJ99r";
        String mySecret = "PE6yauvXjKLuvoQNXZAJo5C8N5U5piSFb3udwkoI76paK6KyqI";
        String myToken = "1098376471-p6iWfxCLtyMvMutTb010w1D1xZ3UyJhcC2kkBjN";
        String myAccess = "2o1uGcp4b2bFynOfu2cA1uz63n5aruV0RwNsUjRpjDBZS";

        out.println("Creating Twitter Stream");
        BlockingQueue<String> statusQueue = new LinkedBlockingQueue<>(1000);
        StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
        endpoint.trackTerms(Lists.newArrayList("twitterapi", this.topic));
        endpoint.stallWarnings(false);
        Authentication twitterAuth = new OAuth1(myKey, mySecret, myToken, myAccess);

        BasicClient twitterClient = new ClientBuilder()
                .name("Twitter client")
                .hosts(Constants.STREAM_HOST)
                .endpoint(endpoint)
                .authentication(twitterAuth)
                .processor(new StringDelimitedProcessor(statusQueue))
                .build();

        twitterClient.connect();

        List<TweetHandler> list = new ArrayList();
        List<String> twitterList = new ArrayList();

        statusQueue.drainTo(twitterList);
        for(int i=0; i<numberOfTweets; i++) {
            String message;
            try {
                message = statusQueue.take();
                list.add(new TweetHandler(message));
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

//        for (int msgRead = 0; msgRead < this.numberOfTweets; msgRead++) {
//            try {
//                if (twitterClient.isDone()) {
//                  //  out.println(twitterClient.getExitEvent().getMessage());
//                    break;
//                }
//
//                String msg = statusQueue.poll(10, TimeUnit.SECONDS);
//                if (msg == null) {
//                    out.println("Waited 10 seconds - no message received");
//                } else {
//                    list.add(new TweetHandler(msg));
//                    out.println("Added message: " + msg.length());
//                }
//            } catch (InterruptedException ex) {
//                ex.printStackTrace();
//            }
//        }
        twitterClient.stop();
        out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());

        return list.stream();
    }
 
Example #11
Source File: TwitterProducer.java    From aws-big-data-blog with Apache License 2.0 4 votes vote down vote up
public void run(String propFilePath) {
	loadFileProperties(propFilePath, DEFAULT_PROP_FILE_NAME);

	String consumerKey = System.getProperty(TWIT_CONSUMER_KEY);
	String consumerSecret = System.getProperty(TWIT_CONSUMER_SECRET);
	String token = System.getProperty(TWIT_TOKEN);
	String secret = System.getProperty(TWIT_SECRET);
	String streamName = System.getProperty(STREAM_NAME);
	String regionName = System.getProperty(REGION_NAME);
	
	while (true) {
		/**
		 * Set up your blocking queues: Be sure to size these properly based
		 * on expected TPS of your stream
		 */
		BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(
				10000);

		/**
		 * Declare the host you want to connect to, the endpoint, and
		 * authentication (basic auth or oauth)
		 */
		StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
		
		// Track  anything that is geo-tagged
		endpoint.addQueryParameter("locations", "-180,-90,180,90");

		// These secrets should be read from a config file
		Authentication hosebirdAuth = new OAuth1(consumerKey,
				consumerSecret, token, secret);

		// create a new basic client - by default gzip is enabled
		Client client = new ClientBuilder().hosts(Constants.STREAM_HOST)
				.endpoint(endpoint).authentication(hosebirdAuth)
				.processor(new StringDelimitedProcessor(msgQueue)).build();

		client.connect();
		

		LOG.info("Got connection to Twitter");
		
		// create producer
		ProducerClient producer = new ProducerBuilder()
				.withName("Twitter")
				.withStreamName(streamName)
				.withRegion(regionName)
				.withThreads(10)
				.build();
		
		producer.connect();
		
		LOG.info("Got connection to Kinesis");

		try {
			if (process(msgQueue, producer)) {
				break;
			}

		} catch (Exception e) {
			// if we get here, our client has broken, throw away and
			// recreate
			e.printStackTrace();
		}
		// also, if we make it here, we have had a problem, so start again
		client.stop();
	}
}
 
Example #12
Source File: SampleStreamExample.java    From hbc with Apache License 2.0 4 votes vote down vote up
public static void run(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
  // Create an appropriately sized blocking queue
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);

  // Define our endpoint: By default, delimited=length is set (we need this for our processor)
  // and stall warnings are on.
  StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
  endpoint.stallWarnings(false);

  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
  //Authentication auth = new com.twitter.hbc.httpclient.auth.BasicAuth(username, password);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
          .name("sampleExampleClient")
          .hosts(Constants.STREAM_HOST)
          .endpoint(endpoint)
          .authentication(auth)
          .processor(new StringDelimitedProcessor(queue))
          .build();

  // Establish a connection
  client.connect();

  // Do whatever needs to be done with messages
  for (int msgRead = 0; msgRead < 1000; msgRead++) {
    if (client.isDone()) {
      System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
      break;
    }

    String msg = queue.poll(5, TimeUnit.SECONDS);
    if (msg == null) {
      System.out.println("Did not receive a message in 5 seconds");
    } else {
      System.out.println(msg);
    }
  }

  client.stop();

  // Print some stats
  System.out.printf("The client read %d messages!\n", client.getStatsTracker().getNumMessages());
}
 
Example #13
Source File: SitestreamExample.java    From hbc with Apache License 2.0 4 votes vote down vote up
public void run(String consumerKey, String consumerSecret, String token, String tokenSecret)
        throws InterruptedException, ControlStreamException, IOException {
  // Create an appropriately sized blocking queue
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);

  // Define our endpoint: By default, delimited=length is set (we need this for our processor)
  // and stall warnings are on.
  List<Long> followings = new ArrayList<Long>();
  followings.add(111111111L);
  followings.add(222222222L);

  SitestreamEndpoint endpoint = new SitestreamEndpoint(followings);
  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, tokenSecret);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
          .hosts(Constants.SITESTREAM_HOST)
          .endpoint(endpoint)
          .authentication(auth)
          .processor(new StringDelimitedProcessor(queue))
          .build();

  // Create an executor service which will spawn threads to do the actual work of parsing the incoming messages and
  // calling the listeners on each message
  int numProcessingThreads = 4;
  ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);

  // Wrap our BasicClient with the twitter4j client
  Twitter4jSitestreamClient t4jClient = new Twitter4jSitestreamClient(
          client, queue, Lists.newArrayList(listener), service);

  // Establish a connection
  t4jClient.connect();
  for (int threads = 0; threads < numProcessingThreads; threads++) {
    // This must be called once per processing thread
    t4jClient.process();
  }

  Thread.sleep(5000);

  // Create a sitestream controller to issue controlstream requests
  SitestreamController controller = new SitestreamController(auth);

  controller.getFriends(t4jClient.getStreamId(), 12345L);
  controller.addUser(t4jClient.getStreamId(), 987765L);

  client.stop();
}
 
Example #14
Source File: HoseBirdTester.java    From kafka-streams with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args)  throws InterruptedException {

        BlockingQueue<String> msgQueue = new LinkedBlockingDeque<>();



        Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
        StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();
        List<String> terms = Lists.newArrayList("superman vs batman","#supermanvsbatman");
        hosebirdEndpoint.trackTerms(terms);




        Authentication hosebirdAuth  = new OAuth1("18qydWMuiUohwCtQpp1MOFCFr",
                                                  "YrYhYd09LKZLbhsKT1o4XcEPl6HiAoNykiOxYBq0dAB8t0vRCo",
                                                  "16972669-KSvyDEMc7dussPfW6a9Ru65L4eWGj637ciHLHZLyn",
                                                  "ky53NE6cbBvtNLopto7o9gVyHDejSB2kPsRhHGKEd1MrS");


        ClientBuilder clientBuilder = new ClientBuilder();
        clientBuilder.name("bbejeck-hosebird")
                     .hosts(hosebirdHosts)
                     .authentication(hosebirdAuth)
                     .endpoint(hosebirdEndpoint)
                     .processor(new StringDelimitedProcessor(msgQueue));

        Client hosebirdClient = clientBuilder.build();
        hosebirdClient.connect();

        for (int msgRead = 0; msgRead < 100; msgRead++) {
            String msg = msgQueue.take();
            System.out.println(msg);
        }

        hosebirdClient.stop();

    }