twitter2mongodb

Extract data from the Twitter Application Programming Interface (API) to a MongoDB collection.

twitter2mongodb
Parameters
options (Object = {}) options for this function.
Name Description
options.twitter Object (default {}) options for twitter (see twitter2return ).
options.twitter.stream function (default function(err,data){}) callback function on a stream 'data' event for the returned Twitter stream .
  • err is the Error object
  • data is in the form of {twitter: {stream: stream, tweets: Object}, mongodb: {client: Object, results: Object}}
  • data.twitter.stream is the twitter stream
  • data.twitter.tweets are the tweets in JSON format
  • data.mongodb.client: contains the MongoDB client
  • data.mongodb.collection: contains the MongoDB collection object from options.mongodb.collection
  • data.mongodb.db: contains the MongoDB db instance from options.mongodb.connection
  • data.mongodb.results: contains the MongoDB method results of options.mongodb.method
options.mongodb Object (default {}) contains options for methods in mongodb .
options.mongodb.connection string (default process.env.MONGODB_CONNECTION||'mongodb://localhost:27017') MongoDB connection string .
options.mongodb.database string (default process.env.MONGODB_DATABASE||'test') MongoDB database name .
options.mongodb.collection string (default process.env.MONGODB_COLLECTION||'twitter_data') Mongodb collection name.
options.mongodb.options (string | Object) (default process.env.MONGODB_OPTIONS) Mongodb client connect options .
options.mongodb.method string (default process.env.MONGODB_METHOD||'insertOne') Mongodb collection method.
options.mongodb.method_options (string | Object) (default process.env.MONGODB_METHOD_OPTIONS) Mongodb collection method options relative to options.mongodb.method .
options.mongodb.check function (default function(tweets){return(true)};) a function that performs a true or false test to determine whether to insert the tweet or not
  • options.mongodb.check is in the form of function(tweets) {return (true || false)};
  • tweets is a tweets object for checking
  • Return true to insert the data into MongoDB or false to skip insertion
options.jsonata string (default process.env.JSONATA) jsonata query for the received tweet object in JSON format before inserting into the MongoDB collection ( options.mongodb.collection ).
Returns
(Promise | stream): Returns a stream if options.twitter.method is 'stream', otherwise returns a Promise:

If options.twitter.method == 'stream'

  • Return a Twitter stream
  • stream.on('data', function): calls function when a tweet is available
  • stream.on('error', function): calls function when there is an error

Else

  • Return a Promise object that resolves a data object in the form {twitter: {client: ..., tweets: ...}, mongodb: {client: ..., results: ...}}

  • data.twitter.client: contains a Twitter client object created from options.twitter.connection

  • data.twitter.tweets: contains the tweets in JSON format

  • data.mongodb.client: contains the MongoDB client

  • data.mongodb.collection: contains the MongoDB collection object from options.mongodb.collection

  • data.mongodb.db: contains the MongoDB (@link https://mongodb.github.io/node-mongodb-native/3.0/api/Db db instance} from options.mongodb.connection

  • data.mongodb.results: contains the MongoDB method results of options.mongodb.method

Example
var twitter2mongodb = require('twitter2mongodb');

// (options) Initialize options object
var options = {
	twitter: {},
	mongodb: {}
};

// *** CONNECTION SETUP ***

// (options_twitter_connection) Twitter API connection keys
options.twitter.connection =  {
	consumer_key: '***', // process.env.TWITTER_CONSUMER_KEY
	consumer_secret: '***', // process.env.TWITTER_CONSUMER_SECRET
	access_token_key: '***', // process.env.TWITTER_ACCESS_TOKEN_KEY
	access_token_secret: '***' // process.env.TWITTER_ACCESS_TOKEN_SECRET
};

// (options_mongodb_connection) MongoDB connection details
// Format: 'mongodb://<user>:<password>@<host>:<port>/<database>'
options.mongodb.connection = 'mongodb://localhost:27017/test';

// *** SEARCH TWEETS ***

// (options_twitter_rest) Search for keyword 'twitter' in path 'GET search/tweets'
options.twitter.method = 'get'; // get, post, or stream
options.twitter.path = 'search/tweets'; // api path
options.twitter.params = {q: 'twitter'}; // query tweets

// (options_jsonata) Filter for statuses array using jsonata
options.jsonata = 'statuses';

// (options_mongodb) MongoDB options
options.mongodb.method = 'insertMany'; // insert many objects due to array

// (twitter2mongodb_rest) Query tweets using REST API into MongoDB collection
twitter2mongodb(options)
	.then(data => {
		console.log(data);
	}).catch(err => {
		console.error(err.message);
	});

// *** STREAM TWEETS ***

// (options_twitter_connection) Track keyword 'twitter' in path 'POST statuses/filter'
options.twitter.method = 'stream'; // get, post, or stream
options.twitter.path = 'statuses/filter'; // api path
options.twitter.params = {track: 'twitter'}; // query tweets

// (options_mongodb) MongoDB options
options.mongodb.method = 'insertOne';

// (options_jsonata) Remove jsonata filter
delete options.jsonata;

// (twitter2mongodb_stream) Stream tweets into MongoDB collection
var stream = twitter2mongodb(options);
stream.on('error', function(error) {
	console.error(error.message);
});