Haha-Yes/utils/download.js

30 lines
826 B
JavaScript
Raw Normal View History

const fs = require('fs');
2020-07-11 01:23:58 +02:00
const events = require('events');
const youtubedl = require('youtube-dl');
2020-07-16 09:22:54 +02:00
// Download submitted video
module.exports = function (url, option, output) {
2020-07-11 01:23:58 +02:00
let eventEmitter = new events.EventEmitter();
2020-07-11 01:23:58 +02:00
if (!url) eventEmitter.emit('error', 'Require an url!');
if (!output) eventEmitter.emit('error', 'Require an output parameter! (If you see this message please send a feedback to the dev about it.)\'');
2020-07-11 01:23:58 +02:00
if (option != null) option.push('--rm-cache-dir');
else option = ['--rm-cache-dir'];
2020-07-11 01:23:58 +02:00
const video = youtubedl(url, option);
2020-07-11 01:23:58 +02:00
video.on('error', function error(err) {
console.log(err.toString());
eventEmitter.emit('error', err.toString());
});
video.pipe(fs.createWriteStream(output));
2020-07-11 01:23:58 +02:00
video.on('end', function() {
eventEmitter.emit('end', output);
});
2020-07-16 09:22:54 +02:00
2020-07-11 01:23:58 +02:00
return eventEmitter;
};