From 87e7df5f90ecd6a53c25429d232ec827024280c8 Mon Sep 17 00:00:00 2001 From: minie4 Date: Tue, 3 May 2022 18:56:00 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Update=20substitutions=20automatica?= =?UTF-8?q?lly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/index.js | 11 +++++++++++ server/parser/index.js | 18 ++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/server/index.js b/server/index.js index ce953c8..3733106 100644 --- a/server/index.js +++ b/server/index.js @@ -5,10 +5,21 @@ import { getHistory, getClasses, } from "./api/index.js"; +import { Parser } from "./parser/index.js"; const app = express(); const port = process.env.PORT || 3000; +if (!process.env.DSB_USER || !process.env.DSB_PASSWORD) { + console.error("Error: DSB Auth environment variables missing!"); + process.exit(1); +} +const parser = new Parser( + process.env.DSB_USER, + process.env.DSB_PASSWORD, + process.env.UPDATE_INTERVAL || 1 * 60 * 1000 +); + app.get("/api/timetable", getTimetable); app.get("/api/substitutions", getSubstitutions); app.get("/api/history", getHistory); diff --git a/server/parser/index.js b/server/parser/index.js index 71990ae..0eb62f1 100644 --- a/server/parser/index.js +++ b/server/parser/index.js @@ -16,18 +16,8 @@ export class Parser { this.dsbUser = dsbUser; this.dsbPassword = dsbPassword; - // setInterval(this.updatePlan, interval); - // this.updatePlan(); - (async () => { - const event = await prisma.parseEvent.findFirst({ - where: { - succeeded: true, - }, - }); - const html = fs.readFileSync("plan.html"); - const plan = parseSubstitutionPlan(html); - this.insertSubstitutions(plan, event); - })(); + setInterval(() => this.updatePlan(), interval); + this.updatePlan(); } async updatePlan() { const startedAt = new Date(); @@ -51,8 +41,7 @@ export class Parser { }, }); for (const plan of plans) { - await this.insertSubstitutions(plans, parseEvent); - fs.writeFileSync("tmpfile", JSON.stringify(plan)); + await this.insertSubstitutions(plan, parseEvent); } } catch (error) { await prisma.parseEvent.create({ @@ -63,6 +52,7 @@ export class Parser { succeeded: false, }, }); + log("Parser / Main", "Parse event failed: " + error); } } async fetchDSB() {