diff options
Diffstat (limited to 'lib/login.js')
-rw-r--r-- | lib/login.js | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/lib/login.js b/lib/login.js index d99f7ea..ddbbfc4 100644 --- a/lib/login.js +++ b/lib/login.js @@ -1,7 +1,7 @@ /* * mongo-edu * - * Copyright (c) 2014-2015 Przemyslaw Pluta + * Copyright (c) 2014-2016 Przemyslaw Pluta * Licensed under the MIT license. * https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE */ @@ -12,6 +12,8 @@ var request = require('request'), _ = require('lodash'), videoHandler = require('./videos'), PrettyError = require('pretty-error'), + prompts = require('./prompts'), + inquirer = require('inquirer'), coursewareHandler = require('./courseware'); var jar = request.jar(), host = 'https://university.mongodb.com', @@ -31,7 +33,7 @@ module.exports = { 'use strict'; var url = host, - bar = new ProgressBar('>'.magenta + ' Searching [:bar] :percent', { complete: '=', incomplete: ' ', width: 20, total: 7 }), + bar = new ProgressBar('>'.magenta + ' Searching [:bar] :percent', { complete: '=', incomplete: ' ', width: 20, total: 6 }), CSRFTokenCookie = '', login = function login(token) { @@ -94,6 +96,32 @@ module.exports = { }, + courseSection = function courseSection() { + var list = prompts.list; + list[0].message = 'Select Courses:'; + list[0].choices = [{name: 'Current', value: 'tab-current'}, {name: 'Completed', value: 'tab-completed'}]; + return list; + }, + + processSelection = function processSelection(body, answers, currentProfile, callback) { + + var list = [], $ = cheerio.load(body), + current = $('#' + answers.url), + link = current.find('div.section-course').children().find('h4').children(), + noCourses = current.find('section.section-courses-empty').text(), + findNewCourses = current.find('div.section-courses-empty-buttons a').attr('href'); + + $(link).each(function each(i, item) { + list.push({ name: $(item).text(), value: url + $(item).attr('href').replace(/about|info|syllabus/g, '') + ((!argv.h) ? (argv.cw || argv.cwd) ? 'courseware' : 'course_wiki' : 'syllabus') }); + }); + + if (noCourses) { currentProfile.noCourses = noCourses.trim().split('\n').map(function items(item) { + return item.replace(/ +/g, ' '); + }).join('').replace(/ +/g, ' ') + ' at ' + url + findNewCourses + '.\n'; } + + return callback(null, list, currentProfile); + }, + checkCourses = function checkCourses(bar, currentProfile) { bar.tick(); @@ -105,25 +133,12 @@ module.exports = { if (res.statusCode === 200) { - var list = [], $ = cheerio.load(body), - current = $('#tab-current'), - link = current.find('div.section-course').children().find('h4').children(), - noCourses = current.find('section.section-courses-empty').text(), - findNewCourses = current.find('div.section-courses-empty-buttons a').attr('href'); - bar.tick(); - $(link).each(function each(i, item) { - list.push({ name: $(item).text(), value: url + $(item).attr('href').replace(/about|info|syllabus/g, '') + ((!argv.h) ? (argv.cw || argv.cwd) ? 'courseware' : 'course_wiki' : 'syllabus') }); + return inquirer.prompt(courseSection(), function prompt(answers) { + processSelection(body, answers, currentProfile, callback); }); - bar.tick(); - - if (noCourses) { currentProfile.noCourses = noCourses.trim().split('\n').map(function items(item) { - return item.replace(/ +/g, ' '); - }).join('').replace(/ +/g, ' ') + ' at ' + url + findNewCourses + '.\n'; } - - return callback(null, list, currentProfile); } callback(pe.render(new Error(res.statusCode))); |