1
0

chore: Started implementing

This commit is contained in:
2026-02-02 08:36:43 +01:00
parent e78ee594a0
commit f34db1c6e8
7 changed files with 814 additions and 56 deletions

View File

@@ -1,43 +0,0 @@
/*
* Copyright (C) 2025 Jakub Žitník
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
import parseTeachers from "../utils/parseTeachers.js"
import fs from "fs/promises";
const PREVIOUS = "db/v3/_previous.json";
export default async function parseV3(fileV2Path) {
const previousStr = fs.readFile(PREVIOUS, {
encoding: "utf8",
});
const nowStr = fs.readFile(fileV2Path, {
encoding: "utf8",
})
const previous = JSON.parse(previousStr);
const previousDays = previous.props.map(prop => prop.date);
const now = JSON.parse(nowStr);
for (const prop of now.props) {
if (!previousDays.includes(prop.date)) {
doWholeDay(prop);
continue;
}
// TODO: Check for changes for each class, if any change is do the class (will do someday)
}
// CHANGE PREVIOUS TO NOW
fs.copyFile(fileV2Path, PREVIOUS);
}

73
scrape/parse/v3/call.js Normal file
View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2025 Jakub Žitník
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
import { GoogleGenAI } from "@google/genai";
import fs from "fs/promises";
const TIMETABLE_PATH = "db/persistent/timetables.json";
export async function setup() {
const timetable = JSON.parse(
await fs.readFile(TIMETABLE_PATH, { encoding: "utf8" })
);
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY
});
const systemPrompt = await fs.readFile("./prompt.txt", "utf-8");
/**
* @param {Object} changesByClass
* {
* "1A": [ ...changes... ],
* "2B": [ ...changes... ]
* }
* @param {number} dayIndex
*/
return async (changesByClass, dayIndex) => {
const input = {};
for (const cls of Object.keys(changesByClass)) {
input[cls] = {
stableSchedule: timetable[cls][dayIndex],
changes: changesByClass[cls]
};
}
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
config: {
systemInstruction: {
parts: [{ text: systemPrompt }]
},
temperature: 0
},
contents: [
{
role: "user",
parts: [{ text: JSON.stringify(input) }]
}
]
});
const aiOutput = response.text ?? "";
try {
return JSON.parse(aiOutput);
} catch {
return { invalid: true, reason: "AI output could not be parsed" };
}
};
}

130
scrape/parse/v3/v3.js Normal file
View File

@@ -0,0 +1,130 @@
/*
* Copyright (C) 2025 Jakub Žitník
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
import fs from "fs/promises";
import { setup } from "./call.js";
const PREVIOUS = "db/v3/_previous.json";
const FINAL = "db/v3/v3.json";
const EXCLUDE_CLASSES = new Set(["ABSENCE"]);
async function checkFileExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch (err) {
return false;
}
}
function arraysAreEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
}
function getTime() {
const currentDate = new Date();
return currentDate.getHours().toString().padStart(2, "0") + ":" + currentDate.getMinutes().toString().padStart(2, "0");
}
function setupFinal() {
return {
schedule: [],
status: {
lastUpdated: getTime(),
}
}
}
export default async function parseV3(fileV2Path) {
const call = await setup();
let clearRun = false;
let previousStr = "{}";
if (await checkFileExists(PREVIOUS)) {
previousStr = await fs.readFile(PREVIOUS, "utf8");
} else {
clearRun = true;
}
const now = JSON.parse(await fs.readFile(fileV2Path, "utf8"));
const previous = JSON.parse(previousStr);
const previousDays = previous.props?.map(p => p.date) || [];
let final;
if (await checkFileExists(FINAL)) {
final = JSON.parse(await fs.readFile(FINAL, "utf8"));
} else {
final = setupFinal();
clearRun = true;
}
let i = 0;
for (const prop of now.props) {
const date = new Date(prop.date);
const dayIndex = (date.getDay() + 6) % 7;
if (!final.schedule[i]) {
final.schedule[i] = {};
}
const day = now.schedule[i];
const batch = {};
for (const cls of Object.keys(day)) {
if (EXCLUDE_CLASSES.has(cls)) continue;
const newClass = day[cls];
if (clearRun || !previousDays.includes(prop.date)) {
batch[cls] = newClass;
continue;
}
const oldPropIndex = previous.props.findIndex(
p => p.date === prop.date
);
const oldClass = previous.schedule[oldPropIndex]?.[cls] || [];
if (!arraysAreEqual(oldClass, newClass)) {
batch[cls] = newClass;
}
}
if (Object.keys(batch).length > 0) {
const results = await call(batch, dayIndex);
for (const cls of Object.keys(results)) {
final.schedule[i][cls] = results[cls];
}
}
i++;
}
if (!clearRun) {
final.status.lastUpdated = getTime();
}
final.props = now.props;
await fs.writeFile(FINAL, JSON.stringify(final), "utf8");
await fs.copyFile(fileV2Path, PREVIOUS);
}
parseV3("db/v2.json");