Reputation: 21
I've been trying to figure out how to use the timeout, problem with my code is that its way to fast, I could not discover how to do it with Request. everything else seems to work great
var Scraper = function() {
for (i in userid) {
//create complete url
totalurl = facebookurl + userid[i];
// request facebook open graph
// WARNING the request func should be called every 1-3 secs?
request(totalurl, function(error, response, html) {
if (!error && response.statusCode == 200) {
// console.log(html);
}
});
};
};
Scraper();
Upvotes: 0
Views: 106
Reputation: 18895
This is the gist of it using recursion:
var userids = [//ids here];
function processNextUrl(index){
var userid = userids[index];
totalurl = facebookurl + userid[index];
request(totalurl, function(error, response, html) {
if (!error && response.statusCode == 200) {
//process
}
if(userids.length > index -1){ //stuff left to do
setTimeout(function(){
processNextUrl(index+1);
}, 3000);
}
});
}
processNextUrl(0); //kickstart the lot
Upvotes: 2
Reputation: 2287
var EE = require('events').EventEmitter;
var Scraper = function(userId)
{
this.Emitter = new EE();
this.userID = userId;
this.current = -1;
};
Scraper.prototype =
{
execute : function()
{
var _self = this;
this.Emitter.on('_requestDone', function(data)
{
return _self.computeData(data);
});
this.Emitter.on('_errorHappened', function(e)
{
return console.log(e);
});
return this.sendRequest();
},
sendRequest : function()
{
this.current++;
var totalurl = facebookurl + this.userID[this.current];
var _self = this;
request(totalurl, function(error, response, html)
{
if (!error && response.statusCode == 200)
{
return _self.Emitter.emit('_requestDone', html);
}
else
{
return _self.Emitter.emit('_errorHappened', error);
}
});
},
computeData : function(data)
{
// Perform your actions on data
if(this.current < this.userID.length)
{
return this.sendRequest();
}
else
{
// When all the array has been computed
}
}
};
var S = new Scraper(/* Put your userId Array Here */);
S.execute();
Upvotes: 0