Tuesday 30 May 2017

node-ytdl-core promise audio title and file download

I'm relatively new to promises, and i'm having trouble with two things while using node-ytdl-core.

 exports.downloadMP3 = function(bot, msg, sSuffix) {
    return Promise
    .all([
        ytdl(sSuffix, {filter : 'audioonly',}).pipe(fs.createWriteStream('audio.mp3')),
        promiseTitle(sSuffix)
    ])
    .then(function(results) {msg.channel.sendFile('audio.mp3', results[1], '', '')})
    .catch(error => console.error(error));
}

function promiseTitle(sSuffix) {
    return new Promise(function (resolve, reject) {
        ytdl.getInfo(sSuffix, function(err, info) {
            if (err) reject(err);
            console.log(info.title);
            return resolve(info.title);
        });
    });
}

When I use the above code, the returned title is a string along the lines of a25998454d48491.V, though the console logs the correct title. Meaning the promise doesn't return my desired value.

Furthermore the createWriteStream doesn't get resolved before the file gets sent (sendFile) meaning i get a 0byte file, though after a bit the file is done downloading and has the correct size/content in the filesystem.

My question therefore is how do I return the correct value in my promise, and turn createWriteStream into a promise?

Thanks in advance.



via Novak

No comments:

Post a Comment