aboutsummaryrefslogtreecommitdiff
path: root/filter.js
diff options
context:
space:
mode:
Diffstat (limited to 'filter.js')
-rwxr-xr-xfilter.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/filter.js b/filter.js
new file mode 100755
index 0000000..5ad75d5
--- /dev/null
+++ b/filter.js
@@ -0,0 +1,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));