0

Im using ytdl-core to get the direct urls for youtube videos (the long urls that start with https://r1-- etc...), and using https.get to download them. ~70% of the time when res.end is called, it returns an empty buffer instead of the data for the video.

this is the code that im currently using to handle the http request:

function largeHttpGet(url, callback, progress) {

    try{

        var _current = 0;
        var _buffer = [];

        var _req = https.get(url, function (res) {

            var _total = parseInt(res.headers['content-length'], 10);

            res.on('data', function (chunk) {
                _buffer.push(chunk);


                _current = parseFloat(_current + chunk.length);

                progress(_current / _total * 100);
            })

            res.on('end', function () {
                callback(Buffer.concat(_buffer));
                //this returns an empty buffer the majority of the time
            })

        })

    }
    catch(error){

        console.log("[HTTP]: " + error);

    }

}

and this is what i use to get the youtube video's info:

getVidInfo(link, function(info){

    back.send("return", {"key": key, "data": info} );

})

This error only occurs with the youtube video links, all other requests work consistently.

KCGD
  • 675
  • 6
  • 10

2 Answers2

0

It may be due to u sing on('end'... instead of `on('finish'...)

As there may still be additional work that has not been completed yet.

Manny
  • 369
  • 2
  • 7
0

Found the issue, when the request failed it was because youtube returned a redirect code (302) instead of a direct link, following the redirection lead to the actual url, which downloaded without issue.

KCGD
  • 675
  • 6
  • 10