1
0

Compare commits

..

6 Commits

Author SHA1 Message Date
53ecce1eca chore: Minor API changes
All checks were successful
Remote Deploy / deploy (push) Successful in 6s
2026-02-07 16:36:34 +01:00
3430b1c2b1 feat: Include text color in colored stuff 2026-02-07 16:35:29 +01:00
c8d18cf248 fix: V3 parser and minor changes
All checks were successful
Remote Deploy / deploy (push) Successful in 7s
2026-02-07 12:20:22 +01:00
0f4bde1b63 fix: Make it always 10 nulls long instead of 9
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-06 20:37:14 +01:00
cdd1d7c078 fix: Add v3 version to the server
All checks were successful
Remote Deploy / deploy (push) Successful in 4s
2026-02-06 20:28:16 +01:00
e3020a278f feat: New v3 API
All checks were successful
Remote Deploy / deploy (push) Successful in 6s
2026-02-06 20:27:21 +01:00
9 changed files with 329 additions and 1330 deletions

913
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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

View File

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

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

@@ -0,0 +1,307 @@
/*
* 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";
import parseAbsence from "../utils/parseAbsence.js"
import parseTeachers from "../utils/parseTeachers.js"
import ExcelJS from "exceljs"
export default async function parseV3(downloadedFilePath) {
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(downloadedFilePath);
const teacherMap = await parseTeachers();
const upcoming = getUpcomingSheets(workbook);
const resolvedDays = groupSheetsByDate(upcoming);
const schedule = {};
for (const { dateKey, sheet } of resolvedDays) {
const { changes, absence, inWork, takesPlace, reservedRooms } = extractDaySchedule(sheet, teacherMap);
schedule[dateKey] = {
info: { inWork },
changes,
absence,
takesPlace,
reservedRooms,
};
}
const data = {
status: { lastUpdated: formatNowTime() },
schedule,
};
fs.writeFileSync("db/v3.json", JSON.stringify(data, null, 2));
}
//
// ────────────────────────────────────────────────────────────
// SHEET FILTERING
// ────────────────────────────────────────────────────────────
//
function getUpcomingSheets(workbook) {
const dateRegex = /^(pondělí|úterý|středa|čtvrtek|pátek|po|út|ut|st|čt|ct|pa|pá)\s+(\d{1,2})\.\s*(\d{1,2})\.\s*(20\d{2})/i;
const today = new Date();
const todayMidnight = new Date(today.getFullYear(), today.getMonth(), today.getDate());
const result = [];
for (const sheet of workbook.worksheets) {
const match = sheet.name.match(dateRegex);
if (!match) continue;
const day = Number(match[2]);
const month = Number(match[3]) - 1;
const year = Number(match[4]);
const sheetDate = new Date(year, month, day);
if (sheetDate < todayMidnight) continue;
const dateKey = `${year}-${String(month + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
result.push({ dateKey, sheet });
}
return result;
}
function groupSheetsByDate(items) {
const map = {};
for (const item of items) {
map[item.dateKey] ??= [];
map[item.dateKey].push(item.sheet);
}
return Object.entries(map).map(([dateKey, sheets]) => {
const chosen =
sheets.length === 1
? sheets[0]
: sheets.find((s) => s.state !== "hidden") ?? sheets[0];
return { dateKey, sheet: chosen };
});
}
//
// ────────────────────────────────────────────────────────────
// DAY PARSING
// ────────────────────────────────────────────────────────────
//
function extractDaySchedule(sheet, teacherMap) {
return {
changes: extractClassChanges(sheet),
absence: extractAbsence(sheet, teacherMap),
inWork: isPripravaSheet(sheet.name),
takesPlace: extractTakesPlace(sheet),
reservedRooms: extractReservedRooms(sheet)
};
}
function isPripravaSheet(name) {
return name
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.includes("priprava");
}
//
// ────────────────────────────────────────────────────────────
// CLASS CHANGES
// ────────────────────────────────────────────────────────────
//
function extractClassChanges(sheet) {
const classRegex = /[AEC][0-4][a-c]?\s*\/.*/s;
const prefixRegex = /[AEC][0-4][a-c]?/;
const classes = [];
const classCells = [];
sheet.eachRow((row) => {
row.eachCell((cell) => {
const value = cell.value;
if (typeof value === "string" && classRegex.test(value) && cell.address.startsWith("A")) {
const prefixMatch = value.match(prefixRegex);
if (prefixMatch) classes.push(prefixMatch[0]);
classCells.push(cell.address);
}
});
});
const changes = {};
classCells.forEach((address, index) => {
const row = sheet.getRow(sheet.getCell(address).row);
changes[classes[index]] = buildLessonArray(row, address);
});
return changes;
}
function buildLessonArray(row, ignoreAddress) {
const lessons = [];
row.eachCell((cell) => {
if (cell.address === ignoreAddress) return;
const colIndex = letterToNumber(cell.address.replace(/[0-9]/g, ""));
lessons[colIndex] = parseLessonCell(cell);
});
const normalized = Array.from(lessons, (x) => (x === undefined ? null : x));
while (normalized.length < 11) normalized.push(null);
return normalized.slice(1, 11);
}
function parseLessonCell(cell) {
try {
const text = (cell.text || "").trim();
const cleanupRegex = /^úklid\s+(?:\d+\s+)?[A-Za-z]{2}$/;
if (!text || cleanupRegex.test(text) || !cell.fill?.fgColor) return null;
const backgroundColor = cell.fill.fgColor.argb === undefined ? undefined : `#${cell.fill.fgColor.argb}`;
const foregroundColor = backgroundColor === undefined ? undefined : (
cell.font?.color?.argb === undefined ? undefined : `#${cell.font.color.argb}`
);
return {
text,
backgroundColor,
foregroundColor,
willBeSpecified: cell.fill.fgColor.argb === "FFFFFF00" ? true : undefined,
};
} catch {
return null;
}
}
function extractTakesPlace(sheet) {
const cell = sheet.getCell("B4");
if (!cell.isMerged) {
return "";
}
let str = cell.master.value.trim();
let i = 5;
while (true) {
const nextCell = sheet.getCell(`B${i}`);
if (!nextCell.isMerged) {
break;
}
const cell = nextCell.master
str += `\n${cell.value.trim()}`
i++;
}
return str;
}
function extractReservedRooms(sheet) {
const result = [];
const cells = [];
sheet.eachRow((row) => {
row.eachCell((cell) => {
const value = cell.value;
if (typeof value === "string" && value.trim() === "rezervace" && cell.address.startsWith("A")) {
cells.push(cell.address);
}
});
});
cells.forEach((address) => {
const row = sheet.getRow(sheet.getCell(address).row);
row.eachCell((cell) => {
if (cell.address === address) return;
result.push(cell.value.trim().length == 0 ? null : cell.value)
});
});
return result;
}
//
// ────────────────────────────────────────────────────────────
// ABSENCE
// ────────────────────────────────────────────────────────────
//
function extractAbsence(sheet, teacherMap) {
let absenceAddress = null;
sheet.eachRow((row) => {
row.eachCell((cell) => {
if ((cell.value || "").toString().trim().toLowerCase() === "absence") {
absenceAddress = cell.address;
}
});
});
if (!absenceAddress) return [];
const row = sheet.getRow(sheet.getCell(absenceAddress).row);
const results = [];
let i = 0;
row.eachCell((cell) => {
if (cell.address === absenceAddress || i >= 10) return;
i++;
const value = (cell.value || "").toString().trim();
if (!value) return;
results.push(...parseAbsence(value, teacherMap));
});
return results;
}
//
// ────────────────────────────────────────────────────────────
// UTILS
// ────────────────────────────────────────────────────────────
//
function letterToNumber(letter) {
return letter.toLowerCase().charCodeAt(0) - 97;
}
function formatNowTime() {
const now = new Date();
return (
now.getHours().toString().padStart(2, "0") +
":" +
now.getMinutes().toString().padStart(2, "0")
);
}
parseV3("db/current.xlsx")

View File

@@ -1,73 +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 { 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" };
}
};
}

View File

@@ -1,130 +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 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

@@ -1,182 +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 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"
});

View File

@@ -1,28 +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 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

@@ -20,7 +20,7 @@ import { getCurrentInterval } from "./scheduleRules.js";
import bodyParser from "body-parser";
import cors from "cors";
const VERSIONS = ["v1", "v2"];
const VERSIONS = ["v1", "v2", "v3"];
const PORT = process.env.PORT || 3000;
globalThis.File = class File {};
@@ -49,6 +49,15 @@ 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) => {
app.get(`/versioned/${version}`, async (_, res) => {
try {