var http = require('http');
var fs = require('fs');
function nowDate(date) {
var date = new Date();
var dd = date.getDate();
if (dd < 10) dd = '0' + dd;
var mm = date.getMonth() + 1;
if (mm < 10) mm = '0' + mm;
var yy = date.getFullYear();
return yy + '-' + mm + '-' + dd;
}
var uploadDir = "C:\/Node\/";
var files = [
808707+1
];
files.forEach( function (fileName) {
var file = fs.createWriteStream(uploadDir + fileName+'.jpeg');
var request = http.get("http://example/images/"+nowDate()+'-'+fileName+'.jpeg', function (response) {
response.pipe(file);
});
})
var path = require('path');
var request = require('request');
var http = require('http');
var fs = require('fs');
var uploadDir = 'C:\/Node\/';
var urlPrefix = 'http://e-shuushuu.net/images/';
var regExpImageURLWrapper = new RegExp('(?:<img src="\/images\/thumbs\/)(.*?)"', 'gim');
var pageAmount = 2;
function getCurrentImage(imageUrl)
{
var fileName = path.join(uploadDir, imageUrl);
var newRequest = request(urlPrefix + imageUrl);
newRequest.pause();
newRequest.on('response', function(resp)
{
if((resp.statusCode === 200) && (resp.headers['content-type'].match('image')))
{
newRequest.pipe(fs.createWriteStream(fileName));
newRequest.resume();
console.log(`Загружаем: ${imageUrl}`);
}
else
{
var message =
'\n********************************************************\n' +
'Ошибка загрузки файла ' + urlPrefix + imageUrl + '\n' +
'Код ошибки: ' + resp.statusCode + ' ' + resp.statusMessage + '; ' +
'Mime-type: ' + resp.headers['content-type'] + '\n' +
'********************************************************\n';
console.log(`${message}`);
newRequest.end();
}
});
newRequest.on('end', function()
{
console.log(`Загрузка завершена: ${imageUrl}`);
});
}
for(var page_counter = 1; page_counter < pageAmount; page_counter++)
{
var newRequest = request
(
'http://e-shuushuu.net/?page=' + page_counter,
function (error, response, body)
{
if (!error && response.statusCode == 200)
{
while(match = regExpImageURLWrapper.exec(body))
{
getCurrentImage(match[1]);
// break;
}
}
}
);
}