Last active
November 22, 2016 15:39
-
-
Save e10101/647b471907903af36e28dd991b73dde5 to your computer and use it in GitHub Desktop.
A Progress Checker for NZQA IQA - NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AUTHOR: Yishi Guo | |
// DATE: 22/11/2016 | |
// A Progress Checker for NZQA IQA. | |
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var colors = require('colors'); | |
var argv = require('optimist') | |
.usage('Usage: $0 --username [username] --password [password] --id [applicant id] --num [application id]') | |
.alias('username', 'u') | |
.alias('password', 'p') | |
.alias('id', 'i') | |
.alias('num', 'n') | |
.describe('username', 'The login username (email)') | |
.describe('password', 'The login password') | |
.describe('id', 'The applicant ID, e.g. 285000') | |
.describe('num', 'The application ID, e.g. 1, 2, 3') | |
.demand(['username', 'password', 'id', 'num']) | |
.argv; | |
request = request.defaults({ | |
jar: true, | |
headers: { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' | |
} | |
}); | |
var login_url = 'https://secure.nzqa.govt.nz/for-qrs/qual-eval/qrs/loginsubmit.do'; | |
var applicants_list_url = 'https://secure.nzqa.govt.nz/for-qrs/qual-eval/qrs/index.do'; | |
var host_url = 'https://secure.nzqa.govt.nz'; | |
var username = argv.username; | |
var password = argv.password; | |
var targetApplicantId = argv.id; | |
var targetApplicationId = argv.num; | |
var loginFlag = 'for-qrs/qual-eval/qrs'; | |
function login(username, password, cb) { | |
var options = { | |
method: 'POST', | |
url: login_url, | |
form: { | |
email: username, | |
password: password, | |
submit: 'Login' | |
} | |
}; | |
request(options, function(err, response, body) { | |
if (err) { | |
console.log(err, response.statusCode, body); | |
cb(err, false); | |
} | |
var location = response.headers.location; | |
var statusCode = response.statusCode; | |
//console.log('location', location, 'statusCode', statusCode); | |
if (location && location.includes(loginFlag)) { | |
//console.log('login ok!'.green.inverse); | |
return cb(null, true); | |
} | |
cb(null, false); | |
}); | |
}; | |
function get_applicants_list(targetId) { | |
request(applicants_list_url, function(err, response, body) { | |
if (err) {} | |
//console.log('body', body); | |
var $ = cheerio.load(body); | |
var list = $('.tableData tr'); | |
//console.log('list length', list.length); | |
if (list.length > 1) { | |
list = list.not(':first-child'); | |
var output = 'Found ' + list.length + ' applicant(s)'; | |
console.log(output.blue.underline.bold); | |
list.each(function(idx, ele) { | |
var tds = $(this).children('td'); | |
var id = $(tds[0]).text().trim(); | |
var name = $(tds[1]).text().trim(); | |
var type = $(tds[2]).text().trim(); | |
var time = $(tds[3]).text().trim(); | |
var output = '#' + idx + ' / ' + id + ' / ' + name + ' / ' + type + ' / ' + time; | |
if (id == targetId) { | |
console.log(output.blue.bold); | |
var url = $(tds[4]).children('a').attr('href'); | |
//console.log('url', url); | |
get_applications_list(host_url + url, targetApplicationId); | |
} else { | |
console.log(output.blue); | |
} | |
}); | |
} | |
}); | |
}; | |
function get_applications_list(url, targetId) { | |
request(url, function(err, response, body) { | |
if (err) {} | |
//console.log('body', body); | |
var $ = cheerio.load(body); | |
var list = $('.tableData tr'); | |
//console.log('list length', list.length); | |
if (list.length > 1) { | |
list = list.not(':first-child'); | |
var output = 'Found ' + list.length + ' application(s)'; | |
console.log(output.cyan.underline.bold); | |
list.each(function(idx, ele) { | |
var tds = $(this).children('td'); | |
var id = $(tds[0]).text().trim(); | |
var type = $(tds[1]).text().trim(); | |
var qualification = $(tds[2]).text().trim(); | |
var status = $(tds[3]).text().trim(); | |
var submitted = $(tds[4]).text().trim(); | |
var output = '#' + idx + ' / ' + id + ' / ' + type + ' / ' + qualification + ' / ' + status + ' / ' + submitted; | |
if (id == targetId) { | |
console.log(output.cyan.bold); | |
var url = $(tds[5]).children('a').attr('href'); | |
//console.log('url', url); | |
get_application_status(host_url + url); | |
} else { | |
console.log(output.cyan); | |
} | |
}); | |
} | |
}); | |
} | |
function get_application_status(url) { | |
request(url, function(err, response, body) { | |
if (err) {} | |
//console.log('body', body); | |
var $ = cheerio.load(body); | |
var progress = $('.tableDetails tr:nth-child(3) td:last-child'); | |
var text = $(progress).text(); | |
//console.log('text', text); | |
var items = $(progress).children('span'); | |
if (items.length > 0) { | |
var output = 'Application Status'; | |
console.log(output.magenta.underline.bold); | |
items.each(function(idx, ele) { | |
var status = $(ele).attr('class'); | |
var name = $(ele).text().trim(); | |
name = name.replace(/>\s*/, ''); | |
var output = idx + ' / ' + name + ' / '; | |
switch(status) { | |
case 'done': | |
status = status.green.inverse; | |
output = output.green; | |
break; | |
case 'inprogress': | |
status = status.cyan.inverse; | |
output = output.cyan; | |
break; | |
case 'todo': | |
status = status.yellow.inverse; | |
output = output.yellow; | |
break; | |
} | |
output += status; | |
console.log(output); | |
}); | |
} | |
}); | |
} | |
function login_callback(err, isLogin) { | |
//console.log('login callback', err, isLogin); | |
if (err || !isLogin) { | |
console.log('LOGIN ERROR!'.red.inverse); | |
} | |
if (isLogin) { | |
console.log('LOGIN OK!'.green.inverse); | |
get_applicants_list(targetApplicantId); | |
} | |
}; | |
login(username, password, login_callback); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "nzqa", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"cheerio": "^0.22.0", | |
"colors": "^1.1.2", | |
"optimist": "^0.6.1", | |
"request": "^2.79.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OUTPUT: