diff options
author | JoelHMikael <joel.h.kronqvist@gmail.com> | 2022-01-16 16:50:44 +0200 |
---|---|---|
committer | JoelHMikael <joel.h.kronqvist@gmail.com> | 2022-01-16 16:50:44 +0200 |
commit | 28e4f1cf0dbd1c14a9724d17cf9127d223d85289 (patch) | |
tree | 4daa0e811e580b1963ffb6b6cda04664b7d19245 | |
parent | 1a1b117cc9eae2cc2ecfb3695bebe21daf08741b (diff) | |
download | LYLLRuoka-28e4f1cf0dbd1c14a9724d17cf9127d223d85289.tar.gz LYLLRuoka-28e4f1cf0dbd1c14a9724d17cf9127d223d85289.zip |
Actual class parsing
Really messy, feel free to improve, or even rewrite.
-rw-r--r-- | dbparse.js | 2 | ||||
-rw-r--r-- | open.js | 16 | ||||
-rw-r--r-- | parseClasses.js | 67 | ||||
-rw-r--r-- | server.js | 20 |
4 files changed, 89 insertions, 16 deletions
@@ -225,7 +225,7 @@ function getIndexType(index) return "course"; if (/^[A-Za-zåäöÅÄÖ]{4}$/.test(index)) return "teacher"; - if (/^\w\d{3}$/.test(index)) + if (/^\w\d{3}R?$/.test(index)) return "class"; } @@ -0,0 +1,16 @@ +const fs = require("fs"); + +function openFile(path) +{ + return new Promise((resolve, reject) => + { + fs.readFile(path, (err, data) => + { + if (err) + reject(err); + resolve(data); + }) + }); +} + +exports.file = openFile; diff --git a/parseClasses.js b/parseClasses.js new file mode 100644 index 0000000..2b3b779 --- /dev/null +++ b/parseClasses.js @@ -0,0 +1,67 @@ +const open = require("./open.js"); +const parse = require("./dbparse.js"); +const fs = require("fs"); +const readline = require("readline"); +const stream = require("stream"); +const getIndexType = require("./dbparse.js").indexType; + +// What a mess. +async function parseClassData(path, DB) +{ + const separator = "\t"; + + const inStream = fs.createReadStream(path) + const outStream = new stream(); + const rl = readline.createInterface(inStream, outStream); + + let lineNum = 0; + let courses = []; + rl.on("line", line => + { + let lineList = line.split(separator); + + let type = getIndexType(lineList[0]); + if (!((type === "class") || (type === "teacher"))) + lineNum = 0; + + if (lineNum % 3 === 0) + courses = lineList; + if ((lineNum % 3) === 2) + { + // Remove the weird "R":s in the end of the classes + for(let i = 0; i < lineList.length; i++) + { + let _s = lineList[i]; + lineList[i] = _s.substring(0, _s.length - 1); + } + addToDBFromLists(DB, courses, lineList, + index => { return getIndexType(index) === "course"; }, + index => { return getIndexType(index) === "class"; } + ); + } + lineNum++ + }); + rl.on("close", () => + { + return 0; + }); +} + +function addToDBFromLists(DB, l1, l2, l1cond, l2cond) +{ + for (let i = 0; i < l1.length; i++) + { + if (l1cond(l1[i]) && l2cond(l2[i])) + DB.execute("INSERT IGNORE INTO classes VALUES (?, ?)", [l1[i], l2[i]]); + } +} + +async function parseClasses(path1, path2, DB) +{ + let parsed = []; + parsed.push(parseClassData(path1, DB)); + parsed.push(parseClassData(path2, DB)); + return await Promise.all(parsed); +} + +exports.classes = parseClasses;
\ No newline at end of file @@ -1,9 +1,11 @@ const http = require("http"); -const fs = require("fs"); +//const fs = require("fs"); const url = require("url"); const scrape = require("./scrape.js"); const SQL_DBS = require("./database.js"); const DBPARSE = require("./dbparse.js"); +const parseClasses = require("./parseClassesCsv.js").classes; +const openFile = require("./open.js").file; async function init() @@ -34,7 +36,7 @@ async function init() // get the MySQL DB connection const SQLDB = new SQL_DBS.Database(JSON.parse(dbcredentials)); - buildDB(SQLDB, "./projectshifts.txt"); + //buildDB(SQLDB, "./projectshifts.txt"); // get the food "database" const foods = [foodsThisWeek, foodsNextWeek]; @@ -123,18 +125,6 @@ function replace(s, from, to) return s; } -function openFile(path) -{ - return new Promise((resolve, reject) => - { - fs.readFile(path, (err, data) => - { - if (err) - reject(err); - resolve(data); - }) - }); -} async function buildMain(args) { @@ -290,7 +280,7 @@ async function buildDB(SQLDB, shiftfile = "./shifts.txt", classfile = "./classes shiftCont = shiftCont.toString("utf-8").replaceAll("\r", ""); // \r because of the \r\n newline on windows which creates problems classCont = classCont.toString("utf-8").replaceAll("\r", ""); await Promise.all([ - DBPARSE.classes(classCont, SQLDB), + parseClasses("./Kurssitarjottimet/2016Classes.txt", "./Kurssitarjottimet/NewClasses.txt", SQLDB), DBPARSE.build(shiftCont, SQLDB) ]); return 0; |