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
|
/*
* mongo-edu
*
* Copyright (c) 2014 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'),
_ = require('lodash');
var downloadPath = '', ncc = false, handout = false;
var handleList = function handleList(list) {
var currentList = list,
opt = (!ncc) ? ['--max-quality=18'] : ['--max-quality=18', '--no-check-certificate'],
getHandouts = function getHandouts(item) {
var name = path.basename(item), bar, dounloadFile, dlh, left,
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.0 });
}
if (!bar.complete) { bar.tick(parseInt(state.percent, 10)); }
});
dlh.on('error', function error(err) {
return console.log(err.stack);
});
dounloadFile = dlh.pipe(fs.createWriteStream(downloadPath + name));
dounloadFile.on('close', function close() {
console.log('[' + 'i'.green + '] Done.' + left);
handleList(currentList);
});
dounloadFile.on('error', function error(err) {
return console.log(err.stack);
});
};
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();
});
},
getVideos = function getVideos(item) {
if (handout) { return getHandouts(item); }
var dl = youtubedl.download(item, downloadPath, opt), bar;
dl.on('download', function download(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 progress(data) {
if (!bar.complete) { bar.tick(parseInt(data.percent, 10)); }
});
dl.on('error', function error(err) {
return console.log(err.stack);
});
dl.on('end', function end(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, argv, callback) {
downloadPath = argv.d;
if (argv.ncc) { ncc = true; }
if (argv.h) { handout = true; }
var bar = new ProgressBar('[' + '>'.magenta + '] Collecting [:bar] :percent', { complete: '=', incomplete: ' ', width: 20, total: opt.length }),
options = (!ncc) ? [] : ['--no-check-certificate'];
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) {
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();
isFinished(count, items);
});
}
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) {
var options = opt.videos, fullList = [], selected = [];
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); }
}
}
};
|