Answer the question
In order to leave comments, you need to log in
How to download images from the site as they arrive?
There is one board with images that are generated on it in the following format: 2016-02-08-808706.jpeg(png or jpg) (for example). and each added image changes the link by +1 to the current one: 2016-02-08-808706(+1).jpeg(png or jpg). The following code only downloads images whose numbers are specified in the files variable.
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);
});
})
Answer the question
In order to leave comments, you need to log in
After filing
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;
}
}
}
);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question