✨ Update substitutions automatically
This commit is contained in:
@ -5,10 +5,21 @@ import {
|
|||||||
getHistory,
|
getHistory,
|
||||||
getClasses,
|
getClasses,
|
||||||
} from "./api/index.js";
|
} from "./api/index.js";
|
||||||
|
import { Parser } from "./parser/index.js";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = process.env.PORT || 3000;
|
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/timetable", getTimetable);
|
||||||
app.get("/api/substitutions", getSubstitutions);
|
app.get("/api/substitutions", getSubstitutions);
|
||||||
app.get("/api/history", getHistory);
|
app.get("/api/history", getHistory);
|
||||||
|
@ -16,18 +16,8 @@ export class Parser {
|
|||||||
this.dsbUser = dsbUser;
|
this.dsbUser = dsbUser;
|
||||||
this.dsbPassword = dsbPassword;
|
this.dsbPassword = dsbPassword;
|
||||||
|
|
||||||
// setInterval(this.updatePlan, interval);
|
setInterval(() => this.updatePlan(), interval);
|
||||||
// this.updatePlan();
|
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);
|
|
||||||
})();
|
|
||||||
}
|
}
|
||||||
async updatePlan() {
|
async updatePlan() {
|
||||||
const startedAt = new Date();
|
const startedAt = new Date();
|
||||||
@ -51,8 +41,7 @@ export class Parser {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
for (const plan of plans) {
|
for (const plan of plans) {
|
||||||
await this.insertSubstitutions(plans, parseEvent);
|
await this.insertSubstitutions(plan, parseEvent);
|
||||||
fs.writeFileSync("tmpfile", JSON.stringify(plan));
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await prisma.parseEvent.create({
|
await prisma.parseEvent.create({
|
||||||
@ -63,6 +52,7 @@ export class Parser {
|
|||||||
succeeded: false,
|
succeeded: false,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
log("Parser / Main", "Parse event failed: " + error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fetchDSB() {
|
async fetchDSB() {
|
||||||
|
Reference in New Issue
Block a user