From c2c4ede84cbc940990b9ce476d86f18db82c8f19 Mon Sep 17 00:00:00 2001 From: xilent Date: Tue, 23 Aug 2016 15:07:57 +0200 Subject: [PATCH] add Seminars --- .gitignore | 1 + .../api/cis/user/cisUserApiController.js | 4 +- src/server/helper/cis/user/cisUserHelper.js | 56 +++++-- src/server/helper/utils.js | 156 +++++++++++++++++- .../routes/api/cis/user/cisUserApiRoutes.js | 2 +- 5 files changed, 198 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index fe41204..4bfa470 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /resources/server/public/js/ /resources/server/public/*.js /target/ +*.json.gzip diff --git a/src/server/controller/api/cis/user/cisUserApiController.js b/src/server/controller/api/cis/user/cisUserApiController.js index 6f21dbe..d403d2a 100644 --- a/src/server/controller/api/cis/user/cisUserApiController.js +++ b/src/server/controller/api/cis/user/cisUserApiController.js @@ -51,11 +51,11 @@ module.exports = (() => { } function getAvailableSeminars(request, response) { - if (request.query.userkey === null) { + if (request.query.userkey === undefined || request.query.year === undefined, request.query.quarter === undefined) { response.status(404).json({success: false, message: 'error wrong data specified'}); response.end(); } else { - cisUserHelper.getSeminars(request.query.userkey, 0, 0, function(userTable) { + cisUserHelper.getSeminars(request.query.userkey, request.query.year, request.query.quarter, function(userTable) { response.json(userTable); }); } diff --git a/src/server/helper/cis/user/cisUserHelper.js b/src/server/helper/cis/user/cisUserHelper.js index 24d968a..9f2d8f2 100644 --- a/src/server/helper/cis/user/cisUserHelper.js +++ b/src/server/helper/cis/user/cisUserHelper.js @@ -1,8 +1,6 @@ -'use strict'; const request = require('request'); -let cheerio = require('cheerio'); +const cheerio = require('cheerio'); const cheerioAdv = require('cheerio-advanced-selectors'); -cheerio = cheerioAdv.wrap(cheerio); const utils = require('../../utils'); const cisUserAuthHelper = require('./auth/cisUserAuthHelper'); @@ -31,7 +29,7 @@ module.exports = (() => { request.get({url: url, jar: ar}, function (err, httpContent, body) { const $ = cheerio.load(body); const keys = ['modulenumber', 'description', 'exam_date', 'entry_date', 'grade', 'credits']; - callback(utils.parseTableGrades($, '#curricular table tbody tr', keys, keys.length)); + callback(utils.parseTable($, '#curricular table tbody tr', keys, keys.length)); }); }); } @@ -45,12 +43,12 @@ module.exports = (() => { request.get({url: url, jar: ar}, function (err, httpContent, body) { const $ = cheerio.load(body); const keys = ['description', 'period', 'grade', 'credits']; - callback(utils.parseTableGrades($, '#seminar table tbody tr', keys, keys.length)); + callback(utils.parseTable($, '#seminar table tbody tr', keys, keys.length)); }); }); } - function getSeminars(userkey, week, quarter, callback) { + function getSeminars(userkey, year, quarter, callback) { cisUserAuthHelper.getValidTypoCookieByApiKey(userkey, function (typoCookie) { if (typoCookie === false) { callback(false); @@ -58,11 +56,48 @@ module.exports = (() => { const ar = request.jar(); const cookie = request.cookie('fe_typo_user=' + typoCookie); const url = 'https://cis.nordakademie.de/seminarwesen/?tx_nasemdb_pi1[action]=programm'; + //const form = {}; + //form['tx_nasemdb_pi1[quartal]'] = utils.getSeminarQuarterID(year,quarter); + //const form = {'tx_nasemdb_pi1[quartal]' : utils.getSeminarQuarterID(year,quarter)}; + //console.log(form); ar.setCookie(cookie, url); - request.get({url: url, jar: ar}, function (err, httpContent, body) { + request.get({url: url, jar: ar}, function (error, httpContent, body) { + console.log(httpContent.post); const $ = cheerio.load(body); - const keys = ['description', 'period', 'grade', 'credits']; - callback(utils.parseTableGrades($, 'table:last tbody tr', keys, keys.length)); + const keys = ['description', 'from', 'to', 'grade', 'credits']; + callback(utils.parseAvailableSeminarsTable($, 'table',2 , 'tr',1)); + + }); + } + }); + } + + function getSeminarsSwitchCase(userkey,callback){ + cisUserAuthHelper.getValidTypoCookieByApiKey(userkey, function (typoCookie) { + if (typoCookie === false) { + callback(false); + } else { + const ar = request.jar(); + const cookie = request.cookie('fe_typo_user=' + typoCookie); + const url = 'https://cis.nordakademie.de/seminarwesen/?tx_nasemdb_pi1[action]=programm'; + ar.setCookie(cookie, url); + request.get({url: url, jar: ar}, function (error, httpContent, body) { + const $ = cheerio.load(body); + //console.log($('table').eq(1).find('td').eq(2).find('option').html()); + console.log('switch(year_quarter){') + $('table').eq(1).find('td').eq(2).find('select').each(function (id, elem) { + const children = $(elem).children(); + for(let i=0;i { getUserDetails, getGrades, getSeminarsParticipated, - getSeminars + getSeminars, + getSeminarsSwitchCase }; })(); diff --git a/src/server/helper/utils.js b/src/server/helper/utils.js index 6cdd657..1156c0b 100644 --- a/src/server/helper/utils.js +++ b/src/server/helper/utils.js @@ -1,4 +1,3 @@ - module.exports = (() => { 'use strict'; function parseTableDetails(cheerioHandle, selection) { @@ -13,19 +12,158 @@ module.exports = (() => { return tableDictionary; } - function parseTableGrades(cheerioHandle, selection, keys, elementCount) { + function parseTable(cheerioHandle, selection, keys, elementCount, offset) { const tableDictionary = []; cheerioHandle(selection).each(function (id, elem) { - const eachEntry = {}; - const children = cheerioHandle(elem).children(); - for (let count = 0; count < elementCount; count++) { - eachEntry[keys[count]] = removeWhitespace(children.eq(count).text()); + if (offset === undefined || id >= offset) { + const eachEntry = {}; + const children = cheerioHandle(elem).children(); + for (let count = 0; count < elementCount; count++) { + eachEntry[keys[count]] = removeWhitespace(children.eq(count).text()); + } + tableDictionary.push(eachEntry); + } + }); + return tableDictionary; + } + + function parseAvailableSeminarsTable(cheerioHandle, selection, eqElement, afterEqSelection, offset) { + const tableDictionary = []; + cheerioHandle(selection).eq(eqElement).find(afterEqSelection).each(function (id, elem) { + //remove last line and start at offset + if (id >= offset && id <= cheerioHandle(selection).eq(eqElement).find(afterEqSelection).length - 2) { + const eachEntry = {}; + const children = cheerioHandle(elem).children(); + eachEntry['title'] = removeWhitespace(children.eq(0).find('b').text()); + eachEntry['lecturer'] = removeWhitespace(children.eq(0).find('i').text()); + eachEntry['from'] = removeWhitespace(children.eq(1).find('b').text()); + eachEntry['to'] = removeWhitespace(children.eq(2).find('b').text()); + eachEntry['description'] = removeWhitespace(children.eq(3).text()); + eachEntry['category'] = children.eq(4).find('img').attr('title'); + eachEntry['seminarid'] = children.eq(5).find('a').eq(0).attr('href').match(/\d+$/)[0]; + tableDictionary.push(eachEntry); } - tableDictionary.push(eachEntry); }); return tableDictionary; } + function getSeminarQuarterID(year, quarter) { + const year_quarter = '' + year + ' - ' + quarter; + switch (year_quarter) { + case '2016 - 4': + return 95; + break; + case '2016 - 3': + return 94; + break; + case '2016 - 2': + return 93; + break; + case '2016 - 1': + return 92; + break; + case '2015 - 4': + return 88; + break; + case '2015 - 3': + return 91; + break; + case '2015 - 2': + return 90; + break; + case '2015 - 1': + return 89; + break; + case '2014 - 4': + return 87; + break; + case '2014 - 3': + return 86; + break; + case '2014 - 2': + return 85; + break; + case '2014 - 1': + return 84; + break; + case '2013 - 4': + return 82; + break; + case '2013 - 3': + return 81; + break; + case '2013 - 2': + return 80; + break; + case '2013 - 1': + return 79; + break; + case '2012 - 4': + return 78; + break; + case '2012 - 3': + return 77; + break; + case '2012 - 2': + return 76; + break; + case '2012 - 1': + return 75; + break; + case '2011 - 4': + return 74; + break; + case '2011 - 3': + return 73; + break; + case '2011 - 2': + return 72; + break; + case '2011 - 1': + return 71; + break; + case '2010 - 4': + return 70; + break; + case '2010 - 3': + return 69; + break; + case '2010 - 2': + return 68; + break; + case '2010 - 1': + return 67; + break; + case '2009 - 4': + return 66; + break; + case '2009 - 3': + return 65; + break; + case '2009 - 2': + return 64; + break; + case '2009 - 1': + return 63; + break; + case '2008 - 4': + return 62; + break; + case '2008 - 3': + return 61; + break; + case '2008 - 2': + return 60; + break; + case '2008 - 1': + return 59; + break; + default: + return null; + break; + } + } + function removeWhitespace(text) { return text.trim().replace(/\s\s+/g, ''); } @@ -33,6 +171,8 @@ module.exports = (() => { return { removeWhitespace, parseTableDetails, - parseTableGrades + parseTable, + parseAvailableSeminarsTable, + getSeminarQuarterID }; })(); diff --git a/src/server/routes/api/cis/user/cisUserApiRoutes.js b/src/server/routes/api/cis/user/cisUserApiRoutes.js index a476d36..e56e44a 100644 --- a/src/server/routes/api/cis/user/cisUserApiRoutes.js +++ b/src/server/routes/api/cis/user/cisUserApiRoutes.js @@ -6,7 +6,7 @@ router.post('/getAuthCookie', cisUserApiController.getNAKAuthCookie); router.get('/getUserDetails', cisUserApiController.getUserDetails); router.get('/getGrades', cisUserApiController.getGrades); router.get('/getSeminarsParticipated', cisUserApiController.getSeminarsParticipated); -router.get('/getAvailableSeminars', cisUserApiController.getAvailableSeminars); +router.get('/seminars', cisUserApiController.getAvailableSeminars); router.use('/auth', cisUserAuthApiRoutes); module.exports = router;