Java Code Examples for twitter4j.conf.ConfigurationBuilder#setIncludeEntitiesEnabled()

The following examples show how to use twitter4j.conf.ConfigurationBuilder#setIncludeEntitiesEnabled() . 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: TwitterProducer.java    From lsiem with Apache License 2.0 4 votes vote down vote up
private void start(Context context) {

/** Producer properties **/
Properties props = new Properties();
props.put("metadata.broker.list", context.getString(TwitterSourceConstant.BROKER_LIST));
props.put("serializer.class", context.getString(TwitterSourceConstant.SERIALIZER));
props.put("request.required.acks", context.getString(TwitterSourceConstant.REQUIRED_ACKS));

ProducerConfig config = new ProducerConfig(props);

final Producer<String, String> producer = new Producer<String, String>(config);

/** Twitter properties **/
consumerKey = context.getString(TwitterSourceConstant.CONSUMER_KEY_KEY);
consumerSecret = context.getString(TwitterSourceConstant.CONSUMER_SECRET_KEY);
accessToken = context.getString(TwitterSourceConstant.ACCESS_TOKEN_KEY);
accessTokenSecret = context.getString(TwitterSourceConstant.ACCESS_TOKEN_SECRET_KEY);

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(consumerKey);
cb.setOAuthConsumerSecret(consumerSecret);
cb.setOAuthAccessToken(accessToken);
cb.setOAuthAccessTokenSecret(accessTokenSecret);
cb.setJSONStoreEnabled(true);
cb.setIncludeEntitiesEnabled(true);

twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
final Map<String, String> headers = new HashMap<String, String>();

/** Twitter listener **/
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
	    // the raw JSON of a tweet
	    logger.info(status.getUser().getScreenName() + ": " + status.getText()); //delete uncomment sign
	    
	    KeyedMessage<String, String> data = new KeyedMessage<String, String>(context.getString(TwitterSourceConstant.KAFKA_TOPIC)
										 , DataObjectFactory.getRawJSON(status));
	    producer.send(data);
	    
	}
	    
	public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
	
	public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
	
	public void onScrubGeo(long userId, long upToStatusId) {}
	
	public void onException(Exception ex) {
	    logger.info("Shutting down Twitter sample stream...");
	    //twitterStream.shutdown();
	}
	
	public void onStallWarning(StallWarning warning) {}
    };

/** Bind the listener **/
twitterStream.addListener(listener);
/** GOGOGO **/
twitterStream.sample();   
   }
 
Example 2
Source File: TwitterSpout.java    From StormTweetsSentimentD3Viz with Apache License 2.0 4 votes vote down vote up
@Override
public final void open(final Map conf, final TopologyContext context,
                 final SpoutOutputCollector collector) {
	this._queue = new LinkedBlockingQueue<>(1000);
	this._outputCollector = collector;

	final StatusListener statusListener = new StatusListener() {
		@Override
		public void onStatus(final Status status) {
			_queue.offer(status);
		}

		@Override
		public void onDeletionNotice(final StatusDeletionNotice sdn) {
		}

		@Override
		public void onTrackLimitationNotice(final int i) {
		}

		@Override
		public void onScrubGeo(final long l, final long l1) {
		}

		@Override
		public void onStallWarning(final StallWarning stallWarning) {
		}

		@Override
		public void onException(final Exception e) {
		}
	};
	//Twitter stream authentication setup
	final Properties properties = new Properties();
	try {
		properties.load(TwitterSpout.class.getClassLoader()
				                .getResourceAsStream(Constants.CONFIG_PROPERTIES_FILE));
	} catch (final IOException ioException) {
		//Should not occur. If it does, we cant continue. So exiting the program!
		LOGGER.error(ioException.getMessage(), ioException);
		System.exit(1);
	}

	final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
	configurationBuilder.setIncludeEntitiesEnabled(true);

	configurationBuilder.setOAuthAccessToken(properties.getProperty(Constants.OAUTH_ACCESS_TOKEN));
	configurationBuilder.setOAuthAccessTokenSecret(properties.getProperty(Constants.OAUTH_ACCESS_TOKEN_SECRET));
	configurationBuilder.setOAuthConsumerKey(properties.getProperty(Constants.OAUTH_CONSUMER_KEY));
	configurationBuilder.setOAuthConsumerSecret(properties.getProperty(Constants.OAUTH_CONSUMER_SECRET));
	this._twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
	this._twitterStream.addListener(statusListener);

	//Our usecase computes the sentiments of States of USA based on tweets.
	//So we will apply filter with US bounding box so that we get tweets specific to US only.
	final FilterQuery filterQuery = new FilterQuery();

	//Bounding Box for United States.
	//For more info on how to get these coordinates, check:
	//http://www.quora.com/Geography/What-is-the-longitude-and-latitude-of-a-bounding-box-around-the-continental-United-States
	final double[][] boundingBoxOfUS = {{-124.848974, 24.396308},
			                                   {-66.885444, 49.384358}};
	filterQuery.locations(boundingBoxOfUS);
	this._twitterStream.filter(filterQuery);
}
 
Example 3
Source File: TwitterSpout.java    From StormTweetsSentimentAnalysis with Apache License 2.0 4 votes vote down vote up
@Override
public final void open(final Map conf, final TopologyContext context,
                 final SpoutOutputCollector collector) {
	this._queue = new LinkedBlockingQueue<>(1000);
	this._outputCollector = collector;

	final StatusListener statusListener = new StatusListener() {
		@Override
		public void onStatus(final Status status) {
			_queue.offer(status);
		}

		@Override
		public void onDeletionNotice(final StatusDeletionNotice sdn) {
		}

		@Override
		public void onTrackLimitationNotice(final int i) {
		}

		@Override
		public void onScrubGeo(final long l, final long l1) {
		}

		@Override
		public void onStallWarning(final StallWarning stallWarning) {
		}

		@Override
		public void onException(final Exception e) {
		}
	};
	//Twitter stream authentication setup
	final Properties properties = new Properties();
	try {
		properties.load(TwitterSpout.class.getClassLoader()
				                .getResourceAsStream(Constants.CONFIG_PROPERTIES_FILE));
	} catch (final IOException ioException) {
		//Should not occur. If it does, we cant continue. So exiting the program!
		LOGGER.error(ioException.getMessage(), ioException);
		System.exit(1);
	}

	final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
	configurationBuilder.setIncludeEntitiesEnabled(true);

	configurationBuilder.setOAuthAccessToken(properties.getProperty(Constants.OAUTH_ACCESS_TOKEN));
	configurationBuilder.setOAuthAccessTokenSecret(properties.getProperty(Constants.OAUTH_ACCESS_TOKEN_SECRET));
	configurationBuilder.setOAuthConsumerKey(properties.getProperty(Constants.OAUTH_CONSUMER_KEY));
	configurationBuilder.setOAuthConsumerSecret(properties.getProperty(Constants.OAUTH_CONSUMER_SECRET));
	this._twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
	this._twitterStream.addListener(statusListener);

	//Our usecase computes the sentiments of States of USA based on tweets.
	//So we will apply filter with US bounding box so that we get tweets specific to US only.
	final FilterQuery filterQuery = new FilterQuery();

	//Bounding Box for United States.
	//For more info on how to get these coordinates, check:
	//http://www.quora.com/Geography/What-is-the-longitude-and-latitude-of-a-bounding-box-around-the-continental-United-States
	final double[][] boundingBoxOfUS = {{-124.848974, 24.396308},
			                                   {-66.885444, 49.384358}};
	filterQuery.locations(boundingBoxOfUS);
	this._twitterStream.filter(filterQuery);
}