summaryrefslogtreecommitdiff
path: root/mongo-edu.js
blob: b446f8e95427414da89b49ddb4bc63f08ed2b1db (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
/*
 * mongo-edu
 *
 * Copyright (c) 2014 Przemyslaw Pluta
 * Licensed under the MIT license.
 * https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
 */

var mdbvideos = require('./lib/login'),
    videoHandler = require('./lib/videos'),
    validate = require('./lib/validate'),
    configure = require('./lib/configure'),
    url = require('url'),
    path = require('path'),
    colors = require('colors'),
    inquirer = require('inquirer'),
    yargs = require('yargs')
        .usage('Usage: $0 [options]')
        .describe('d', 'download path').describe('u', 'email address')
        .describe('h', 'switch from videos (default) to handouts').boolean('h')
        .describe('py', 'py switch').describe('py', 'switch to point to Python')
        .describe('proxy', 'pass proxy').describe('proxy', 'pass proxy switch for video download')
        .describe('test', 'proxy test').describe('test', 'use with --proxy to test if usable')
        .describe('cw', 'switch from wiki\'s video lists (default) to courseware').boolean('cw')
        .describe('cwd', 'same as --cw and dumps list of videos to file in -d').boolean('cwd')
        .describe('cc', 'get closed captions').boolean('cc')
        .describe('hq', 'get high quality videos').boolean('hq')
        .describe('ncc', 'no check certificate').boolean('ncc')
        .describe('uz', 'unzip handout files').boolean('uz')
        .describe('co', 'sequence video files in order of the courseware').boolean('co')
        .describe('verbose', 'print debug information').boolean('verbose')
        .example('$0 -d your_download_path', 'download videos from wiki')
        .example('$0 -d your_download_path -u your_user_name --cw --hq --cc', 'download high quality videos from courseware with closed caltions')
        .example('$0 -d your_download_path -h --uz', 'download and unzip handouts')
        .example('$0 -d your_download_path --cw --verbose', 'download videos from courseware and print debug info')
        .example('$0 -d your_download_path --cw --proxy http://proxy_ip_address:proxy_port_number', 'download videos from courseware via proxy tunnel')
        .example('$0 -d your_download_path --proxy http://proxy_ip_address:proxy_port_number --test', 'test proxy and download video via proxy tunnel')
        .demand('d');

exports.create = function start() {

    'use strict';

    process.title = 'mongo-edu';

    var argv = yargs.argv, proxyDetails = {}, lookFor = ((!argv.h)? 'Videos' : 'Handouts'), isWin = /^win/.test(process.platform), slash = (isWin) ? '\\' : '/';

    if (argv.help) { return yargs.showHelp(); }

    argv.d = path.normalize(argv.d);

    if (argv.d.substr(-1) !== slash) { argv.d += slash; }

    validate.init(argv, function init(err, profile) {
        if (err !== null) { throw err; }

        configure(argv, function conf(err) {
            if (err !== null) { throw err; }

            if (!argv.proxy) { return run(profile); }

            proxyDetails = url.parse(argv.proxy);

            console.log('i'.magenta + ' Proxy Host: '.bold + proxyDetails.hostname.cyan + ' Port: '.bold + proxyDetails.port.cyan + ' Protocol: '.bold + proxyDetails.protocol.replace(':', '').toUpperCase().cyan);

            mdbvideos.checkProxy(argv.proxy, function get(err, data) {
                if (err !== null) {
                    (argv.verbose) ? console.log('i'.red + ' Proxy Error: '.red + err.stack) : console.log('i'.red + ' Proxy Might By Unusable.'.red);
                }

                if (data) { console.log('i'.magenta + ' ' + data); }
                if (argv.test) { return videoHandler.checkProxyDownload(argv); }

                run(profile);
            });

        });

    });

    function run(profile) {

        inquirer.prompt(profile, function prompt(answers) {

            var list = [{ type: 'list', name: 'url', message: '', choices: [] }], classes = list,

                check = [{ type: 'checkbox', message: '', name: 'videos', choices: [],
                    validate: function validate(answer) {
                        if ( answer.length < 1 ) { return 'You must choose at least one option.'; }
                        return true;
                    }
                }];

            mdbvideos.init(answers, argv, function get(err, data) {
                if (err !== null) { throw err; }

                if (data.length) {

                    classes[0].message = 'Found ' + data.length + ' Course'+ ((data.length > 1)? 's' : '') + '. Select:';
                    classes[0].choices = data;
                    return currentList();

                }

            });

            function currentList() {
                inquirer.prompt(classes, function prompt(answers) {

                    mdbvideos.getList(answers, argv, function get(err, data, pass) {
                        if (err !== null) { throw err; }

                        if (data.length) {

                            if (pass) { return showDetails(err, data); }

                            list[0].message = 'Found ' + data.length + ' List' + ((data.length > 1)? 's' : '') + '. Select:';
                            list[0].choices = data;

                            return currentVideos();

                        } else {
                            if (pass) { return console.log('i'.red + ' Looks like the course is not yet available or has already ended. ' +
                                lookFor + ' list is not available.\n\nCheck the start/end date for selected course.\n'); }
                        }

                        return console.log('i'.red + ' Unable to locate any ' + lookFor.toLowerCase() + ' lists in the wiki. Are ' +
                            lookFor.toLowerCase() + ' list present in the wiki?' +
                            ((lookFor === 'Videos') ? ' Try to add ' + '--cw'.green + ' to switch and search on courseware instead.' : ''));
                    });

                });
            }

            function currentVideos() {

                inquirer.prompt(list, function prompt(answers) {

                    mdbvideos.listVideos(answers, argv, function get(err, data, pass) {
                        if (err !== null) { throw err; }
                        if (!pass) { return videoHandler.details(data, argv, showDetails); }
                        showDetails(err, data);
                    });

                });
            }

            function showDetails(err, data) {
                if (err !== null) { throw err; }

                if (data.length) {
                    check[0].message = 'Select From ' + (data.length - 2) + ' ' + lookFor + '. Download:';
                    check[0].choices = data;

                    return inquirer.prompt(check, function prompt(answers) {
                        videoHandler.download(answers, data, argv);
                    });

                }

                console.log('i'.red + ' Could not locate any ' + lookFor.toLowerCase() + '.'); process.exit(0);
            }

        });

    }

};