1
0

Compare commits

...

4 Commits

Author SHA1 Message Date
f34db1c6e8 chore: Started implementing 2026-02-02 08:36:43 +01:00
e78ee594a0 chore: Idk 2026-01-30 19:18:04 +01:00
16f6eef215 fix: Login await 2026-01-30 18:25:46 +01:00
4a3f1e9f4e feat: Load statis schedule 2026-01-30 18:19:44 +01:00
8 changed files with 1330 additions and 19 deletions

913
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,9 +10,13 @@
"test": "node tests/test.js", "test": "node tests/test.js",
"start": "concurrently \"node server.js\" \"node cron-runner.js\"", "start": "concurrently \"node server.js\" \"node cron-runner.js\"",
"build": "cd web && hugo --gc --minify", "build": "cd web && hugo --gc --minify",
"dev-web": "cd web && hugo serve" "dev-web": "cd web && hugo serve",
"setup-static": "node scripts/loadstaticschedule.js"
}, },
"dependencies": { "dependencies": {
"@google/genai": "^1.38.0",
"axios": "^1.13.4",
"axios-cookiejar-support": "^6.0.5",
"body-parser": "^2.2.0", "body-parser": "^2.2.0",
"cheerio": "^1.1.2", "cheerio": "^1.1.2",
"concurrently": "^9.2.0", "concurrently": "^9.2.0",
@@ -21,6 +25,8 @@
"exceljs": "^4.4.0", "exceljs": "^4.4.0",
"express": "^5.1.0", "express": "^5.1.0",
"node-cron": "^4.2.1", "node-cron": "^4.2.1",
"puppeteer": "^24.10.0" "node-fetch": "^3.3.2",
"puppeteer": "^24.10.0",
"tough-cookie": "^6.0.0"
} }
} }

View File

@@ -13,7 +13,9 @@
*/ */
import parseV1V2 from "./parse/v1_v2.js"; import parseV1V2 from "./parse/v1_v2.js";
import parseV3 from "./parse/v3/v3.js";
export default async function parseThisShit(downloadedFilePath) { export default async function parseThisShit(downloadedFilePath) {
await parseV1V2(downloadedFilePath) await parseV1V2(downloadedFilePath);
await parseV3("db/v2.json"); // NEEDS TO BE RAN AFTER V2 (uses its format)
} }

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");

View File

@@ -0,0 +1,182 @@
/*
* 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 axios from "axios";
import { CookieJar } from "tough-cookie";
import { wrapper } from "axios-cookiejar-support";
import * as cheerio from "cheerio";
import { URLSearchParams } from "url";
import fs from "fs";
const BASE = "https://www.spsejecna.cz";
const PATHS = {
SET_ROLE: "/user/role",
LOGIN: "/user/login",
TEACHERS: "/ucitel",
TEACHER: teacherCode => `/ucitel/${teacherCode}`
};
const DB_PATH = "db/persistent/timetables.json";
const jar = new CookieJar();
const client = wrapper(axios.create({
baseURL: BASE,
jar,
withCredentials: true,
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}
}));
globalThis.File = class File {};
async function login(username, password) {
await client.get("/");
await client.get(PATHS.SET_ROLE, {
params: { role: "student" }
});
const token3Res = await client.get("/");
const token3 = token3Res.data.match(/"token3"\s+value="(\d+)"/)[1];
const form = new URLSearchParams();
form.append('user', username);
form.append('pass', password);
form.append('token3', token3);
form.append('submit', 'Přihlásit+se');
try {
const response = await client.post(PATHS.LOGIN, form.toString(), {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
maxRedirects: 0
});
if (response.status == 200) {
console.log("INVALID CREDENTIALS!");
process.exit(1);
}
} catch {}
}
async function getAllTeacherCodes() {
const list = new Set();
const response = await client.get(PATHS.TEACHERS);
const $ = cheerio.load(response.data);
$("main .contentLeftColumn li, main .contentRightColumn li").each((_, el) => {
const link = $(el).find("a");
const href = link.attr("href");
if (href) {
const key = href.split("/").pop().toLowerCase();
list.add(key);
}
});
return list;
}
async function constructSchedules(allTeachers) {
const classes = {};
function setupClass(className) {
function generateArray(width, height) {
return Array.from({ length: height }, () => Array.from({ length: width }, () => []));
}
classes[className] = generateArray(10, 5);
}
let idk = 0;
for (const key of allTeachers) {
idk++;
const response = await client.get(PATHS.TEACHER(key));
const $ = cheerio.load(response.data);
const tbody = $('table.timetable > tbody');
if (!tbody.length) {
console.log(`ERROR: ${key}`)
continue;
}
tbody.find('tr').slice(1).each((dayIndex, tr) => {
const $tr = $(tr);
let currentHour = 0;
$tr.find('td').each((_, td) => {
const $td = $(td);
const colspan = parseInt($td.attr('colspan') || '1', 10);
const $subject = $td.find('span.subject');
const $class = $td.find('span.class');
const $group = $td.find('span.group');
const $room = $td.find('a.room');
const $employee = $td.find('a.employee');
const hasData = $subject.length && $class.length && $room.length && $employee.length;
let cellData = null;
let classText = '';
if (hasData) {
classText = $class.text().trim();
cellData = {
subject: $subject.text().trim(),
title: $subject.attr('title')?.trim() || '',
group: $group.length ? $group.text().trim() : null,
room: $room.text().trim(),
teacher: {
code: $employee.text().trim().toLowerCase(),
name: $employee.attr('title')?.trim() || ''
}
};
}
for (let i = 0; i < colspan; i++) {
if (currentHour >= 10) {
break;
}
if (hasData && cellData) {
if (classes[classText] === undefined) {
setupClass(classText);
}
classes[classText][dayIndex][currentHour].push(cellData);
}
currentHour++;
}
});
});
console.log(`DONE: ${idk}/${allTeachers.size}`)
}
return classes;
}
await login(process.env.USERNAME, process.env.PASSWORD);
const allTeachers = await getAllTeacherCodes();
const schedule = await constructSchedules(allTeachers)
const str = JSON.stringify(schedule);
fs.writeFileSync(DB_PATH, str, {
encoding: "utf8"
});

28
scripts/setup.js Normal file
View File

@@ -0,0 +1,28 @@
/*
* 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";
const DIRS = [
"db/persistent",
"db/v3",
];
for (const dir of DIRS) {
try {
fs.mkdirSync(dir);
} catch {}
}
// LEAVE ME ALONE I KNOW THIS CODE IS SHIT

View File

@@ -49,15 +49,6 @@ app.get('/', async (req, res) => {
} }
}); });
app.get('/versioned/v1', async (_, res) => {
const dataStr = await fs.readFile(path.join(process.cwd(), "db", "v1.json"), "utf8");
const data = JSON.parse(dataStr);
data["status"]["currentUpdateSchedule"] = getCurrentInterval();
res.json(data);
});
VERSIONS.forEach((version) => { VERSIONS.forEach((version) => {
app.get(`/versioned/${version}`, async (_, res) => { app.get(`/versioned/${version}`, async (_, res) => {
try { try {