diff options
author | JoelHMikael <53561102+JoelHMikael@users.noreply.github.com> | 2022-01-18 18:57:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-18 18:57:53 +0200 |
commit | a259127b9fa0349c73340c882f6525f27f7caaed (patch) | |
tree | ad18984d3031ce6bc7ccb549415c58d77672a53f /parseClasses.js | |
parent | 8f711465194f6779271825bdb2413658880f4c18 (diff) | |
parent | edf044c1e1e0c751229549dd9d14062b230149be (diff) | |
download | LYLLRuoka-a259127b9fa0349c73340c882f6525f27f7caaed.tar.gz LYLLRuoka-a259127b9fa0349c73340c882f6525f27f7caaed.zip |
Merge pull request #1 from JoelHMikael/MYSQLDB
Mysqldb
Diffstat (limited to 'parseClasses.js')
-rw-r--r-- | parseClasses.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/parseClasses.js b/parseClasses.js new file mode 100644 index 0000000..170b63d --- /dev/null +++ b/parseClasses.js @@ -0,0 +1,69 @@ +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 = []; + await DB.query_raw("DELETE FROM classes"); + parsed.push(parseClassData(path1, DB)); + if (path2 !== undefined) + parsed.push(parseClassData(path2, DB)); + return await Promise.all(parsed); +} + +exports.classes = parseClasses;
\ No newline at end of file |