Node.js Recursive Loop fail

180 Views Asked by At

Below is the Node.js Script. It downloads the images contained in a div. The loop works fine for 9.86% that is upto id = 36. When id > 36 it exits the loop. I am using the node 0.12 version. The loop needs to run 365 times before its completion. I am usign the method of recursive callback.

Code:

//Required modules
var fs = require('fs'),
         cheerio = require('cheerio'),
         request = require('request');

//Default Variables
var baseURI =  'http://www.website/';
var year = 2013;
var id = 1;
var savePath = process.argv[2]; 

//Download Function
var download = function(uri, filename, callback){
    request({ uri: uri }, function(err, res, body){    
    var $ = cheerio.load(body);
    var imgDiv = $('#img-wallpaper').children()['0'];
    if(err)
        console.err(err);
    if(typeof imgDiv !== 'undefined') {
    request(imgDiv.attribs.src).pipe(fs.createWriteStream(filename)).on('close', callback);}
    });
};



//Main Function
console.log("Downloading . . .");

// Loop function to create a recursive effect
(function loop(){
    download(baseURI+year+'/'+id+'/wallpaper/', savePath+id+'.jpg', 
    function(){
        console.log(((id/365)*100).toFixed(2)+'% completed');
        if(id == 330) 
            year = "2014";
        if(((id/365)*100) != 100){
            id=id+1;
            loop();}
        });
})(1)
2

There are 2 best solutions below

0
On

As @stdob said, The error was caused due to

  • Or a false condition typeof imgDiv !== 'undefined'

Though the answer is not the right way to overcome the error, it is more of an hack type. It ignores the error and continues the script!

if(typeof imgDiv !== 'undefined') {
    request(imgDiv.attribs.src).pipe(fs.createWriteStream(filename)).on('close', callback);
}
else{
    id++;
    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
}
1
On

Do I understand correctly that if you set the starting value for the id more than 35 (36?) the script is not downloaded any images?

Test the script on the fixed uri and on the fixed image by changing only the variables. Script is expected to work out? If this is the case:

  • Or not called callback for body request
  • Or a false condition typeof imgDiv !== 'undefined'
  • Or not called callback for request image

Thus, when an error in one of these points the script stops working. It is necessary to change the severity of conditions.