Twitfinder with Twit Stream
Today's project is a variation on yesterday's theme, although with a much more sophisticated interface. I used the Twit module in nodejs in order to open a stream from the Twitter API. Search terms can now be tracked in real-time and written to a file using the fs module. I think I will continue expanding on this in future posts in order to write to some sort of a database instead of a file.
In the learning process for today's creation, I managed to write a significant amount of simple code to discover the output of different routines by trial and error. I mostly used console.log(tweet)
statements to find the correct part of the JSON from Twitter to use in my code. Ultimately, I found that tweet['user']['screen_name']
resulted in a string format for the @username that is standard in Twitter.
The rest of the code is shared below.
var Twit = require('twit');
var config1 = require('./config1');
var fs = require('fs');
var searchTerm = 'bike';
var totalUsers = 0;
var T = new Twit(config1);
var stream = T.stream('statuses/filter',{track:searchTerm});
stream.on('tweet',function(tweet) {
var temp = tweet['user']['screen_name'];
var userList = {};
userList[temp] = 1;
fs.appendFile('usertest.txt',JSON.stringify(userList),function(err) {
if (err) return console.log(err);
});
console.log(tweet['user']['screen_name']);
});