Added ES6 arrow functions instead of callback function clusterfuck

This commit is contained in:
Bram van der Veen 2016-06-18 15:26:06 +02:00
parent 182ec3ed9a
commit 8756628c4c
8 changed files with 40 additions and 59 deletions

6
api.js
View file

@ -24,7 +24,7 @@ function parse(req, res, next, api) {
if (api == 'search') {
if (!req.query.name) error('You didn\'t send the needed queries: name', res);
else {
lookup.api(req, function (lookup) {
lookup.api(req, (lookup) => {
if (lookup.error) error(lookup.error, res);
else sendResponse(lookup.data, res);
});
@ -33,12 +33,12 @@ function parse(req, res, next, api) {
else if (api == 'schedule') {
if (!req.query.name) error('You didn\'t send the needed queries : name', res);
else {
lookup.api(req, function (lookup) {
lookup.api(req, (lookup) => {
if (lookup.error) error(lookup.error, res);
else {
if (lookup.data.length > 1 || lookup.data.length == 0) error('The request that you did had multiple responses or none, make sure that your query returns one.', res, lookup.data)
else {
schedule.api(lookup, function (scheduleData) {
schedule.api(lookup, (scheduleData) => {
sendResponse(scheduleData, res, true);
});
}

12
auth.js
View file

@ -37,7 +37,7 @@ function getLogin(username, password, callback) {
},
socksPort: config().torPort,
socksHost: config().torHost
}, function (res) {
}, (res) => {
if (res.statusCode == 201 || res.statusCode == 200) callback(true);
else callback(false);
}).write(login);
@ -54,14 +54,14 @@ function getLogin(username, password, callback) {
function login(req, res, next) {
let _data = '';
req.on('data', function (data) {
req.on('data', (data) => {
_data += data;
});
req.on('end', function () {
req.on('end', () => {
let loginInformation = qs.parse(_data)
getLogin(loginInformation.username, loginInformation.password, function (legit) {
getLogin(loginInformation.username, loginInformation.password, (legit) => {
let username = crypt.encrypt(loginInformation.username);
let password = crypt.encrypt(loginInformation.password);
if (legit) {
@ -99,10 +99,10 @@ function is(req, res, next) {
let username = crypt.decrypt(cookies.username),
password = crypt.decrypt(cookies.password);
getLogin(username, password, function (legit) {
getLogin(username, password, (legit) => {
if (legit) {
req.query.name = username;
lookup.api(req, function (databaseEntry) {
lookup.api(req, (databaseEntry) => {
req.headers.user = databaseEntry.data[0];
next();
});

View file

@ -12,7 +12,7 @@ const fs = require('fs');
* Function for the return/creating of a settings file/object.
* @return {Object} settings - Object of all the settings.
*/
module.exports = function () {
module.exports = () => {
if (!fs.existsSync(__dirname + '/settings.json')) {
//Template for settings.json if not available.
var settings = {

View file

@ -17,7 +17,7 @@ const config = require('./configuration');
* Either local (NeDB) or remote (MongoDB).
* @return {Object} database - Entire database engine (NeDB/MongoDB).
*/
module.exports = function () {
module.exports = () => {
if (!config().localDatabase) return require('mongoskin').db('mongodb://' + config().database);
else {
let databases = {
@ -25,16 +25,16 @@ module.exports = function () {
};
return {
'collection': function (collection) {
'collection': (collection) => {
let database = databases[collection];
database.drop = function () {
database.drop = () => {
fs.writeFileSync(database.filename, '');
}
return database;
},
'close': function () {
'close': () => {
return;
}
}

View file

@ -30,13 +30,13 @@ function get(req, res, next, search) {
search = new RegExp(search, 'i');
if (!config().localDatabase) {
index.find({$or : [{id : search}, {name : search}, {first_name : search}, {last_name : search}, {username: search}]}).toArray(function (err, databaseEntry) {
index.find({$or : [{id : search}, {name : search}, {first_name : search}, {last_name : search}, {username: search}]}).toArray((err, databaseEntry) => {
if (err) console.warn(err);
handle(req, res, next, databaseEntry);
});
}
else {
index.find({$or : [{id : search}, {name : search}, {first_name : search}, {last_name : search}, {username: search}]}, function (err, databaseEntry) {
index.find({$or : [{id : search}, {name : search}, {first_name : search}, {last_name : search}, {username: search}]}, (err, databaseEntry) => {
if (err) console.warn(err);
handle(req, res, next, databaseEntry);
});
@ -52,7 +52,7 @@ function get(req, res, next, search) {
*/
function handle(req, res, next, databaseEntry) {
if ((req.easter || {}).type == 'RIP') {
require('./auth').is(req, res, function () {
require('./auth').is(req, res, () => {
res.render('schedule', req);
});
}
@ -62,13 +62,13 @@ function handle(req, res, next, databaseEntry) {
next();
}
else if (databaseEntry.length == 0) {
require('./auth').is(req, res, function () {
require('./auth').is(req, res, () => {
res.render('not_found', req);
});
}
else {
req.match = databaseEntry;
require('./auth').is(req, res, function () {
require('./auth').is(req, res, () => {
res.render('list', req);
});
}
@ -84,7 +84,7 @@ function api(req, callback) {
let query = RegExp(req.query.name, 'i');
if (!config().localDatabase) {
index.find({$or : [{id : query}, {name : query}, {first_name : query}, {last_name : query}, {username: query}, {group: query}]}).toArray(function (err, databaseEntry) {
index.find({$or : [{id : query}, {name : query}, {first_name : query}, {last_name : query}, {username: query}, {group: query}]}).toArray((err, databaseEntry) => {
if (err) callback({'error': err});
else {
for (entry of databaseEntry) {entry.url = makeUrl(req, entry)}
@ -93,7 +93,7 @@ function api(req, callback) {
});
}
else {
index.find({$or : [{id : query}, {name : query}, {first_name : query}, {last_name : query}, {username: query}, {group: query}]}, function (err, databaseEntry) {
index.find({$or : [{id : query}, {name : query}, {first_name : query}, {last_name : query}, {username: query}, {group: query}]}, (err, databaseEntry) => {
if (err) callback({'error': err});
else {
for (entry of databaseEntry) {entry.url = makeUrl(req, entry)}
@ -115,10 +115,10 @@ function list(req, res, next, list) {
let query = RegExp(list, 'i');
if (!config().localDatabase) {
index.find({group: list}).toArray(function (err, databaseEntry) {
index.find({group: list}).toArray((err, databaseEntry) => {
if (err) {req.error = err; next();}
else {
if (databaseEntry.length < 1) require('./auth').is(req, res, function () {
if (databaseEntry.length < 1) require('./auth').is(req, res, () => {
res.render('not_found', req);
});
req.match = databaseEntry;
@ -127,10 +127,10 @@ function list(req, res, next, list) {
});
}
else {
index.find({group: list}, function (err, databaseEntry) {
index.find({group: list}, (err, databaseEntry) => {
if (err) {req.error = err; next();}
else {
if (databaseEntry.length < 1) require('./auth').is(req, res, function () {
if (databaseEntry.length < 1) require('./auth').is(req, res, () => {
res.render('not_found', req);
});
req.match = databaseEntry;

View file

@ -15,15 +15,13 @@ const qs = require('querystring');
* @param {Object} req - Request object supplied by Express.
* @param {Object} res - Response object supplied by Express.
*/
module.exports = function (req, res) {
module.exports = (req, res) => {
let referer = req.headers.referer.split('/')[3] || 'rooster';
let _data = '';
req.on('data', function (data) {
_data += data;
});
req.on('data', (data) => _data += data);
req.on('end', function () {
req.on('end', () => {
let query = qs.parse(_data);
if (query && query.search != '') {

View file

@ -23,7 +23,7 @@ const config = require('./configuration');
* @param {Function} next - Next function supplied by Express.
*/
function get(req, res, next) {
getSchedule(req.match.url, function (json) {
getSchedule(req.match.url, (json) => {
req.match.json = json;
next();
});
@ -39,16 +39,11 @@ function getSchedule(getUrl, callback) {
options.socksPort = config().torPort;
options.socksHost = config().torHost;
http.get(options, function (res) {
http.get(options, (res) => {
let _download = '';
res.on('data', function (data) {
_download += data;
});
res.on('end', function () {
callback(toJSON(_download));
});
res.on('data', (data) => _download += data);
res.on('end', () => callback(toJSON(_download)));
});
}

View file

@ -43,19 +43,12 @@ function crawl() {
options.socksPort = config().torPort;
options.socksHost = config().torHost;
http.get(options, function (res) {
http.get(options, (res) => {
let _download = {};
_download.type = scheduletype;
res.on('data', function (data) {
_download.data += data;
});
res.on('end', function () {
rip(_download);
});
res.on('data', (data) => _download.data += data);
res.on('end', () => rip(_download));
});
})(scheduletype);
}
@ -88,14 +81,12 @@ function rip(page) {
options.socksPort = config().torPort;
options.socksHost = config().torHost;
http.get(options, function (res) {
http.get(options, (res) => {
let _download = '';
res.on('data', function (data) {
_download += iconv.decode(data, 'binary');
});
res.on('data', (data) => _download += iconv.decode(data, 'binary'));
res.on('end', function () {
res.on('end', () => {
let listOfStudents = cheerio('select', _download).children();
for (student in listOfStudents) {
@ -119,11 +110,8 @@ function rip(page) {
collection.insert(databaseEntry);
if (studentcategory == list[list.length - 1] && student == listOfStudents.length - 1) {
setTimeout(function () {
database.close();
}, config().spiderTimeout);
setTimeout(() => database.close(), config().spiderTimeout);
}
}
}
});
@ -150,5 +138,5 @@ module.exports = {
//Testing/ripping command to be used from cli.
if (process.argv[2] == 'test' || process.argv[2] == 'rip') {
module.exports.crawl(934);
module.exports.crawl(config().schoolID);
}