summaryrefslogtreecommitdiff
path: root/lib/videos.js
blob: 0f58523ce0259abf3d527c9f89ffa1f988544c68 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * mongo-edu
 *
 * Copyright (c) 2014-2016 Przemyslaw Pluta
 * Licensed under the MIT license.
 * https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
 */

var path = require('path'),
    fs = require('fs'),
    youtubedl = require('youtube-dl'),
    filesize = require('filesize'),
    ProgressBar = require('progress'),
    request = require('request'),
    progress = require('request-progress'),
    colors = require('colors'),
    extract = require('extract-zip'),
    rimraf = require('rimraf'),
    mv = require('mv'),
    _ = require('lodash');

var isDebug = /[debug]/, downloadPath = '', proxy = '', downloadList = [], hash = {},
    co = false, ncc = false, handout = false, cc = false, uz = false, hq = false, verbose = false;

function setOptions(argv) {

    'use strict';

    downloadPath = argv.d;
    proxy = argv.proxy;
    if (argv.ncc) { ncc = true; }
    if (argv.h) { handout = true; }
    if (argv.cc) { cc = true; }
    if (argv.uz) { uz = true; }
    if (argv.hq) { hq = true; }
    if (argv.co) { co = true; }
    if (argv.verbose) { verbose = true; }
}

function rename(downloadPath, item, id, count, pass) {

    'use strict';

    var tag = (id + 1);

    if (tag < 10) { tag = '00' + tag; }
    if (tag >= 10 && tag < 100) { tag = '0' + tag; }

    mv(downloadPath + item, downloadPath + tag + '_' + item, function move() {
        if (cc && !pass) {
            var subtitle = path.basename(item, path.extname(item)) + '.en.srt';
            return rename(downloadPath, subtitle, id, count, true);
        }
        if (count === 0) { console.log('[ Finished ]'.green); }
    });
}

