summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorprzemyslawpluta <przemekpluta@hotmail.com>2014-04-06 19:37:29 +0100
committerprzemyslawpluta <przemekpluta@hotmail.com>2014-04-06 19:37:29 +0100
commit11b317833a8d5a332d8cba564edbcd68cf7f9a7a (patch)
tree7529d9a469514db79053ea88f77a77d82f8f87a6 /lib
parentaa5184821120aac47547cf1745c350d1efa51c8f (diff)
downloadmongo-edu-11b317833a8d5a332d8cba564edbcd68cf7f9a7a.zip
handle youtube playlists
Diffstat (limited to 'lib')
-rw-r--r--lib/login.js15
-rw-r--r--lib/videos.js31
2 files changed, 41 insertions, 5 deletions
diff --git a/lib/login.js b/lib/login.js
index 12de9e4..6804031 100644
--- a/lib/login.js
+++ b/lib/login.js
@@ -9,7 +9,8 @@
var request = require('request'),
cheerio = require('cheerio'),
ProgressBar = require('progress'),
- _ = require('lodash');
+ _ = require('lodash'),
+ videoHandler = require('./videos');
var jar = request.jar(), host = 'https://university.mongodb.com';
@@ -106,19 +107,19 @@ module.exports = {
if (err !== null) { return callback(err, null); }
if (res.statusCode === 200) {
- var list = [], getCourses = [], $ = cheerio.load(body), tag = opt.url.replace('course_', ''), options = (!argv.h)? 'Video' : 'Handout';
+ var list = [], getCourses = [], $ = cheerio.load(body), tag = opt.url.replace('course_', ''), options = (!argv.h)? 'Video|playlist' : 'Handout';
if (!argv.h) {
$('div.wiki-article p').children().filter('a').map(function map(i, item) {
var current = $(item);
- if (current.text().indexOf(options) !== -1) {
+ if (current.text().match(options)) {
list.push({ href: current.attr('href'), text: current.text() });
}
});
getCourses = _.filter(list, function map(item) {
- if (item.href.match(/(wiki\/M101|wiki\/M102|wiki\/C100|wiki\/M202|wiki\/list-youtube-links)/)) { return item; }
+ if (item.href.match(/(wiki\/M101|wiki\/M102|wiki\/C100|wiki\/M202|wiki\/list-youtube-links|playlist\?list|view_play_list)/)) { return item; }
});
return callback(null, _.map(getCourses, function map(item) { return { name: item.text, value: item.href }; }));
@@ -144,10 +145,14 @@ module.exports = {
},
- listVideos: function listVideos(opt, callback) {
+ listVideos: function listVideos(opt, argv, callback) {
var videos = [], state = false, list, $, current;
+ if (opt.url.match(/playlist\?list|view_play_list/)) {
+ return videoHandler.listVideosFromPlaylist(opt, argv, callback);
+ }
+
request({ url: opt.url, jar: jar }, function get(err, res, body) {
if (err !== null) { return callback(err, null); }
if (res.statusCode === 200) {
diff --git a/lib/videos.js b/lib/videos.js
index cb07cf8..017787a 100644
--- a/lib/videos.js
+++ b/lib/videos.js
@@ -14,6 +14,7 @@ var path = require('path'),
request = require('request'),
progress = require('request-progress'),
colors = require('colors'),
+ execFile = require('child_process').execFile,
_ = require('lodash');
var downloadPath = '', ncc = false, handout = false, cc = false;
@@ -196,5 +197,35 @@ module.exports = {
if (selected.length) { return handleList(selected); }
}
+ },
+
+ listVideosFromPlaylist: function getIdsFromPlaylist(opt, argv, callback) {
+
+ var isWin = /^win/.test(process.platform),
+ file = path.join(__dirname, '..', 'node_modules/youtube-dl/bin', 'youtube-dl'),
+ args = [opt.url, '--get-id', '--get-title'],
+ options = [file, (!argv.ncc) ? args : args.concat(['--no-check-certificate']), ''];
+
+ if (isWin) { options = ['python', [file].concat(args), '\r']; }
+
+ execFile(options[0], options[1], function(err, stdout, stderr) {
+ if (err !== null) { return callback(err); }
+ if (stderr) { return callback(new Error(stderr.slice(7))); }
+
+ var data = stdout.trim().split(options[2] + '\n');
+ var out = [], total = data.length, count = 0;
+
+ if (data.length) {
+
+ for (var i = 0; i < total / 2; i++) {
+ count = count + 1;
+ out.push({ name: data.shift(), value: 'https://www.youtube.com/watch?v=' + data.shift(), id: count});
+ }
+
+ out.unshift({name: 'All', value: 'all', checked: true}, {name: 'Cancel', value: 'cancel'});
+ }
+
+ callback(null, out, true);
+ });
}
}; \ No newline at end of file