var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
var fs = require('fs');
var fsp = require('fs-promise');
var ProgressBar = require('progress');
var request = request.defaults({jar: true})
var credentials = {
username: 'login',
password: 'pass'
};
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
if ('content-type:' != 'text/html'){
var req = request.get({url: uri, encoding: 'binary'}, function (err, response, body) {
//console.log('content-type:', res.headers['content-type']);
//console.log('content-length:', res.headers['content-length']);
fs.writeFile(filename, body, 'binary', function(err) {
if(err){
console.log(err);
}
else {
console.log(filename + ' saved');
}
})
}).on('close', callback);
}
});
};
request.post({
uri: 'https://mysite.com/login/',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
body: require('querystring').stringify(credentials),
headers: {'user-agent': 'Mozilla/5.0'}
}, function(err, res, body){
if(err) {
callback.call(null, new Error(' Login failed'));
return;
}
for(var i = 1; i < 50; i++) {
(function(i) {
setTimeout(function(){
request('https://mysite.com' + i + '/', function(err, res, body) {
if(err) {
callback.call(null, new Error('Request failed'));
return;
}
var $ = cheerio.load(body);
//var arr = "a,b,c".split(',')
var pages = $('.paginator_pages').text().split(' ');
//console.log(body);
var img = $('.p_body', '.post').each(function(task, next){
var link = $(this).find('img');
var href = link.attr("src");
if (typeof href !== "undefined") {
var name_arr = href.split('/');
var name = name_arr[name_arr.length - 1]
var dir = 'img/' + i + '/';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
download(href, dir + name, function(){
console.log(name + ' saved');;
});
}
});
//fs.writeFile
//console.log(i);
});
}, 5000 / i);
})(i);
}
});
что я сделал не так?