var handleList = function handleList(list, tags) {

    'use strict';

    var currentList = list,
        quality = (!hq) ? '18' : '22',
        opt = (!ncc) ? ['--format=' + quality] : ['--format=' + quality, '--no-check-certificate'], i, count;

    if (verbose) { opt = opt.concat(['--verbose']); }

    if (proxy) { opt = opt.concat(['--proxy', proxy]); }

    var getHandouts = function getHandouts(item) {

        var name = path.basename(item), bar, dounloadFile, dlh, left, extname, unzipFile, progressSoFar = -1, hold = 0,

        downloadItem = function downloadItem() {

            dlh = progress(request(item), { throttle: 0 });

            console.log('i'.magenta + ' Downloading: ' + name.cyan);

            dlh.on('progress', function(state) {
                if (!bar) { bar = new ProgressBar('>'.green + ' ' + filesize(state.total) + ' [:bar] :percent :etas', { complete: '=', incomplete: ' ', width: 20, total: 100 }); }
                if (!bar.complete && progressSoFar !== state.percent) {
                    var i;
                    for (i = 0; i < (state.percent - hold); i++) { bar.tick(); }
                    hold = state.percent;
                }
                progressSoFar = state.percent;
            });

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

            dounloadFile = dlh.pipe(fs.createWriteStream(downloadPath + name));

            dounloadFile.on('close', function close() {

                extname = path.extname(name);

                if (!uz || (extname !== '.zip')) {
                    console.log('i'.green + ' Done.' + left);
                    return nextItem(currentList);
                }

                unzipFile = extract(downloadPath + name, {dir: downloadPath + path.basename(name, extname)}, function out(err) {
                    if (err) {
                        console.log('i'.red + ' Unable to unzip ' + name.red + ' try manually.');
                        return rimraf(downloadPath + name.replace('.zip', ''), function rf() { nextItem(currentList); });
                    }
                    console.log('i'.green + ' Done.' + left);
                    cleanup(downloadPath + name, currentList);
                });

            });

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

        cleanup = function cleanup(target, next) {
            fs.unlink(target, function (err) {
                if (err !== null) { console.log(err.stack); }
                nextItem(next);
            });
        },

        nextItem = function nextItem(next) {
            handleList(next);
        };

        fs.exists(downloadPath + name, function (exists) {
            left = (currentList.length)? ' ' + currentList.length + ' left ...' : '';

            if (exists) {
                console.log('>'.magenta + ' ' + name + ' has already been downloaded.' + left);
                return handleList(currentList);
            }

            downloadItem();

        });

    },

    getSubtitles = function getSubtitles(item, left, currentList, tags) {
        if (!cc) {
            if (left) { console.log('i'.magenta + ' ' + left); }
            return handleList(currentList, tags);
        }

        youtubedl.getSubs(item, { auto: true, all: false, lang: 'en', cwd: downloadPath }, function getSubs(err) {
            if (err !== null) {
                console.log('i'.red + ' Unable to download subtitles.');
            } else {
                console.log('i'.magenta + ' Subtitles downloaded.');
            }
            if (left) { console.log('i'.magenta + ' ' + left); }
            handleList(currentList, tags);
        });
    },

    getVideos = function getVideos(item, nocc, notAvailable) {

        if (handout) { return getHandouts(item); }

        var downloaded = 0, size = 0, stash = {}, bar;

        if (fs.existsSync(downloadPath + hash[item])) {
            downloaded = fs.statSync(downloadPath + hash[item]).size;
        }

        var dl = youtubedl(item, nocc || opt, {start: downloaded, cwd: downloadPath});

        dl.on('info', function(info) {
            size = info.size + downloaded;
            stash = info;
            if (co) { downloadList.push({id: item, name: path.basename(info._filename)}); }
            if (notAvailable) { console.log('i'.magenta + ' No HQ video available for ' + info.fulltitle.white.bold + ' trying default quality ...'); }
            console.log('i'.magenta + ((downloaded > 0) ? ' Resuming download: ' : ' Downloading: ') + info._filename.cyan + ' > ' + item);
            bar = new ProgressBar('>'.green + ' ' + filesize(info.size) + ' [:bar] :percent :etas', { complete: '=', incomplete: ' ', width: 20, total: parseInt(info.size, 10) });
            console.time('i'.magenta + ' ' + info._filename + '. Done in');
            dl.pipe(fs.createWriteStream(downloadPath + info._filename, {flags: 'a'}));
        });

        dl.on('data', function(data) {
            if (!bar.complete) { bar.tick(data.length); }
        });

        dl.on('error', function error(err) {
            if (err.message.indexOf('requested format not available') !== -1) {
                _.pull(opt, '--format=22');
                opt.push('--format=18');
                return getVideos(item, nocc, true);
            }
            console.log(err.stack);
        });

        dl.on('close', function close() {
            dl.emit('end');
        });

        dl.on('end', function end() {
            var left = (currentList.length)? currentList.length + ' left ...' : '';
            console.timeEnd('i'.magenta + ' ' + stash._filename + '. Done in');
            getSubtitles(item, left, currentList, tags);
        });
    };

    if (currentList.length) {
        return getVideos(currentList.shift());
    }

    if (co) {

        count = downloadList.length;

        for (i = 0; i < downloadList.length; i++) {
            count = count -1;
            rename(downloadPath, downloadList[i].name, tags[downloadList[i].id], count);
        }

    } else {
        console.log('[ Finished ]'.green);
        process.exit(0);
    }

};

module.exports = {

    details: function details(opt, argv, callback) {

        'use strict';

        setOptions(argv);

        var bar = new ProgressBar('>'.magenta + ' Collecting [:bar] :percent', { complete: '=', incomplete: ' ', width: 20, total: opt.length }),
            options = (!ncc) ? [] : ['--no-check-certificate'];

        if (verbose) { options = options.concat(['--verbose']); }

        if (proxy) { options = options.concat(['--proxy', proxy]); }

        var isFinished = function isFinished(count, items) {
            if (count === 0) {

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

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

        getDetails = function getDetails(item, i) {

            function getInfo() {

                if (handout) {
                    items.push({name: path.basename(item), value: item, id: i});
                    count = count - 1;
                    bar.tick();
                    return isFinished(count, items);
                }

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

                    if (verbose && isDebug.test(err)) { console.log(err); }

                    hash[item] = info._filename;

                    items.push((!err)?{name: info.fulltitle + ' - ' + info.width + 'x' + info.height, value: item, id: i}:{name: 'No info: ' + item, value: item, id: i});

                    count = count - 1;

                    bar.tick();

                    isFinished(count, items);

                });
            }

            setTimeout(function timeout(){ getInfo(); }, 200 * 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) {

        'use strict';

        setOptions(argv);

        var i, options = opt.videos, fullList = [], selected = [], tags = {};

        for (i = 0; i < list.length; i++) {
            tags[list[i].value] = list[i].id;
        }

        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, tags);
        }

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

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

    },

    listVideosFromPlaylist: function getIdsFromPlaylist(opt, argv, callback) {

        'use strict';

        setOptions(argv);

        var options = (!ncc) ? [] : ['--no-check-certificate'], items = [], i, item;

        if (verbose) { options = options.concat(['--verbose']); }

        if (proxy) { options = options.concat(['--proxy', proxy]); }

        youtubedl.getInfo(opt.url, options, function(err, info) {

            if (verbose && isDebug.test(err)) { console.log(err); }

            if (info.length) {
                for (i = 0; i < info.length; i++) {
                    item = 'https://www.youtube.com/watch?v=' + info[i].id;
                    items.push((!err)?{name: info.fulltitle + ' - ' + info.width + 'x' + info.height, value: item, id: i}:{name: 'No info: ' + item, value: item, id: i});
                }

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

            callback(null, items, true);

        });

    },

    checkProxyDownload: function checkProxyDownload(argv) {

        'use strict';

        setOptions(argv);
        var video = 'https://youtu.be/nm20j_x9Ol8', opt = { all: undefined, cancel: undefined };
        if (typeof argv.test === 'string') { video = argv.test; }
        opt[video] = 0;
        handleList([video], opt);
    }
};