Added ES6 arrow functions instead of callback function clusterfuck
This commit is contained in:
parent
182ec3ed9a
commit
8756628c4c
8 changed files with 40 additions and 59 deletions
6
api.js
6
api.js
|
|
@ -24,7 +24,7 @@ function parse(req, res, next, api) {
|
||||||
if (api == 'search') {
|
if (api == 'search') {
|
||||||
if (!req.query.name) error('You didn\'t send the needed queries: name', res);
|
if (!req.query.name) error('You didn\'t send the needed queries: name', res);
|
||||||
else {
|
else {
|
||||||
lookup.api(req, function (lookup) {
|
lookup.api(req, (lookup) => {
|
||||||
if (lookup.error) error(lookup.error, res);
|
if (lookup.error) error(lookup.error, res);
|
||||||
else sendResponse(lookup.data, res);
|
else sendResponse(lookup.data, res);
|
||||||
});
|
});
|
||||||
|
|
@ -33,12 +33,12 @@ function parse(req, res, next, api) {
|
||||||
else if (api == 'schedule') {
|
else if (api == 'schedule') {
|
||||||
if (!req.query.name) error('You didn\'t send the needed queries : name', res);
|
if (!req.query.name) error('You didn\'t send the needed queries : name', res);
|
||||||
else {
|
else {
|
||||||
lookup.api(req, function (lookup) {
|
lookup.api(req, (lookup) => {
|
||||||
if (lookup.error) error(lookup.error, res);
|
if (lookup.error) error(lookup.error, res);
|
||||||
else {
|
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)
|
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 {
|
else {
|
||||||
schedule.api(lookup, function (scheduleData) {
|
schedule.api(lookup, (scheduleData) => {
|
||||||
sendResponse(scheduleData, res, true);
|
sendResponse(scheduleData, res, true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
auth.js
12
auth.js
|
|
@ -37,7 +37,7 @@ function getLogin(username, password, callback) {
|
||||||
},
|
},
|
||||||
socksPort: config().torPort,
|
socksPort: config().torPort,
|
||||||
socksHost: config().torHost
|
socksHost: config().torHost
|
||||||
}, function (res) {
|
}, (res) => {
|
||||||
if (res.statusCode == 201 || res.statusCode == 200) callback(true);
|
if (res.statusCode == 201 || res.statusCode == 200) callback(true);
|
||||||
else callback(false);
|
else callback(false);
|
||||||
}).write(login);
|
}).write(login);
|
||||||
|
|
@ -54,14 +54,14 @@ function getLogin(username, password, callback) {
|
||||||
function login(req, res, next) {
|
function login(req, res, next) {
|
||||||
let _data = '';
|
let _data = '';
|
||||||
|
|
||||||
req.on('data', function (data) {
|
req.on('data', (data) => {
|
||||||
_data += data;
|
_data += data;
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('end', function () {
|
req.on('end', () => {
|
||||||
let loginInformation = qs.parse(_data)
|
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 username = crypt.encrypt(loginInformation.username);
|
||||||
let password = crypt.encrypt(loginInformation.password);
|
let password = crypt.encrypt(loginInformation.password);
|
||||||
if (legit) {
|
if (legit) {
|
||||||
|
|
@ -99,10 +99,10 @@ function is(req, res, next) {
|
||||||
let username = crypt.decrypt(cookies.username),
|
let username = crypt.decrypt(cookies.username),
|
||||||
password = crypt.decrypt(cookies.password);
|
password = crypt.decrypt(cookies.password);
|
||||||
|
|
||||||
getLogin(username, password, function (legit) {
|
getLogin(username, password, (legit) => {
|
||||||
if (legit) {
|
if (legit) {
|
||||||
req.query.name = username;
|
req.query.name = username;
|
||||||
lookup.api(req, function (databaseEntry) {
|
lookup.api(req, (databaseEntry) => {
|
||||||
req.headers.user = databaseEntry.data[0];
|
req.headers.user = databaseEntry.data[0];
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ const fs = require('fs');
|
||||||
* Function for the return/creating of a settings file/object.
|
* Function for the return/creating of a settings file/object.
|
||||||
* @return {Object} settings - Object of all the settings.
|
* @return {Object} settings - Object of all the settings.
|
||||||
*/
|
*/
|
||||||
module.exports = function () {
|
module.exports = () => {
|
||||||
if (!fs.existsSync(__dirname + '/settings.json')) {
|
if (!fs.existsSync(__dirname + '/settings.json')) {
|
||||||
//Template for settings.json if not available.
|
//Template for settings.json if not available.
|
||||||
var settings = {
|
var settings = {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ const config = require('./configuration');
|
||||||
* Either local (NeDB) or remote (MongoDB).
|
* Either local (NeDB) or remote (MongoDB).
|
||||||
* @return {Object} database - Entire database engine (NeDB/MongoDB).
|
* @return {Object} database - Entire database engine (NeDB/MongoDB).
|
||||||
*/
|
*/
|
||||||
module.exports = function () {
|
module.exports = () => {
|
||||||
if (!config().localDatabase) return require('mongoskin').db('mongodb://' + config().database);
|
if (!config().localDatabase) return require('mongoskin').db('mongodb://' + config().database);
|
||||||
else {
|
else {
|
||||||
let databases = {
|
let databases = {
|
||||||
|
|
@ -25,16 +25,16 @@ module.exports = function () {
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'collection': function (collection) {
|
'collection': (collection) => {
|
||||||
let database = databases[collection];
|
let database = databases[collection];
|
||||||
|
|
||||||
database.drop = function () {
|
database.drop = () => {
|
||||||
fs.writeFileSync(database.filename, '');
|
fs.writeFileSync(database.filename, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
return database;
|
return database;
|
||||||
},
|
},
|
||||||
'close': function () {
|
'close': () => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
22
lookup.js
22
lookup.js
|
|
@ -30,13 +30,13 @@ function get(req, res, next, search) {
|
||||||
search = new RegExp(search, 'i');
|
search = new RegExp(search, 'i');
|
||||||
|
|
||||||
if (!config().localDatabase) {
|
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);
|
if (err) console.warn(err);
|
||||||
handle(req, res, next, databaseEntry);
|
handle(req, res, next, databaseEntry);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
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);
|
if (err) console.warn(err);
|
||||||
handle(req, res, next, databaseEntry);
|
handle(req, res, next, databaseEntry);
|
||||||
});
|
});
|
||||||
|
|
@ -52,7 +52,7 @@ function get(req, res, next, search) {
|
||||||
*/
|
*/
|
||||||
function handle(req, res, next, databaseEntry) {
|
function handle(req, res, next, databaseEntry) {
|
||||||
if ((req.easter || {}).type == 'RIP') {
|
if ((req.easter || {}).type == 'RIP') {
|
||||||
require('./auth').is(req, res, function () {
|
require('./auth').is(req, res, () => {
|
||||||
res.render('schedule', req);
|
res.render('schedule', req);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -62,13 +62,13 @@ function handle(req, res, next, databaseEntry) {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
else if (databaseEntry.length == 0) {
|
else if (databaseEntry.length == 0) {
|
||||||
require('./auth').is(req, res, function () {
|
require('./auth').is(req, res, () => {
|
||||||
res.render('not_found', req);
|
res.render('not_found', req);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
req.match = databaseEntry;
|
req.match = databaseEntry;
|
||||||
require('./auth').is(req, res, function () {
|
require('./auth').is(req, res, () => {
|
||||||
res.render('list', req);
|
res.render('list', req);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +84,7 @@ function api(req, callback) {
|
||||||
let query = RegExp(req.query.name, 'i');
|
let query = RegExp(req.query.name, 'i');
|
||||||
|
|
||||||
if (!config().localDatabase) {
|
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});
|
if (err) callback({'error': err});
|
||||||
else {
|
else {
|
||||||
for (entry of databaseEntry) {entry.url = makeUrl(req, entry)}
|
for (entry of databaseEntry) {entry.url = makeUrl(req, entry)}
|
||||||
|
|
@ -93,7 +93,7 @@ function api(req, callback) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
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});
|
if (err) callback({'error': err});
|
||||||
else {
|
else {
|
||||||
for (entry of databaseEntry) {entry.url = makeUrl(req, entry)}
|
for (entry of databaseEntry) {entry.url = makeUrl(req, entry)}
|
||||||
|
|
@ -115,10 +115,10 @@ function list(req, res, next, list) {
|
||||||
let query = RegExp(list, 'i');
|
let query = RegExp(list, 'i');
|
||||||
|
|
||||||
if (!config().localDatabase) {
|
if (!config().localDatabase) {
|
||||||
index.find({group: list}).toArray(function (err, databaseEntry) {
|
index.find({group: list}).toArray((err, databaseEntry) => {
|
||||||
if (err) {req.error = err; next();}
|
if (err) {req.error = err; next();}
|
||||||
else {
|
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);
|
res.render('not_found', req);
|
||||||
});
|
});
|
||||||
req.match = databaseEntry;
|
req.match = databaseEntry;
|
||||||
|
|
@ -127,10 +127,10 @@ function list(req, res, next, list) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
index.find({group: list}, function (err, databaseEntry) {
|
index.find({group: list}, (err, databaseEntry) => {
|
||||||
if (err) {req.error = err; next();}
|
if (err) {req.error = err; next();}
|
||||||
else {
|
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);
|
res.render('not_found', req);
|
||||||
});
|
});
|
||||||
req.match = databaseEntry;
|
req.match = databaseEntry;
|
||||||
|
|
|
||||||
|
|
@ -15,15 +15,13 @@ const qs = require('querystring');
|
||||||
* @param {Object} req - Request object supplied by Express.
|
* @param {Object} req - Request object supplied by Express.
|
||||||
* @param {Object} res - Response 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 referer = req.headers.referer.split('/')[3] || 'rooster';
|
||||||
let _data = '';
|
let _data = '';
|
||||||
|
|
||||||
req.on('data', function (data) {
|
req.on('data', (data) => _data += data);
|
||||||
_data += data;
|
|
||||||
});
|
|
||||||
|
|
||||||
req.on('end', function () {
|
req.on('end', () => {
|
||||||
let query = qs.parse(_data);
|
let query = qs.parse(_data);
|
||||||
|
|
||||||
if (query && query.search != '') {
|
if (query && query.search != '') {
|
||||||
|
|
|
||||||
13
schedule.js
13
schedule.js
|
|
@ -23,7 +23,7 @@ const config = require('./configuration');
|
||||||
* @param {Function} next - Next function supplied by Express.
|
* @param {Function} next - Next function supplied by Express.
|
||||||
*/
|
*/
|
||||||
function get(req, res, next) {
|
function get(req, res, next) {
|
||||||
getSchedule(req.match.url, function (json) {
|
getSchedule(req.match.url, (json) => {
|
||||||
req.match.json = json;
|
req.match.json = json;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
@ -39,16 +39,11 @@ function getSchedule(getUrl, callback) {
|
||||||
options.socksPort = config().torPort;
|
options.socksPort = config().torPort;
|
||||||
options.socksHost = config().torHost;
|
options.socksHost = config().torHost;
|
||||||
|
|
||||||
http.get(options, function (res) {
|
http.get(options, (res) => {
|
||||||
let _download = '';
|
let _download = '';
|
||||||
|
|
||||||
res.on('data', function (data) {
|
res.on('data', (data) => _download += data);
|
||||||
_download += data;
|
res.on('end', () => callback(toJSON(_download)));
|
||||||
});
|
|
||||||
|
|
||||||
res.on('end', function () {
|
|
||||||
callback(toJSON(_download));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
28
spider.js
28
spider.js
|
|
@ -43,19 +43,12 @@ function crawl() {
|
||||||
options.socksPort = config().torPort;
|
options.socksPort = config().torPort;
|
||||||
options.socksHost = config().torHost;
|
options.socksHost = config().torHost;
|
||||||
|
|
||||||
http.get(options, function (res) {
|
http.get(options, (res) => {
|
||||||
|
|
||||||
let _download = {};
|
let _download = {};
|
||||||
_download.type = scheduletype;
|
_download.type = scheduletype;
|
||||||
|
|
||||||
res.on('data', function (data) {
|
res.on('data', (data) => _download.data += data);
|
||||||
_download.data += data;
|
res.on('end', () => rip(_download));
|
||||||
});
|
|
||||||
|
|
||||||
res.on('end', function () {
|
|
||||||
rip(_download);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
})(scheduletype);
|
})(scheduletype);
|
||||||
}
|
}
|
||||||
|
|
@ -88,14 +81,12 @@ function rip(page) {
|
||||||
options.socksPort = config().torPort;
|
options.socksPort = config().torPort;
|
||||||
options.socksHost = config().torHost;
|
options.socksHost = config().torHost;
|
||||||
|
|
||||||
http.get(options, function (res) {
|
http.get(options, (res) => {
|
||||||
let _download = '';
|
let _download = '';
|
||||||
|
|
||||||
res.on('data', function (data) {
|
res.on('data', (data) => _download += iconv.decode(data, 'binary'));
|
||||||
_download += iconv.decode(data, 'binary');
|
|
||||||
});
|
|
||||||
|
|
||||||
res.on('end', function () {
|
res.on('end', () => {
|
||||||
let listOfStudents = cheerio('select', _download).children();
|
let listOfStudents = cheerio('select', _download).children();
|
||||||
|
|
||||||
for (student in listOfStudents) {
|
for (student in listOfStudents) {
|
||||||
|
|
@ -119,11 +110,8 @@ function rip(page) {
|
||||||
collection.insert(databaseEntry);
|
collection.insert(databaseEntry);
|
||||||
|
|
||||||
if (studentcategory == list[list.length - 1] && student == listOfStudents.length - 1) {
|
if (studentcategory == list[list.length - 1] && student == listOfStudents.length - 1) {
|
||||||
setTimeout(function () {
|
setTimeout(() => database.close(), config().spiderTimeout);
|
||||||
database.close();
|
|
||||||
}, config().spiderTimeout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -150,5 +138,5 @@ module.exports = {
|
||||||
|
|
||||||
//Testing/ripping command to be used from cli.
|
//Testing/ripping command to be used from cli.
|
||||||
if (process.argv[2] == 'test' || process.argv[2] == 'rip') {
|
if (process.argv[2] == 'test' || process.argv[2] == 'rip') {
|
||||||
module.exports.crawl(934);
|
module.exports.crawl(config().schoolID);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue