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)
As @stdob said, The error was caused due to
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!