chore: Started implementing
This commit is contained in:
73
scrape/parse/v3/call.js
Normal file
73
scrape/parse/v3/call.js
Normal 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
130
scrape/parse/v3/v3.js
Normal 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");
|
||||
Reference in New Issue
Block a user