2020-06-17 18:41:28 +02:00
|
|
|
const fs = require('fs');
|
2020-07-11 01:23:58 +02:00
|
|
|
const events = require('events');
|
2020-06-17 18:41:28 +02:00
|
|
|
const youtubedl = require('youtube-dl');
|
2020-07-16 09:22:54 +02:00
|
|
|
// Download submitted video
|
2020-06-17 18:41:28 +02:00
|
|
|
module.exports = function (url, option, output) {
|
2020-07-11 01:23:58 +02:00
|
|
|
let eventEmitter = new events.EventEmitter();
|
2020-06-17 18:41:28 +02:00
|
|
|
|
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-06-17 18:41:28 +02:00
|
|
|
|
2020-07-11 01:23:58 +02:00
|
|
|
if (option != null) option.push('--rm-cache-dir');
|
|
|
|
else option = ['--rm-cache-dir'];
|
2020-06-17 18:41:28 +02:00
|
|
|
|
2020-07-11 01:23:58 +02:00
|
|
|
const video = youtubedl(url, option);
|
2020-06-17 18:41:28 +02:00
|
|
|
|
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-06-17 18:41:28 +02:00
|
|
|
|
2020-07-11 01:23:58 +02:00
|
|
|
video.on('end', function() {
|
|
|
|
eventEmitter.emit('end', output);
|
2020-06-17 18:41:28 +02:00
|
|
|
});
|
2020-07-16 09:22:54 +02:00
|
|
|
|
2020-07-11 01:23:58 +02:00
|
|
|
return eventEmitter;
|
2020-06-17 18:41:28 +02:00
|
|
|
};
|
|
|
|
|