Skip to content

Instantly share code, notes, and snippets.

@karlding
Created April 28, 2016 23:32
Show Gist options
  • Save karlding/a09050233c8e6d56f9ea1e342487ba35 to your computer and use it in GitHub Desktop.
Save karlding/a09050233c8e6d56f9ea1e342487ba35 to your computer and use it in GitHub Desktop.
Screenshot each page of the CECA employment statistics website using CasperJS, for "safekeeping". Follow the on-screen prompts for instructions.
var system = require('system');
var casper = require('casper').create({
pageSettings: {
loadImages: true,
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
}
});
// read from cin to prevent login credentials being stored in bash history
system.stdout.writeLine('WatIAm login:');
var username = system.stdin.readLine();
system.stdout.writeLine('WatIAm password:');
var password = system.stdin.readLine();
system.stdout.writeLine('Term: (ex. 1161)');
var term = system.stdin.readLine();
system.stdout.writeLine('Date: (ex. 20160114)');
var date = system.stdin.readLine();
casper.start('https://info.uwaterloo.ca/infocecs/students/statistics/index.php');
casper.setHttpAuth(username, password);
casper.on( 'page.error', function (msg, trace) {
this.echo( 'Error: ' + msg, 'ERROR' );
});
var faculty;
// first scrape all the valid faculties
casper.then(function() {
faculty = this.evaluate(function() {
var faculties = document.querySelectorAll('select[name="Faculty"] option');
var temp = [];
for (i = 0; i < faculties.length; ++i) {
temp.push(faculties[i].text);
}
return temp;
});
});
// iterate through all the faculties we scraped
var current = 0;
casper.then(function() {
for (; current < faculty.length; ) {
(function(cntr) {
var id = current;
casper.then(function() {
iterateFaculty(id);
});
})(current);
current++;
}
});
function iterateFaculty(id) {
var baseurl = "https://info.uwaterloo.ca/infocecs/students/statistics/graph.php";
var screenshoturl = baseurl + "?Term=" + term + "&Faculty=" + id + "&Level=-1&Date=" + date;
casper.thenOpen(screenshoturl, function() {
});
casper.then(function() {
var location = term + "/" + date + "/" + faculty[id].replace(/\s+/g, '-').toLowerCase() + ".png";
this.capture(location);
console.log("Saving to: " + location);
})
}
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment