diff --git a/api.js b/api.js index a3d75f1..96c5583 100644 --- a/api.js +++ b/api.js @@ -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); }); } diff --git a/auth.js b/auth.js index 35ca18b..16bbaf9 100644 --- a/auth.js +++ b/auth.js @@ -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(); }); diff --git a/configuration.js b/configuration.js index ec0aa29..0d25967 100644 --- a/configuration.js +++ b/configuration.js @@ -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 = { diff --git a/database.js b/database.js index e0d77e4..1575b65 100644 --- a/database.js +++ b/database.js @@ -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; } } diff --git a/lookup.js b/lookup.js index 97d302b..1c4bec3 100644 --- a/lookup.js +++ b/lookup.js @@ -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; diff --git a/redirecter.js b/redirecter.js index 88058c1..aac5e82 100644 --- a/redirecter.js +++ b/redirecter.js @@ -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 != '') { diff --git a/schedule.js b/schedule.js index 225955d..acfa6e8 100644 --- a/schedule.js +++ b/schedule.js @@ -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))); }); } diff --git a/spider.js b/spider.js index d8fa7bb..95a7752 100644 --- a/spider.js +++ b/spider.js @@ -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); }