diff options
author | tomsmeding <tom.smeding@gmail.com> | 2018-10-14 14:39:41 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2018-10-14 14:39:41 +0200 |
commit | a8e16aa96f4826ffff16d060f694341953b6392e (patch) | |
tree | 3cdc2aae001f1b73ff213f0dce4c0d5c5db7c6bb /filter.js |
Diffstat (limited to 'filter.js')
-rwxr-xr-x | filter.js | 45 |
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)); |