V
V
Vladislav Chernetsky2014-09-12 01:16:35
JavaScript
Vladislav Chernetsky, 2014-09-12 01:16:35

How to parse images via a NodeJs GET request?

I've been sitting for more than 9 hours, help me figure it out.

So everything works great, the problem is that I want to check if there is an image at this url or not? And it overwrites and I can't do it.

var request = require('request'),
    cheerio = require('cheerio'),
    sys = require('sys'),
    http = require('http'),
    token = require('token'),
    crypto = require('crypto'),
    net = require('net'),
    get = require('get'),
    Iconv  = require('iconv').Iconv;

for (var i = 0; i < 5; i++) { // неудачная попытка сделать цикл, хотя работает но request.get перезаписивает 
// переменную и происходит такая ситуация что реально проверяется только одна ссылка на картинку
    var token = crypto.randomBytes(16).toString('hex');  // генерация мд5 на угад парсить думаю
    var url = 'https://wu.wsiz.rzeszow.pl/wunet/photos/s' + token + '.jpg';
request.get({uri: url, method: 'GET', encoding: 'binary' }, function (err, res, body) {

    console.time(url);
    body = new Buffer(body, 'binary');
    var iconv = new Iconv('latin1', 'utf8//IGNORE');
    body = iconv.convert(body).toString();

    var $ = cheerio.load(body);


    var tmp = $('h1').html();
// Если есть h1 значит попало на ошибку и изображения тут нету, проверка ниже
    if (tmp==null)
        console.log('ok='+url+'##############################################');
       // Если заголовка нету, значит есть изображения
    else {
        console.log('fuck %)'+ token);
    }
    console.timeEnd(url);

});}


Normal image - https://wu.wsiz.rzeszow.pl/wunet/photos/se9191809b...
e9191809bf90e68444f1200592ee4c06 - a unique code of 32 characters. s is the salt, .jpg is the extension of the picture

How to make it so that it doesn't overwrite. I want to do this for myself. I wonder if nodejs can pull this off. Thanks for answers.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Timur Shemsedinov, 2014-09-12
@MarcusAurelius

Or read what a closure is or this code won't work. Or you can't declare functions inside a loop, or you can't write var inside a loop. Or you learn JavaScript here: learn.javascript.ru or node here: learn.javascript.ru/nodejs-screencast and then labs here: nodeschool.io

var request = require('request'),
    crypto = require('crypto');

var token, url;
for (var i = 0; i < 5; i++) {
  token = crypto.randomBytes(16).toString('hex');
  url = 'https://wu.wsiz.rzeszow.pl/wunet/photos/s' + token + '.jpg';
  doRequest(url);
}

function doRequest(url) {
    request.get(
    	{ uri:url, method:'GET', encoding:'binary' },
    	function (err, res, body) {
            console.time(url);
            body = new Buffer(body, 'binary');
            console.timeEnd(url);
        }
    );
}

_
_ _, 2014-09-12
@AMar4enko

It looks like you just run the loop to the end and the process ends. The node is asynchronous, control on request.get is not blocked. Read about async and promises.

V
Vladislav Chernetsky, 2014-09-12
@vcdesinger

Thank you very much! Tell me, how can the memory be freed, because with a large cycle - it gives an error that there is not enough memory ... Can I use this module?
https://github.com/nodejitsu/forever

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question