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
|
/*
* mongo-edu
*
* Copyright (c) 2014 Przemyslaw Pluta
* Licensed under the MIT license.
* https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
*/
var fs = require('fs'),
request = require('request'),
cheerio = require('cheerio'),
ProgressBar = require('progress'),
inquirer = require('inquirer'),
colors = require('colors'),
_ = require('lodash');
var isWin = /^win/.test(process.platform),
camo = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36' };
function saveData(argv, details, data, callback) {
'use strict';
details = details.map(function items(item) { return item.replace(/[^a-z0-9]/gi, '_').replace(/(_){2,}/g, '_'); });
fs.writeFile(argv.d + details[0] + '-' + details[1] + '.txt', data.join((!isWin) ? '\n' : '\r\n'), function write(err) {
if (err !== null) { return console.log(err.stack); }
console.log('>'.magenta + ' Video list generated with ' + data.length + ' item' + ((data.length > 1)? 's.' : '.'));
inquirer.prompt({type: 'confirm', name: 'download', message: 'Select and download?'}, function prompt(answers) {
if (answers.download) { return callback(null, data); }
console.log('[ Finished ]'.green);
process.exit(0);
});
});
}
function listVideos(url, jar, id, callback) {
'use strict';
request({ url: url, jar: jar, headers: camo }, function get(err, res, body) {
if (err !== null) { return callback(null, []); }
if (res.statusCode === 200) {
var $ = cheerio.load(body),
pageView = $('section.course-content').html(),
getVideoIds = (pageView) ? pageView.match(/(.0:)(.*?)(&)/g) : pageView;
getVideoIds = (!getVideoIds) ? [] : getVideoIds.map(function map(item) { return { id: id, video: 'https://youtu.be/' + item.replace('.0:','').replace('&','')}; });
callback(null, getVideoIds);
}
});
}
function getDetails(jar, count, bar, head, argv, callback) {
'use strict';
var list = [], out = [];
return function filterDetail(url, i) {
function getInfo() {
listVideos(url, jar, i, function details(err, items) {
count = count - 1;
list.push(items);
bar.tick();
if (count === 0) {
out = _.map(_.sortBy(_.flatten(list), 'id'), function map(item) { return item.video; });
if (argv.cwd) {
saveData(argv, head, out, callback);
} else {
callback(null, out);
}
}
});
}
setTimeout(function timeout() { getInfo(); }, 50 * i);
};
}
module.exports = {
findDetails: function findDetails($, callback) {
'use strict';
var chapter = $('.chapter'), master = $('div.inner-wrapper h1').text(), courses = [];
$(chapter).each(function each(i, item) {
var current = $(item),
title = current.find('h3 a').text(),
list = current.find('ul'),
stack = [];
$(list).children().each(function each(i, item) {
var link = $(item).find('a').attr('href');
stack[i] = link;
});
courses.push({ name: title, value: { master: [ master, title ], stack: stack } });
});
callback(null, courses);
},
filterVideos: function filterVideos(opt, url, jar, argv, callback) {
'use strict';
if (!opt.url.stack) { return callback(null, []); }
var bar = new ProgressBar('>'.magenta + ' Searching Courseware [:bar] :percent', { complete: '=', incomplete: ' ', width: 20, total: opt.url.stack.length }),
getPage = getDetails(jar, opt.url.stack.length, bar, opt.url.master, argv, callback), i;
for (i = 0; i < opt.url.stack.length; i++) {
getPage(url + opt.url.stack[i], i);
}
}
};
|