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.
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");
npm install twitter
npm install request
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;
});