rooster.io/spider.js
2015-06-14 23:04:33 +02:00

139 lines
3.6 KiB
JavaScript

var http = require('http');
var cheerio = require('cheerio');
var iconv = require('iconv-lite');
var mongodb = require('mongodb').MongoClient;
var scheduletypes = [
'Klasrooster',
'Docentrooster',
'Leerlingrooster',
'Lokaalrooster'
];
var school_id;
var database;
//Function for getting pages with http requests.
function get() {
database.collection('index').drop();
for (scheduletype of scheduletypes) {
(function (scheduletype) {
var link = 'http://roosters5.gepro-osi.nl/roosters/rooster.php?school=' + school_id + '&type=' + scheduletype;
http.get(link, function (res) {
var _download = {};
_download.type = scheduletype;
res.on('data', function (data) {
_download.data += data;
});
res.on('end', function () {
rip(_download);
});
});
})(scheduletype);
}
}
//Function for extracting the lists with useful information from the crawled pages.
//(e.g Student names/ids, Teacher codes, Chamber numbers)
function extract(page) {
var array = cheerio('select', page).text().split('\n');
return array.splice(1, array.length - 2);
}
//Function for ripping all of the information
function rip(data) {
var list = extract(data.data);
var collection = database.collection('index');
if (data.type == 'Leerlingrooster') {
for(studentcategory of list) {
(function (studentcategory) {
http.get('http://roosters5.gepro-osi.nl/roosters/rooster.php?school=' + school_id + '&type=' + data.type + '&afdeling=' + studentcategory, function (res) {
var _download = '';
res.on('data', function (data) {
_download += iconv.decode(data, 'binary');
});
res.on('end', function () {
var list_students = cheerio('select', _download).children();
for (student in list_students) {
if (!isNaN(student)) {
var name = cheerio(list_students[student]).text().split(' - ')[1];
var id = cheerio(list_students[student]).val();
var database_entry = {
'id' : id,
'username' : id + name.split(' ')[0].toLowerCase(),
'name' : name,
'first_name' : name.split(' ')[0],
'last_name' : name.split(' ').splice(1).join(' '),
'studentcategory' : studentcategory,
'type' : data.type.replace(/rooster/g, '').toLowerCase()
}
collection.insert(database_entry, show_output);
if (studentcategory == list[list.length - 1] && student == list_students.length - 1) {
database.close();
}
}
}
});
});
})(studentcategory);
}
}
else {
for (entry of list) {
var database_entry = {
'name' : entry,
'type' : data.type.replace(/rooster/g, '').toLowerCase()
}
collection.insert(database_entry, show_output);
}
}
}
//Function being called to access functionality from this module.
function crawl(sid) {
school_id = sid;
mongodb.connect('mongodb://wallpiece/roosterio', function (error, db) {
if (error) console.warn(error);
database = db;
get();
});
}
//Redundant function for draining native-mongodb-driver output
function show_output(error, message) {
if (process.argv[3] == '-v') {
// if (error) process.stdout.write(error.toString());
if (message != null) console.log(message);
}
}
module.exports = {
'crawl' : crawl
}
if (process.argv[2] == 'test') {
module.exports.crawl(934);
}