How to get tweets Using twitter's API with node.js and storing tweets in a file


What do we intend to make ?

Capturing tweets for data acquisition purposes in data science projects is quite common these days. In the tutorial to follow, we will learn how we can capture a stream of tweets filtered using certain keywords and all the steps required to accomplish this will be outlined.

Prerequisites

  • Node.js Installed
  • In order to access twitter from node.js application , you'll need to have your app credentials which include the following ::
    1. Consumer Key
    2. Consumer Secret
    3. Access Token Key
    4. Access Token Secret
    You can get it HERE
Let's Start !

Step - 1 : We will start by installing all the required packages from npm. We are using the following packages in our application :

		        
//Twitter.js
var Twitter = require('twitter');
var fs = require("fs");
var request = require("request");
                
	            

We can install them one by one using the following command :
twitter

		        
npm install twitter
                
	            

request
		        
npm install request
                
	            

fs
		        
npm install fs
                
	            

Step - 2 : Add your credentials

		        
//Twitter.js

var client = new Twitter({
  consumer_key: 'Your_CONSUMER_KEY',
  consumer_secret: 'Your_CONSUMER_SECRET',
  access_token_key: 'Your_ACCESS_TOKEN_KEY',
  access_token_secret: 'YOUR_ACCESS_TOKEN_SECRET'
});
                
	            

Step - 3 : Now it's time to collect tweets. Using the following code you will be collecting tweets related to IPL i.e. Indian premier league. You can change it to any keyword. And Further we will store all the tweets in the file < tweet.txt > using fs module of node.js

		        
//Twitter.js

var stream = client.stream('statuses/filter', {track: 'ipl'});
stream.on('data', function(event) {
  console.log("Tweeted by ::::>>>" + event.user.name + " ::::>>> " +  "Tweet is :::>>>> " + event.text + " ::::>>>");
  fs.appendFile("tweettttt.txt", JSON.stringify(event));
});

stream.on('error', function(error) {
  throw error;
});