Compare commits
14 Commits
0325ce1815
...
v3
| Author | SHA1 | Date | |
|---|---|---|---|
| f34db1c6e8 | |||
|
e78ee594a0
|
|||
|
16f6eef215
|
|||
|
4a3f1e9f4e
|
|||
|
82074aee60
|
|||
|
ae98a05158
|
|||
|
703139ede0
|
|||
|
7353666eb2
|
|||
|
1013a3ba15
|
|||
|
c34f4ee1d1
|
|||
|
4da178d227
|
|||
|
1f30cda90a
|
|||
|
9d8db43cd4
|
|||
|
6b383b1af4
|
945
package-lock.json
generated
945
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
11
package.json
11
package.json
@@ -10,16 +10,23 @@
|
|||||||
"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",
|
||||||
|
"cors": "^2.8.6",
|
||||||
"dotenv": "^17.2.3",
|
"dotenv": "^17.2.3",
|
||||||
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,10 @@
|
|||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import parseV1 from "./parse/v1.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 parseV1(downloadedFilePath)
|
await parseV1V2(downloadedFilePath);
|
||||||
|
await parseV3("db/v2.json"); // NEEDS TO BE RAN AFTER V2 (uses its format)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,13 +17,11 @@ import fs from "fs"
|
|||||||
import parseAbsence from "../utils/parseAbsence.js"
|
import parseAbsence from "../utils/parseAbsence.js"
|
||||||
import parseTeachers from "../utils/parseTeachers.js"
|
import parseTeachers from "../utils/parseTeachers.js"
|
||||||
|
|
||||||
export default async function parseV1(downloadedFilePath) {
|
export default async function parseV1V2(downloadedFilePath) {
|
||||||
const workbook = new ExcelJS.Workbook();
|
const workbook = new ExcelJS.Workbook();
|
||||||
await workbook.xlsx.readFile(downloadedFilePath);
|
await workbook.xlsx.readFile(downloadedFilePath);
|
||||||
const teacherMap = await parseTeachers();
|
const teacherMap = await parseTeachers();
|
||||||
|
|
||||||
const sheetNames = workbook.worksheets.map((sheet) => sheet.name);
|
|
||||||
|
|
||||||
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 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;
|
||||||
|
|
||||||
// Get today's date for comparison
|
// Get today's date for comparison
|
||||||
@@ -34,18 +32,37 @@ export default async function parseV1(downloadedFilePath) {
|
|||||||
|
|
||||||
const today = getCurrentDateObject();
|
const today = getCurrentDateObject();
|
||||||
|
|
||||||
const upcomingSheets = sheetNames.filter((name) => {
|
const datedSheets = [];
|
||||||
const match = name.match(dateRegex);
|
|
||||||
if (!match) return false;
|
|
||||||
|
|
||||||
const day = Number.parseInt(match[2], 10);
|
for (const sheet of workbook.worksheets) {
|
||||||
const month = Number.parseInt(match[3], 10) - 1; // JavaScript months are 0-indexed
|
const match = sheet.name.match(dateRegex);
|
||||||
const year = Number.parseInt(match[4], 10);
|
if (!match) continue;
|
||||||
|
|
||||||
|
const day = parseInt(match[2], 10);
|
||||||
|
const month = parseInt(match[3], 10) - 1;
|
||||||
|
const year = parseInt(match[4], 10);
|
||||||
|
|
||||||
const sheetDate = new Date(year, month, day);
|
const sheetDate = new Date(year, month, day);
|
||||||
|
if (sheetDate < today) continue;
|
||||||
|
|
||||||
return sheetDate >= today;
|
const dateKey = `${year}-${month + 1}-${day}`;
|
||||||
})
|
|
||||||
|
datedSheets.push({
|
||||||
|
sheet,
|
||||||
|
dateKey,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const sheetsByDate = {};
|
||||||
|
for (const item of datedSheets) {
|
||||||
|
sheetsByDate[item.dateKey] ??= [];
|
||||||
|
sheetsByDate[item.dateKey].push(item.sheet);
|
||||||
|
}
|
||||||
|
|
||||||
|
const upcomingSheets = Object.values(sheetsByDate).map((sheets) => {
|
||||||
|
if (sheets.length === 1) return sheets[0].name;
|
||||||
|
return (sheets.find((s) => s.state !== "hidden") ?? sheets[0]).name;
|
||||||
|
});
|
||||||
|
|
||||||
const final = [];
|
const final = [];
|
||||||
|
|
||||||
@@ -216,7 +233,29 @@ export default async function parseV1(downloadedFilePath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFileSync("db/v1.json", JSON.stringify(data, null, 2));
|
fs.writeFileSync("db/v2.json", JSON.stringify(data, null, 2));
|
||||||
|
|
||||||
|
// Modify the data for v1
|
||||||
|
const copy = JSON.parse(JSON.stringify(data));
|
||||||
|
|
||||||
|
copy.schedule.forEach(day => {
|
||||||
|
if (!Array.isArray(day.ABSENCE)) return;
|
||||||
|
|
||||||
|
day.ABSENCE = day.ABSENCE.map(old => {
|
||||||
|
if (old.type === "zastoupen") {
|
||||||
|
return {
|
||||||
|
type: "invalid",
|
||||||
|
teacher: null,
|
||||||
|
teacherCode: null,
|
||||||
|
hours: null,
|
||||||
|
original: `za ${old.teacherCode.toUpperCase()} zastupuje ${old.zastupuje.teacherCode.toUpperCase()}`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return old;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
fs.writeFileSync("db/v1.json", JSON.stringify(copy, null, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
//parseThisShit("downloads/table.xlsx")
|
//parseV1V2("db/current.xlsx")
|
||||||
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");
|
||||||
@@ -98,8 +98,9 @@ export default function parseAbsence(input, teacherMap = {}) {
|
|||||||
const markConsumed = (start, end) => consumed.push([start, end]);
|
const markConsumed = (start, end) => consumed.push([start, end]);
|
||||||
const isConsumed = (i) => consumed.some(([a, b]) => i >= a && i < b);
|
const isConsumed = (i) => consumed.some(([a, b]) => i >= a && i < b);
|
||||||
|
|
||||||
|
// 1. Teachers with specific hours (e.g. "Ab 1-4")
|
||||||
const teacherListThenSpecRe =
|
const teacherListThenSpecRe =
|
||||||
/([A-Za-z]+(?:[,;]\s?[A-Za-z]+)*)(?:\s*)(\d+(?:\+|-\d+|,\d+)?)(?![A-Za-z])/g;
|
/([A-Za-z]+(?:[,;]\s?[A-Za-z]+)*)(?:\s*)(\d+(?:\+|-\d+|,\d+)?)(?:\.\s*h)?(?![A-Za-z])/g;
|
||||||
|
|
||||||
let m;
|
let m;
|
||||||
while ((m = teacherListThenSpecRe.exec(s)) !== null) {
|
while ((m = teacherListThenSpecRe.exec(s)) !== null) {
|
||||||
@@ -115,6 +116,7 @@ export default function parseAbsence(input, teacherMap = {}) {
|
|||||||
markConsumed(matchStart, matchEnd);
|
markConsumed(matchStart, matchEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. Teachers with "-exk" suffix
|
||||||
const teacherExkRe = /([A-Za-z]+)-exk/gi;
|
const teacherExkRe = /([A-Za-z]+)-exk/gi;
|
||||||
while ((m = teacherExkRe.exec(s)) !== null) {
|
while ((m = teacherExkRe.exec(s)) !== null) {
|
||||||
const matchStart = m.index;
|
const matchStart = m.index;
|
||||||
@@ -132,7 +134,34 @@ export default function parseAbsence(input, teacherMap = {}) {
|
|||||||
markConsumed(matchStart, matchEnd);
|
markConsumed(matchStart, matchEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Standalone teachers → whole day
|
// ---------------------------------------------------------
|
||||||
|
// 3. Substitution Pattern: "za Vn zastupuje Jk"
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
const substitutionRe = /za\s+([A-Za-z]+)\s+zastupuje\s+([A-Za-z]+)/gi;
|
||||||
|
while ((m = substitutionRe.exec(s)) !== null) {
|
||||||
|
const matchStart = m.index;
|
||||||
|
const matchEnd = substitutionRe.lastIndex;
|
||||||
|
if (isConsumed(matchStart)) continue;
|
||||||
|
|
||||||
|
const missingCode = m[1];
|
||||||
|
const substituteCode = m[2];
|
||||||
|
|
||||||
|
const missingResolved = resolveTeacher(missingCode, teacherMap);
|
||||||
|
const subResolved = resolveTeacher(substituteCode, teacherMap);
|
||||||
|
|
||||||
|
results.push({
|
||||||
|
teacher: missingResolved.name,
|
||||||
|
teacherCode: missingResolved.code.toLowerCase(),
|
||||||
|
type: "zastoupen",
|
||||||
|
zastupuje: {
|
||||||
|
teacher: subResolved.name,
|
||||||
|
teacherCode: subResolved.code.toLowerCase()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
markConsumed(matchStart, matchEnd);
|
||||||
|
}
|
||||||
|
|
||||||
const teacherOnlyRe = /([A-Za-z]+(?:[,;]\s?[A-Za-z]+)*)/g;
|
const teacherOnlyRe = /([A-Za-z]+(?:[,;]\s?[A-Za-z]+)*)/g;
|
||||||
while ((m = teacherOnlyRe.exec(s)) !== null) {
|
while ((m = teacherOnlyRe.exec(s)) !== null) {
|
||||||
const matchStart = m.index;
|
const matchStart = m.index;
|
||||||
@@ -141,6 +170,9 @@ export default function parseAbsence(input, teacherMap = {}) {
|
|||||||
|
|
||||||
const tList = m[1].split(/[,;]\s*/).filter(Boolean);
|
const tList = m[1].split(/[,;]\s*/).filter(Boolean);
|
||||||
tList.forEach((t) => {
|
tList.forEach((t) => {
|
||||||
|
const lowerT = t.toLowerCase();
|
||||||
|
if (lowerT === 'za' || lowerT === 'zastupuje') return;
|
||||||
|
|
||||||
if (isTeacherToken(t)) results.push(makeResult(t, null, teacherMap));
|
if (isTeacherToken(t)) results.push(makeResult(t, null, teacherMap));
|
||||||
else
|
else
|
||||||
results.push({
|
results.push({
|
||||||
@@ -154,12 +186,12 @@ export default function parseAbsence(input, teacherMap = {}) {
|
|||||||
markConsumed(matchStart, matchEnd);
|
markConsumed(matchStart, matchEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bare specs without teacher → invalid
|
// 5. Bare specs without teacher → invalid
|
||||||
const specOnlyRe = /\b(\d+(?:\+|-\d+|,\d+)?)\b/g;
|
const specOnlyRe = /\b(\d+(?:\+|-\d+|,\d+)?)\b/g;
|
||||||
while ((m = specOnlyRe.exec(s)) !== null) {
|
while ((m = specOnlyRe.exec(s)) !== null) {
|
||||||
const matchStart = m.index;
|
const matchStart = m.index;
|
||||||
if (isConsumed(matchStart)) continue;
|
if (isConsumed(matchStart)) continue;
|
||||||
if (m[1].trim() == "exk") continue; // Exkurze, will be implemented later
|
if (m[1].trim() == "exk") continue;
|
||||||
|
|
||||||
results.push({
|
results.push({
|
||||||
type: "invalid",
|
type: "invalid",
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ globalThis.File = class File {};
|
|||||||
export default async function parseTeachers() {
|
export default async function parseTeachers() {
|
||||||
const url = "https://spsejecna.cz/ucitel";
|
const url = "https://spsejecna.cz/ucitel";
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
const data = await response.text(); // fetch needs .text() to get HTML
|
const data = await response.text();
|
||||||
const $ = cheerio.load(data);
|
const $ = cheerio.load(data);
|
||||||
|
|
||||||
const map = {};
|
const map = {};
|
||||||
|
|||||||
182
scripts/loadstaticschedule.js
Normal file
182
scripts/loadstaticschedule.js
Normal 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
28
scripts/setup.js
Normal 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
|
||||||
22
server.js
22
server.js
@@ -18,13 +18,21 @@ const app = express();
|
|||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
import { getCurrentInterval } from "./scheduleRules.js";
|
import { getCurrentInterval } from "./scheduleRules.js";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
|
import cors from "cors";
|
||||||
|
|
||||||
|
const VERSIONS = ["v1", "v2"];
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
globalThis.File = class File {};
|
globalThis.File = class File {};
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
app.use(cors({
|
||||||
|
origin: "*",
|
||||||
|
methods: ["GET", "POST", "OPTIONS"],
|
||||||
|
allowedHeaders: ["Content-Type"],
|
||||||
|
}));
|
||||||
|
|
||||||
app.get('/', async (req, res) => {
|
app.get('/', async (req, res) => {
|
||||||
const userAgent = req.headers['user-agent'] || '';
|
const userAgent = req.headers['user-agent'] || '';
|
||||||
const isBrowser = /Mozilla|Chrome|Firefox|Safari|Edg/.test(userAgent);
|
const isBrowser = /Mozilla|Chrome|Firefox|Safari|Edg/.test(userAgent);
|
||||||
@@ -41,13 +49,21 @@ app.get('/', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/versioned/v1', async (_, res) => {
|
VERSIONS.forEach((version) => {
|
||||||
const dataStr = await fs.readFile(path.join(process.cwd(), "db", "v1.json"), "utf8");
|
app.get(`/versioned/${version}`, async (_, res) => {
|
||||||
|
try {
|
||||||
|
const filePath = path.join(process.cwd(), "db", `${version}.json`);
|
||||||
|
const dataStr = await fs.readFile(filePath, "utf8");
|
||||||
const data = JSON.parse(dataStr);
|
const data = JSON.parse(dataStr);
|
||||||
|
|
||||||
data["status"]["currentUpdateSchedule"] = getCurrentInterval();
|
data.status.currentUpdateSchedule = getCurrentInterval();
|
||||||
|
|
||||||
res.json(data);
|
res.json(data);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).json({ error: "Failed to load version data" });
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/status", async (_, res) => {
|
app.get("/status", async (_, res) => {
|
||||||
|
|||||||
127
tests/test.js
127
tests/test.js
@@ -12,19 +12,18 @@
|
|||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import fs from "fs"
|
import fs from "fs";
|
||||||
import parseAbsence from "../scrape/utils/parseAbsence.js";
|
import parseAbsence from "../scrape/utils/parseAbsence.js";
|
||||||
|
|
||||||
const teachermap = JSON.parse(fs.readFileSync("./teachermap.json"))
|
const teachermap = JSON.parse(fs.readFileSync("./teachermap.json"));
|
||||||
|
|
||||||
|
|
||||||
test("Me", [
|
test("Me", [
|
||||||
{
|
{
|
||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("ad", [
|
test("ad", [
|
||||||
@@ -32,8 +31,8 @@ test("ad", [
|
|||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me ad", [
|
test("me ad", [
|
||||||
@@ -41,14 +40,14 @@ test("me ad", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me 3", [
|
test("me 3", [
|
||||||
@@ -56,8 +55,8 @@ test("me 3", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "single",
|
type: "single",
|
||||||
hours: 3
|
hours: 3,
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("ad 1", [
|
test("ad 1", [
|
||||||
@@ -65,8 +64,8 @@ test("ad 1", [
|
|||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "single",
|
type: "single",
|
||||||
hours: 1
|
hours: 1,
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me 2-4", [
|
test("me 2-4", [
|
||||||
@@ -74,8 +73,8 @@ test("me 2-4", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 2, to: 4 }
|
hours: { from: 2, to: 4 },
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("ad 5,6", [
|
test("ad 5,6", [
|
||||||
@@ -83,8 +82,8 @@ test("ad 5,6", [
|
|||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 5, to: 6 }
|
hours: { from: 5, to: 6 },
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me 7+", [
|
test("me 7+", [
|
||||||
@@ -92,8 +91,8 @@ test("me 7+", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 7, to: 10 }
|
hours: { from: 7, to: 10 },
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me,ad 3-5", [
|
test("me,ad 3-5", [
|
||||||
@@ -101,14 +100,14 @@ test("me,ad 3-5", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 3, to: 5 }
|
hours: { from: 3, to: 5 },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 3, to: 5 }
|
hours: { from: 3, to: 5 },
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me;ad 4", [
|
test("me;ad 4", [
|
||||||
@@ -116,14 +115,14 @@ test("me;ad 4", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "single",
|
type: "single",
|
||||||
hours: 4
|
hours: 4,
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me;ad;bo 2-3", [
|
test("me;ad;bo 2-3", [
|
||||||
@@ -131,20 +130,20 @@ test("me;ad;bo 2-3", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Ing. Anna Bodnárová",
|
teacher: "Ing. Anna Bodnárová",
|
||||||
teacherCode: "bo",
|
teacherCode: "bo",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 2, to: 3 }
|
hours: { from: 2, to: 3 },
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me 2 ad 4-5", [
|
test("me 2 ad 4-5", [
|
||||||
@@ -152,14 +151,14 @@ test("me 2 ad 4-5", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "single",
|
type: "single",
|
||||||
hours: 2
|
hours: 2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 4, to: 5 }
|
hours: { from: 4, to: 5 },
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("3", [
|
test("3", [
|
||||||
@@ -168,8 +167,8 @@ test("3", [
|
|||||||
teacher: null,
|
teacher: null,
|
||||||
teacherCode: null,
|
teacherCode: null,
|
||||||
hours: null,
|
hours: null,
|
||||||
original: "3"
|
original: "3",
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("2-4", [
|
test("2-4", [
|
||||||
@@ -178,8 +177,8 @@ test("2-4", [
|
|||||||
teacher: null,
|
teacher: null,
|
||||||
teacherCode: null,
|
teacherCode: null,
|
||||||
hours: null,
|
hours: null,
|
||||||
original: "2-4"
|
original: "2-4",
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me Xx 3", [
|
test("me Xx 3", [
|
||||||
@@ -187,9 +186,9 @@ test("me Xx 3", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
},
|
},
|
||||||
{ teacher: null, teacherCode: 'xx', type: 'single', hours: 3 },
|
{ teacher: null, teacherCode: "xx", type: "single", hours: 3 },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("me,ad;bo 1-2 ad 5+", [
|
test("me,ad;bo 1-2 ad 5+", [
|
||||||
@@ -197,26 +196,26 @@ test("me,ad;bo 1-2 ad 5+", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "wholeDay",
|
type: "wholeDay",
|
||||||
hours: null
|
hours: null,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Ing. Anna Bodnárová",
|
teacher: "Ing. Anna Bodnárová",
|
||||||
teacherCode: "bo",
|
teacherCode: "bo",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 1, to: 2 }
|
hours: { from: 1, to: 2 },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
teacher: "Bc. Daniel Adámek",
|
teacher: "Bc. Daniel Adámek",
|
||||||
teacherCode: "ad",
|
teacherCode: "ad",
|
||||||
type: "range",
|
type: "range",
|
||||||
hours: { from: 5, to: 10 }
|
hours: { from: 5, to: 10 },
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test("Me-exk", [
|
test("Me-exk", [
|
||||||
@@ -224,9 +223,30 @@ test("Me-exk", [
|
|||||||
teacher: "Michaela Meitnerová",
|
teacher: "Michaela Meitnerová",
|
||||||
teacherCode: "me",
|
teacherCode: "me",
|
||||||
type: "exkurze",
|
type: "exkurze",
|
||||||
hours: null
|
hours: null,
|
||||||
}
|
},
|
||||||
])
|
]);
|
||||||
|
|
||||||
|
test("za Vn zastupuje Jk", [
|
||||||
|
{
|
||||||
|
teacher: "Ing. Zdeněk Vondra",
|
||||||
|
teacherCode: "vn",
|
||||||
|
type: "zastoupen",
|
||||||
|
zastupuje: {
|
||||||
|
teacher: "David Janoušek",
|
||||||
|
teacherCode: "jk",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
test("Vc 1. h", [
|
||||||
|
{
|
||||||
|
teacher: "Ing. Antonín Vobecký",
|
||||||
|
teacherCode: "vc",
|
||||||
|
type: "single",
|
||||||
|
hours: 1,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
function test(input, expectedOutput) {
|
function test(input, expectedOutput) {
|
||||||
const res = parseAbsence(input, teachermap);
|
const res = parseAbsence(input, teachermap);
|
||||||
@@ -242,7 +262,12 @@ function test(input, expectedOutput) {
|
|||||||
function deepEqual(a, b) {
|
function deepEqual(a, b) {
|
||||||
if (a === b) return true;
|
if (a === b) return true;
|
||||||
|
|
||||||
if (typeof a !== "object" || a === null || typeof b !== "object" || b === null) {
|
if (
|
||||||
|
typeof a !== "object" ||
|
||||||
|
a === null ||
|
||||||
|
typeof b !== "object" ||
|
||||||
|
b === null
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,7 +277,7 @@ function deepEqual(a, b) {
|
|||||||
|
|
||||||
const used = new Array(b.length).fill(false);
|
const used = new Array(b.length).fill(false);
|
||||||
|
|
||||||
return a.every(itemA =>
|
return a.every((itemA) =>
|
||||||
b.some((itemB, i) => {
|
b.some((itemB, i) => {
|
||||||
if (used[i]) return false; // don't reuse elements
|
if (used[i]) return false; // don't reuse elements
|
||||||
if (deepEqual(itemA, itemB)) {
|
if (deepEqual(itemA, itemB)) {
|
||||||
@@ -260,7 +285,7 @@ function deepEqual(a, b) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,5 +296,5 @@ function deepEqual(a, b) {
|
|||||||
|
|
||||||
if (keysA.length !== keysB.length) return false;
|
if (keysA.length !== keysB.length) return false;
|
||||||
|
|
||||||
return keysA.every(key => keysB.includes(key) && deepEqual(a[key], b[key]));
|
return keysA.every((key) => keysB.includes(key) && deepEqual(a[key], b[key]));
|
||||||
}
|
}
|
||||||
|
|||||||
249
web/content/posts/api-usage-v2/index.md
Normal file
249
web/content/posts/api-usage-v2/index.md
Normal file
@@ -0,0 +1,249 @@
|
|||||||
|
---
|
||||||
|
title: "API Dokumentace - Verze 2"
|
||||||
|
date: 2026-01-28
|
||||||
|
tags: ["api", "docs", "v2"]
|
||||||
|
hiddenInHomelist: true
|
||||||
|
---
|
||||||
|
|
||||||
|
Tato stránka detailně popisuje **Verzi 2 (v2)** API Ječná Rozvrh.
|
||||||
|
|
||||||
|
## Endpoint: `GET /versioned/v2`
|
||||||
|
|
||||||
|
Toto je hlavní endpoint, který poskytuje veškerá data o rozvrhu pro v2.
|
||||||
|
|
||||||
|
### Struktura Odpovědi
|
||||||
|
|
||||||
|
Odpověď je JSON objekt, který obsahuje tři hlavní klíče: `schedule`, `props`, a `status`.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Zobrazit příklad struktury odpovědi</summary>
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"schedule": [ /* pole denních rozvrhů */ ],
|
||||||
|
"props": [ /* pole vlastností dnů */ ],
|
||||||
|
"status": { /* objekt stavu */ }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Datové Struktury
|
||||||
|
|
||||||
|
#### Sekce: `schedule`
|
||||||
|
|
||||||
|
Tato sekce je pole, kde každý prvek představuje jeden den. Každý den je objekt, jehož klíče jsou názvy jednotlivých tříd (např. `A1`, `C2a`, `E4`) a speciální klíč `ABSENCE`.
|
||||||
|
|
||||||
|
##### Rozvrh Třídy
|
||||||
|
- **Klíč:** Název třídy (např. `"A1"`)
|
||||||
|
- **Hodnota:** Pole s 10 prvky, které reprezentují 10 vyučovacích hodin.
|
||||||
|
- `string`: Pokud je hodina normálně vyučována, obsahuje název předmětu nebo informaci o změně.
|
||||||
|
- `null`: Pokud hodina odpadá nebo pro ni není záznam.
|
||||||
|
- Text `(bude upřesněno)` může být připojen k předmětu, pokud je změna nejistá.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Zobrazit příklad rozvrhu pro třídu A1</summary>
|
||||||
|
|
||||||
|
```json
|
||||||
|
"A1": [
|
||||||
|
"M 5 Kp(Ng)",
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
"(Me) (bude upřesněno)",
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
]
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|
||||||
|
##### Absence Učitelů
|
||||||
|
- **Klíč:** `"ABSENCE"`
|
||||||
|
- **Hodnota:** Pole objektů, kde každý objekt specifikuje jednu absenci. Struktura objektu je následující:
|
||||||
|
- `teacher` (string | null): Celé jméno učitele, pokud je známé.
|
||||||
|
- `teacherCode` (string | null): Zkratka jména učitele (např. "me", "ad").
|
||||||
|
- `type` (string): Typ absence. Může nabývat následujících hodnot:
|
||||||
|
- `"wholeDay"`: Učitel chybí celý den.
|
||||||
|
- `"single"`: Učitel chybí jednu vyučovací hodinu.
|
||||||
|
- `"range"`: Učitel chybí v rozmezí několika hodin.
|
||||||
|
- `"exkurze"`: Učitel je na exkurzi.
|
||||||
|
- `"invalid"`: Záznam o absenci se nepodařilo zpracovat.
|
||||||
|
- `hours` (object | number | null): Specifikuje hodiny absence.
|
||||||
|
- `null`: Pro typy `wholeDay`, `exkurze`, a `invalid`.
|
||||||
|
- `number` (např. `3`): Pro typ `single`.
|
||||||
|
- `object` (např. `{ "from": 2, "to": 4 }`): Pro typ `range`.
|
||||||
|
- `original` (string | null): Pouze pro typ `invalid`, obsahuje původní nezpracovaný text.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Zobrazit příklady absencí</summary>
|
||||||
|
|
||||||
|
**Celý den:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"teacher": "Jan Novák",
|
||||||
|
"teacherCode": "no",
|
||||||
|
"type": "wholeDay",
|
||||||
|
"hours": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Jedna hodina:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"teacher": "Jan Novák",
|
||||||
|
"teacherCode": "no",
|
||||||
|
"type": "single",
|
||||||
|
"hours": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Rozsah hodin:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"teacher": "Jan Novák",
|
||||||
|
"teacherCode": "no",
|
||||||
|
"type": "range",
|
||||||
|
"hours": { "from": 2, "to": 4 }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Exkurze:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"teacher": "Jan Novák",
|
||||||
|
"teacherCode": "no",
|
||||||
|
"type": "exkurze",
|
||||||
|
"hours": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Zastupuje:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"teacher": "Ing. Zdeněk Vondra",
|
||||||
|
"teacherCode": "vn",
|
||||||
|
"type": "zastoupen",
|
||||||
|
"hours": null,
|
||||||
|
"zastupuje": {
|
||||||
|
"teacher": "David Janoušek",
|
||||||
|
"teacherCode": "jk",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
**Neplatný záznam:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "invalid",
|
||||||
|
"teacher": null,
|
||||||
|
"teacherCode": null,
|
||||||
|
"hours": null,
|
||||||
|
"original": "Nezpracovatelný text"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|
||||||
|
#### Sekce: `props` - Vlastnosti Dnů
|
||||||
|
|
||||||
|
Pole objektů, které doplňují metadata ke každému dni v poli `schedule`. Pořadí prvků v `props` přesně odpovídá pořadí dnů v `schedule`.
|
||||||
|
|
||||||
|
- `date` (string): Datum daného rozvrhu ve formátu `YYYY-MM-DD`.
|
||||||
|
- `priprava` (boolean): Hodnota je `true`, pokud je den součástí "přípravného týdne", jinak `false`.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Zobrazit příklad props</summary>
|
||||||
|
|
||||||
|
```json
|
||||||
|
"props": [
|
||||||
|
{
|
||||||
|
"date": "2025-12-20",
|
||||||
|
"priprava": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"date": "2025-12-21",
|
||||||
|
"priprava": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|
||||||
|
#### Sekce: `status` - Stav a Metadata
|
||||||
|
|
||||||
|
Objekt poskytující informace o aktuálnosti dat.
|
||||||
|
|
||||||
|
- `lastUpdated` (string): Čas poslední úspěšné aktualizace dat ve formátu `HH:MM`.
|
||||||
|
- `currentUpdateSchedule` (number): Interval v **minutách**, ve kterém scraper interně kontroluje a stahuje novou verzi rozvrhu. Tento interval se dynamicky mění v závislosti na denní době (kratší během vyučování, delší v noci).
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Zobrazit příklad status</summary>
|
||||||
|
|
||||||
|
```json
|
||||||
|
"status": {
|
||||||
|
"lastUpdated": "08:30",
|
||||||
|
"currentUpdateSchedule": 5
|
||||||
|
}
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Kompletní Příklad Odpovědi z `GET /versioned/v2`
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Zobrazit kompletní příklad</summary>
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"schedule": [
|
||||||
|
{
|
||||||
|
"A1": [
|
||||||
|
"M 6 Kp(Ng)",
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
"(Me) (bude upřesněno)",
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
],
|
||||||
|
"C2": [
|
||||||
|
"M 6 Kp(Ng)",
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
"(Me) (bude upřesněno)",
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
],
|
||||||
|
"ABSENCE": [
|
||||||
|
{
|
||||||
|
"teacher": "Jan Novák",
|
||||||
|
"teacherCode": "no",
|
||||||
|
"type": "range",
|
||||||
|
"hours": {"from": 1, "to": 3}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"props": [
|
||||||
|
{
|
||||||
|
"date": "2025-12-20",
|
||||||
|
"priprava": false
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"status": {
|
||||||
|
"lastUpdated": "14:30",
|
||||||
|
"currentUpdateSchedule": 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
</details>
|
||||||
@@ -16,7 +16,7 @@ Všechny cesty v této dokumentaci jsou relativní k následující základní U
|
|||||||
|
|
||||||
Kořenový endpoint (`/`) je **zastaralý (deprecated)**.
|
Kořenový endpoint (`/`) je **zastaralý (deprecated)**.
|
||||||
|
|
||||||
Ačkoliv v současnosti vrací stejná data jako `/versioned/v1`, jeho podpora může být v budoucnu ukončena. **Prosím, nepoužívejte tento endpoint pro nové projekty a existující projekty aktualizujte na verzované endpoint.**
|
Ačkoliv v současnosti vrací stejná data jako `/versioned/v1`, jeho podpora může být v budoucnu ukončena. **Prosím, nepoužívejte tento endpoint pro nové projekty a existující projekty aktualizujte na verzované endpointy.**
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -24,11 +24,15 @@ Ačkoliv v současnosti vrací stejná data jako `/versioned/v1`, jeho podpora m
|
|||||||
|
|
||||||
API je verzované, aby byla zajištěna zpětná kompatibilita. Zde je seznam dostupných verzí:
|
API je verzované, aby byla zajištěna zpětná kompatibilita. Zde je seznam dostupných verzí:
|
||||||
|
|
||||||
- ### [Verze 1 (v1)](../api-usage-v1)
|
- ### [Verze 2 (v2)](../api-usage-v2)
|
||||||
**Status:** Stabilní
|
**Status:** Stabilní
|
||||||
**Endpoint:** `/versioned/v1`
|
**Endpoint:** `/versioned/v1`
|
||||||
|
|
||||||
Toto je aktuální a doporučená verze API. Klikněte na odkaz pro zobrazení kompletní dokumentace pro v1.
|
Toto je aktuální a doporučená verze API. Klikněte na odkaz pro zobrazení kompletní dokumentace pro v2.
|
||||||
|
|
||||||
|
- ### [Verze 1 (v1)](../api-usage-v1)
|
||||||
|
**Status:** Stabilní
|
||||||
|
**Endpoint:** `/versioned/v1`
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ menu:
|
|||||||
- name: Autor
|
- name: Autor
|
||||||
url: https://jzitnik.dev
|
url: https://jzitnik.dev
|
||||||
weight: 20
|
weight: 20
|
||||||
|
- name: Zdrojový kód
|
||||||
|
url: https://gitea.jzitnik.dev/jzitnik/jecnarozvrh
|
||||||
|
weight: 30
|
||||||
|
|
||||||
outputs:
|
outputs:
|
||||||
home:
|
home:
|
||||||
|
|||||||
Reference in New Issue
Block a user