For some reason, I am trying to present a simple script that can extract tweets from a given Twitter account and then do something with it (which is not a really important part). Following a friend's advice, I tried to do this in nodejs. He helped me a bit (gave me some code), and I managed to do something. Therefore, if I want to get all the tweets from one user, it works great. But in fact, what I would like is a loop and check for new tweets. But when I convert my code at a simple moment (1) (and change it as needed), it does nothing. So here is my code:
var tweetFetcher = require('./tweetFetcher');
var codeExtractor = require('./codeExtractor');
var open = require('open');
var fs = require('fs');
var filters = [filter1, filter2];
var hasfirstpart = 0;
var code = "";
var tweets;
var i = 0;
while(1){
tweets = tweetFetcher.getFrom('twitteraccount', function(tweets) {
console.log(tweets[0]);
if(tweets[0].indexOf(filters[hasfirstpart]) > -1){
code += codeExtractor.extract(tweets[0]).substring(3);
hasfirstpart = (hasfirstpart+1)%2;
console.log(code);
}
if(hasfirstpart == 0 && code != ""){
...
}
});
}
var parser = require('cheerio');
var request = require('request');
exports.getFrom = function(username, cb) {
var url = "http://twitter.com/"+username;
request(url, function(err, resp, body) {
var $ = parser.load(body);
var tweets = [];
$('.ProfileTweet-text').each(function() {
tweets.push($(this).text());
});
if(typeof cb == 'function') {
cb(tweets);
}
});
}
Do you have an idea why it cannot be wrapped in a loop? Thank you in advance for your reply :)