diff options
author | przemyslawpluta <przemekpluta@hotmail.com> | 2015-08-06 22:06:44 +0100 |
---|---|---|
committer | przemyslawpluta <przemekpluta@hotmail.com> | 2015-08-06 22:06:44 +0100 |
commit | 26d1a1af0a7ad4d8fef24be22bd579ed292fc2f3 (patch) | |
tree | eb05b13ed0eabffca6f57ab08d7821ecc20020f4 /lib/login.js | |
parent | 6dfe97bfb474c53d91ee000f30ac8d5a4bcf0be9 (diff) | |
download | mongo-edu-26d1a1af0a7ad4d8fef24be22bd579ed292fc2f3.zip |
add video preferences check and control
Diffstat (limited to 'lib/login.js')
-rw-r--r-- | lib/login.js | 59 |
1 files changed, 53 insertions, 6 deletions
diff --git a/lib/login.js b/lib/login.js index 88b97f0..4715bc3 100644 --- a/lib/login.js +++ b/lib/login.js @@ -13,7 +13,7 @@ var request = require('request'), videoHandler = require('./videos'), coursewareHandler = require('./courseware'); -var jar = request.jar(), host = 'https://university.mongodb.com'; +var jar = request.jar(), host = 'https://university.mongodb.com', xcsrftoken = {}; function addCookies(cookies, url) { @@ -29,16 +29,18 @@ module.exports = { 'use strict'; var url = host, - bar = new ProgressBar('>'.magenta + ' Searching [:bar] :percent', { complete: '=', incomplete: ' ', width: 20, total: 5 }), + bar = new ProgressBar('>'.magenta + ' Searching [:bar] :percent', { complete: '=', incomplete: ' ', width: 20, total: 7 }), CSRFTokenCookie = '', login = function login(token) { + xcsrftoken = { 'X-CSRFToken': token[0].split('=')[1] }; + request({ uri: url + '/login', method: 'POST', jar: jar, - headers: { 'X-CSRFToken': token[0].split('=')[1] }, + headers: xcsrftoken, form: { 'email': opt.user, 'password': opt.password } }, function post(err, res, body) { @@ -50,7 +52,7 @@ module.exports = { bar.tick(); - return checkCourses(bar); + return checkProfile(bar); } @@ -59,7 +61,34 @@ module.exports = { }); }, - checkCourses = function checkCourses(bar) { + checkProfile = function checkProfile(bar) { + request({ url: url + '/edit_profile', jar: jar }, function get(err, res, body) { + if (err !== null) { return callback(err, null); } + bar.tick(); + if (res.statusCode === 200) { + + bar.tick(); + + var $ = cheerio.load(body), + provider = $('select[name="provider"] option:selected'), + language = $('select[name="language"] option:selected'), + currentProfile = { + preset: { + video: [provider.text(), provider.val()], + language: [language.text(), language.val()] + }, + firstName: $('input[name="first_name"]').attr('value'), + lastName: $('input[name="last_name"]').attr('value'), + username: $('input[name="username"]').attr('value') + }; + + return checkCourses(bar, currentProfile); + } + }); + + }, + + checkCourses = function checkCourses(bar, currentProfile) { bar.tick(); @@ -82,7 +111,7 @@ module.exports = { bar.tick(); - callback(null, list); + callback(null, list, currentProfile); } }); @@ -211,5 +240,23 @@ module.exports = { } callback(new Error('Server Response: '.red + response.statusCode)); }); + }, + + updateProfile: function updateProfile(options, callback) { + + 'use strict'; + + request.post({ + uri: host + '/save_video_preferences', + method: 'POST', + jar: jar, + headers: xcsrftoken, + form: { csrfmiddlewaretoken: xcsrftoken, provider: options.provider, language: options.language } + }, function post(err, res, body) { + + if (err !== null) { return callback(err, null); } + if (res.statusCode === 200) { callback(null, JSON.parse(body)); } + + }); } }; |