javascript - Node.js Recursive Loop fail -
below node.js script. downloads images contained in div. loop works fine 9.86% upto id = 36. when id > 36 exits loop. using node 0.12 version. loop needs run 365 times before completion. usign 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 create 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)
do understand correctly if set starting value id
more 35 (36?) script not downloaded images?
test script on fixed uri , on fixed image changing variables. script expected work out? if case:
- or not called callback
body request
- or false condition
typeof imgdiv !== 'undefined'
- or not called callback
request image
thus, when error in 1 of these points script stops working. necessary change severity of conditions.
Comments
Post a Comment