summaryrefslogtreecommitdiff
path: root/lib/videos.js
blob: 44bfd7e7dd2c1bf9a67796496cc090c6ced158ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * mongo-edu
 *
 * Copyright (c) 2014 Przemyslaw Pluta
 * Licensed under the MIT license.
 * https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
 */

var youtubedl = require('youtube-dl'),
    ProgressBar = require('progress'),
    colors = require('colors'),
    _ = require('lodash');

var downloadPath = '';

var handleList = function handleList(list) {

    var currentList = list,

    getVideos = function getVideos(item) {

        var dl = youtubedl.download(item, downloadPath, ['--max-quality=18']), bar;

        dl.on('download', function(data) {
            console.log('[' + 'i'.magenta + '] Downloading: ' + data.filename.cyan + ' > ' + item);
            bar = new ProgressBar('[' + '>'.green + '] ' + data.size + ' [:bar] :percent :etas', { complete: '=', incomplete: ' ', width: 20, total: 100.0 });
        });

        dl.on('progress', function(data) {
            if (!bar.complete) { bar.tick(parseInt(data.percent, 10)); }
        });

        dl.on('error', function(err) {
            return console.log(err.stack);
        });

        dl.on('end', function(data) {
            var left = (currentList.length)? ' ' + currentList.length + ' left ...' : '';
            if (data.filename) {
                if (data.filename.indexOf('has already been downloaded') !== -1) {
                    console.log('[' + '>'.magenta + '] ' + data.filename + '.' + left);
                } else {
                    console.log('[' + 'i'.green + '] Done in ' + data.timeTakenms + 'ms.' + left);

                }
            } else {
                console.log('[' + 'i'.red + '] Download Issues');
            }
            handleList(currentList);
        });
    };

    if (currentList.length) { return getVideos(currentList.shift()); }
    console.log('[ Finished ]'.green);

};

module.exports = {

    details: function details(opt, callback) {

        var bar = new ProgressBar('[' + '>'.magenta + '] Collecting [:bar] :percent', { complete: '=', incomplete: ' ', width: 20, total: opt.length });

        var getDetails = function getDetails(item, i) {

            function getInfo() {
                youtubedl.getInfo(item, function(err, info) {

                    items.push((!err)?{name: info.title + ' - ' + info.resolution, value: item, id: i}:{name: 'No info: ' + item, value: item, id: i});

                    count = count - 1;

                    bar.tick();

                    if (count === 0) {

                        var sortedList = _.map(_.sortBy(items, 'id'));

                        sortedList.unshift({name: 'All', value: 'all', checked: true}, {name: 'Cancel', value: 'cancel'});
                        callback(null, sortedList);
                    }
                });
            }

            setTimeout(function timeout(){ getInfo(); }, 100 * i);

        }, items = [], list = [], i, count;

        list = _.filter(opt, function filter(item) { return (item.indexOf('http://') !== -1) || (item.indexOf('https://') !== -1); });

        if (!list.length) { return callback(null, list); }

        count = list.length;

        for (i = 0; i < list.length; i++) {
            getDetails(list[i], i);
        }
    },

    download: function download(opt, list, argv) {

        var options = opt.videos, fullList = [], selected = [];

        downloadPath = argv.d;

        if (options.length === 1 && options[0] === 'cancel') { return console.log('Cancel'); }

        if (options.length === 1 && options[0] === 'all') {
            fullList = _.pull(_.map(list, function map(item) { return item.value; }), 'all', 'cancel');
            return handleList(fullList);
        }

        if (options.length >= 1) {
            selected = _.pull(options, 'all', 'cancel');

            if (selected.length) { return handleList(selected); }
        }

    }
};