aboutsummaryrefslogtreecommitdiff
path: root/filter.js
blob: 5ad75d55139734b157fa6443bca1de71539bbac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env node
const cheerio = require("cheerio");
const fetch = require("node-fetch");

let query;
if (process.argv.length == 2) {
	query = function(obj) {return true;};
} else if (process.argv.length == 3) {
	const s = process.argv[2];
	query = function(obj) {with (obj) return eval(s);};
} else {
	console.log("Usage: ./filter.js 'period == 4 && slot[0] != \"D\"'");
	process.exit(1);
}

function parseRow(row) {
	if (row.length != 6) return null;
	return {
		period: row[0],
		slot: row[1],
		code: row[2],
		year: row[3],
		ects: row[4],
		name: row[5],
	};
}

function handle(html) {
	const $ = cheerio.load(html);
	let trs = [];
	$("tbody").last().children("tr").each((_, e) =>
		trs.push($(e).children("td").map((_, td) => $(td).text()).get())
	);

	trs = trs.map(r => parseRow(r)).filter(obj => obj != null);

	trs.forEach(obj => {
		if (query(obj)) console.log(JSON.stringify(obj));
	});
}

fetch("http://www.cs.uu.nl/education/")
	.then(res => res.text())
	.then(body => handle(body))
	.catch(err => console.error(err));