Skip to content

Commit

Permalink
add Seminars
Browse files Browse the repository at this point in the history
  • Loading branch information
xilent committed Aug 23, 2016
1 parent 3f3f994 commit c2c4ede
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/resources/server/public/js/
/resources/server/public/*.js
/target/
*.json.gzip
4 changes: 2 additions & 2 deletions src/server/controller/api/cis/user/cisUserApiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
56 changes: 46 additions & 10 deletions src/server/helper/cis/user/cisUserHelper.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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));
});
});
}
Expand All @@ -45,24 +43,61 @@ 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);
} 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';
//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<children.length;i++){
console.log(' case \''+children.eq(i).text()+'\':')
console.log(' return '+children.eq(i).attr('value')+';');
console.log(' break;');
}
console.log(' default:');
console.log(' break;');
console.log('}');
});
callback(false);

});
}
});
Expand All @@ -72,6 +107,7 @@ module.exports = (() => {
getUserDetails,
getGrades,
getSeminarsParticipated,
getSeminars
getSeminars,
getSeminarsSwitchCase
};
})();
156 changes: 148 additions & 8 deletions src/server/helper/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module.exports = (() => {
'use strict';
function parseTableDetails(cheerioHandle, selection) {
Expand All @@ -13,26 +12,167 @@ 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, '');
}

return {
removeWhitespace,
parseTableDetails,
parseTableGrades
parseTable,
parseAvailableSeminarsTable,
getSeminarQuarterID
};
})();
2 changes: 1 addition & 1 deletion src/server/routes/api/cis/user/cisUserApiRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit c2c4ede

Please sign in to comment.