1
0

Compare commits

..

25 Commits

Author SHA1 Message Date
ae17dc241a refactor: Rewrite to typescript
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
2026-02-11 08:20:56 +01:00
138fa17e54 fix: Absence keys
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-10 21:53:55 +01:00
d4815d39ca fix: Background color when not specified
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-10 21:44:42 +01:00
de7ac3a48d feat: Background color fetching
All checks were successful
Remote Deploy / deploy (push) Successful in 6s
2026-02-10 21:36:53 +01:00
cfdd97c935 fix: Exkurze shit
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-09 19:26:55 +01:00
75d8ba745f fix: Fix
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-09 16:06:19 +01:00
7937ac4d65 fix: Some minor changes
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-09 16:04:13 +01:00
0c4ca11d7f chore: Some minor fixes
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-09 15:55:40 +01:00
5e5e01e9cb Fix v3
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-09 09:50:57 +00:00
64a01bf98d Idk
All checks were successful
Remote Deploy / deploy (push) Successful in 6s
2026-02-09 09:46:56 +00:00
de5cd4e911 Emergency debugging
All checks were successful
Remote Deploy / deploy (push) Successful in 5s
2026-02-09 08:12:07 +00:00
166b53bb1a Emergency debugging
Some checks failed
Remote Deploy / deploy (push) Has been cancelled
2026-02-09 08:12:06 +00:00
f5166e5231 Fix parser from phone
All checks were successful
Remote Deploy / deploy (push) Successful in 6s
2026-02-09 07:52:08 +00:00
1d743c5553 fix: Takes place
All checks were successful
Remote Deploy / deploy (push) Successful in 6s
2026-02-08 19:38:22 +01:00
dc845edef0 fix: This 2026-02-08 19:29:17 +01:00
0beb0411d6 fix: Another
All checks were successful
Remote Deploy / deploy (push) Successful in 4s
2026-02-08 19:27:19 +01:00
768913aacc fix: Maybe fix
All checks were successful
Remote Deploy / deploy (push) Successful in 6s
2026-02-08 19:25:27 +01:00
3e1b912cf8 fix: New date format
All checks were successful
Remote Deploy / deploy (push) Successful in 4s
2026-02-08 19:23:00 +01:00
d2a702f9f3 fix: Parser
All checks were successful
Remote Deploy / deploy (push) Successful in 6s
2026-02-08 19:19:21 +01:00
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
18 changed files with 1397 additions and 1414 deletions

1
.gitignore vendored
View File

@@ -3,6 +3,7 @@ volume/browser
db db
downloads downloads
errors errors
dist
# Web # Web
web/public web/public

View File

@@ -13,11 +13,25 @@
*/ */
import cron from 'node-cron'; import cron from 'node-cron';
import { exec } from 'child_process'; import { exec } from 'child_process';
import { scheduleRules, toMinutes } from './scheduleRules.js'; import { scheduleRules, toMinutes, ScheduleRule } from './scheduleRules.js';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function runScraper() { function runScraper() {
console.log(`Running scraper at ${new Date().toLocaleString()}...`); console.log(`Running scraper at ${new Date().toLocaleString()}...`);
exec('node scrape/scraper.js', (error, stdout, stderr) => {
let command;
if (process.env.NODE_ENV === 'development') {
const scriptPath = path.resolve(__dirname, 'scrape/scraper.ts');
command = `npx tsx "${scriptPath}"`;
} else {
const scriptPath = path.resolve(__dirname, 'scrape/scraper.js');
command = `node "${scriptPath}"`;
}
exec(command, (error, stdout, stderr) => {
if (error) { if (error) {
console.error(`Scraper error: ${error.message}`); console.error(`Scraper error: ${error.message}`);
return; return;
@@ -27,11 +41,11 @@ function runScraper() {
}); });
} }
function createSchedules(rules) { function createSchedules(rules: ScheduleRule[]) {
rules.forEach(rule => { rules.forEach(rule => {
const startMin = toMinutes(rule.start); const startMin = toMinutes(rule.start);
const endMin = toMinutes(rule.end === "0:00" ? "24:00" : rule.end); const endMin = toMinutes(rule.end === "0:00" ? "24:00" : rule.end);
const times = []; const times: { h: number; m: number }[] = [];
const adjustedEnd = endMin <= startMin ? endMin + 1440 : endMin; const adjustedEnd = endMin <= startMin ? endMin + 1440 : endMin;
for (let t = startMin; t < adjustedEnd; t += rule.interval) { for (let t = startMin; t < adjustedEnd; t += rule.interval) {

1640
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,16 +7,13 @@
"type": "module", "type": "module",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"test": "node tests/test.js", "test": "tsx tests/test.ts",
"start": "concurrently \"node server.js\" \"node cron-runner.js\"", "start": "concurrently \"NODE_ENV=development tsx server.ts\" \"NODE_ENV=development tsx cron-runner.ts\"",
"build": "cd web && hugo --gc --minify", "build": "tsc && cd web && hugo --gc --minify",
"dev-web": "cd web && hugo serve", "serve": "concurrently \"node dist/server.js\" \"node dist/cron-runner.js\"",
"setup-static": "node scripts/loadstaticschedule.js" "dev-web": "cd web && hugo serve"
}, },
"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",
@@ -24,9 +21,20 @@
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"exceljs": "^4.4.0", "exceljs": "^4.4.0",
"express": "^5.1.0", "express": "^5.1.0",
"jszip": "^3.10.1",
"node-cron": "^4.2.1", "node-cron": "^4.2.1",
"node-fetch": "^3.3.2",
"puppeteer": "^24.10.0", "puppeteer": "^24.10.0",
"tough-cookie": "^6.0.0" "xml2js": "^0.6.2"
},
"devDependencies": {
"@types/body-parser": "^1.19.6",
"@types/cors": "^2.8.19",
"@types/express": "^5.0.6",
"@types/jszip": "^3.4.0",
"@types/node": "^25.2.3",
"@types/node-cron": "^3.0.11",
"@types/xml2js": "^0.4.14",
"tsx": "^4.21.0",
"typescript": "^5.9.3"
} }
} }

View File

@@ -13,7 +13,13 @@
*/ */
// Rules: start and end in 24h format, interval in minutes // Rules: start and end in 24h format, interval in minutes
export const scheduleRules = [ export interface ScheduleRule {
start: string;
end: string;
interval: number;
}
export const scheduleRules: ScheduleRule[] = [
{ start: "0:00", end: "3:00", interval: 180 }, { start: "0:00", end: "3:00", interval: 180 },
{ start: "3:00", end: "4:00", interval: 60 }, { start: "3:00", end: "4:00", interval: 60 },
{ start: "5:00", end: "6:00", interval: 30 }, { start: "5:00", end: "6:00", interval: 30 },
@@ -23,12 +29,12 @@ export const scheduleRules = [
{ start: "19:00", end: "0:00", interval: 180 } { start: "19:00", end: "0:00", interval: 180 }
]; ];
export function toMinutes(timeStr) { export function toMinutes(timeStr: string): number {
const [h, m] = timeStr.split(":").map(Number); const [h, m] = timeStr.split(":").map(Number);
return h * 60 + (m || 0); return h * 60 + (m || 0);
} }
export function getCurrentInterval(date = new Date()) { export function getCurrentInterval(date: Date = new Date()): number | null {
const nowMinutes = date.getHours() * 60 + date.getMinutes(); const nowMinutes = date.getHours() * 60 + date.getMinutes();
for (const rule of scheduleRules) { for (const rule of scheduleRules) {

View File

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

View File

@@ -12,17 +12,27 @@
* GNU General Public License for more details. * GNU General Public License for more details.
*/ */
import ExcelJS from "exceljs" import ExcelJS, { Worksheet, Cell } from "exceljs"
import fs from "fs" import fs from "fs"
import parseAbsence from "../utils/parseAbsence.js" import parseAbsence, { AbsenceResult } from "../utils/parseAbsence.js"
import parseTeachers from "../utils/parseTeachers.js" import parseTeachers from "../utils/parseTeachers.js"
export default async function parseV1V2(downloadedFilePath) { interface DatedSheet {
sheet: Worksheet;
dateKey: string;
}
interface ScheduleDay {
[key: string]: any;
ABSENCE?: AbsenceResult[];
}
export default async function parseV1V2(downloadedFilePath: string) {
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 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*(\d{4}|\d{2})/i;
// Get today's date for comparison // Get today's date for comparison
function getCurrentDateObject() { function getCurrentDateObject() {
@@ -32,15 +42,15 @@ export default async function parseV1V2(downloadedFilePath) {
const today = getCurrentDateObject(); const today = getCurrentDateObject();
const datedSheets = []; const datedSheets: DatedSheet[] = [];
for (const sheet of workbook.worksheets) { for (const sheet of workbook.worksheets) {
const match = sheet.name.match(dateRegex); const match = sheet.name.toLowerCase().match(dateRegex);
if (!match) continue; if (!match) continue;
const day = parseInt(match[2], 10); const day = parseInt(match[2], 10);
const month = parseInt(match[3], 10) - 1; const month = parseInt(match[3], 10) - 1;
const year = parseInt(match[4], 10); const year = match[4].length === 2 ? Number('20' + match[4]) : Number(match[4]);
const sheetDate = new Date(year, month, day); const sheetDate = new Date(year, month, day);
if (sheetDate < today) continue; if (sheetDate < today) continue;
@@ -53,7 +63,7 @@ export default async function parseV1V2(downloadedFilePath) {
}); });
} }
const sheetsByDate = {}; const sheetsByDate: Record<string, Worksheet[]> = {};
for (const item of datedSheets) { for (const item of datedSheets) {
sheetsByDate[item.dateKey] ??= []; sheetsByDate[item.dateKey] ??= [];
sheetsByDate[item.dateKey].push(item.sheet); sheetsByDate[item.dateKey].push(item.sheet);
@@ -61,20 +71,23 @@ export default async function parseV1V2(downloadedFilePath) {
const upcomingSheets = Object.values(sheetsByDate).map((sheets) => { const upcomingSheets = Object.values(sheetsByDate).map((sheets) => {
if (sheets.length === 1) return sheets[0].name; if (sheets.length === 1) return sheets[0].name;
return (sheets.find((s) => s.state !== "hidden") ?? sheets[0]).name; const found = sheets.find((s) => s.state !== "hidden");
return (found ?? sheets[0]).name;
}); });
const final = []; const final: ScheduleDay[] = [];
let finalIndex = 0 let finalIndex = 0
for (const key of upcomingSheets) { for (const key of upcomingSheets) {
const currentSheet = workbook.getWorksheet(key); const currentSheet = workbook.getWorksheet(key);
if (!currentSheet) continue;
final.push({}); final.push({});
const regex = /[AEC][0-4][a-c]?\s*\/.*/s; const regex = /[AEC][0-4][a-c]?\s*\/.*/s;
const prefixRegex = /[AEC][0-4][a-c]?/; const prefixRegex = /[AEC][0-4][a-c]?/;
const classes = []; const classes: string[] = [];
const matchingKeys = []; const matchingKeys: string[] = [];
currentSheet.eachRow((row) => { currentSheet.eachRow((row) => {
row.eachCell((cell) => { row.eachCell((cell) => {
@@ -95,7 +108,7 @@ export default async function parseV1V2(downloadedFilePath) {
}) })
}) })
function letterToNumber(letter) { function letterToNumber(letter: string) {
return letter.toLowerCase().charCodeAt(0) - "a".charCodeAt(0); return letter.toLowerCase().charCodeAt(0) - "a".charCodeAt(0);
} }
@@ -104,17 +117,20 @@ export default async function parseV1V2(downloadedFilePath) {
for (const matchingKey of matchingKeys) { for (const matchingKey of matchingKeys) {
const matchingCell = currentSheet.getCell(matchingKey); const matchingCell = currentSheet.getCell(matchingKey);
const rowNumber = matchingCell.row; const rowNumber = matchingCell.row;
const allKeys = []; const allKeys: string[] = [];
// Get all cells in the same row // Get all cells in the same row
const row = currentSheet.getRow(rowNumber); const row = currentSheet.getRow(Number(rowNumber));
row.eachCell((cell) => { row.eachCell((cell) => {
if (cell.address !== matchingKey) { if (cell.address !== matchingKey) {
allKeys.push(cell.address); allKeys.push(cell.address);
} }
}) })
let final2 = []; // Use an array directly, initialized with nulls or sparse array logic
// The original code used `let final2 = []` but treated it as object `final2[parsedKey] = ...`
// Then `Array.from(final2)` converts it to array.
let final2: (string | null)[] = [];
for (const key of allKeys) { for (const key of allKeys) {
const cell = currentSheet.getCell(key); const cell = currentSheet.getCell(key);
@@ -124,13 +140,16 @@ export default async function parseV1V2(downloadedFilePath) {
try { try {
const regex = /^úklid\s+(?:\d+\s+)?[A-Za-z]{2}$/; const regex = /^úklid\s+(?:\d+\s+)?[A-Za-z]{2}$/;
const cellText = cell.text || ""; const cellText = cell.text || "";
if (regex.test(cellText.trim()) || cellText.trim().length == 0 || cell.fill?.fgColor === undefined) { // @ts-ignore - fgColor is missing in type definition for some versions or intricate structure
const fgColor = cell.fill?.fgColor;
if (regex.test(cellText.trim()) || cellText.trim().length == 0 || fgColor === undefined) {
d = false; d = false;
} }
} catch {} } catch {}
if (d) { if (d) {
let text = cell.text; let text = cell.text;
// @ts-ignore
if (cell.fill?.fgColor?.argb == "FFFFFF00") { if (cell.fill?.fgColor?.argb == "FFFFFF00") {
text += "\n(bude upřesněno)"; text += "\n(bude upřesněno)";
} }
@@ -140,19 +159,19 @@ export default async function parseV1V2(downloadedFilePath) {
} }
} }
final2 = Array.from(final2, (item) => (item === undefined ? null : item)); const final2Array = Array.from(final2, (item) => (item === undefined ? null : item));
while (final2.length < 10) { while (final2Array.length < 10) {
final2.push(null); final2Array.push(null);
} }
final[finalIndex][classes[classI]] = final2.slice(1, 11); final[finalIndex][classes[classI]] = final2Array.slice(1, 11);
classI++; classI++;
} }
// ABSENCE // ABSENCE
final[finalIndex]["ABSENCE"] = []; final[finalIndex]["ABSENCE"] = [];
let absenceKey = null; let absenceKey: string | null = null;
currentSheet.eachRow((row) => { currentSheet.eachRow((row) => {
row.eachCell((cell) => { row.eachCell((cell) => {
@@ -166,22 +185,21 @@ export default async function parseV1V2(downloadedFilePath) {
if (absenceKey) { if (absenceKey) {
const absenceCell = currentSheet.getCell(absenceKey); const absenceCell = currentSheet.getCell(absenceKey);
const rowNumber = absenceCell.row; const rowNumber = absenceCell.row;
const allAbsenceKeys = []; const allAbsenceKeys: string[] = [];
// Get all cells in the same row as absence // Get all cells in the same row as absence
const row = currentSheet.getRow(rowNumber); const row = currentSheet.getRow(Number(rowNumber));
row.eachCell((cell) => { row.eachCell((cell) => {
if (cell.address !== absenceKey) { if (cell.address !== absenceKey) { // absenceKey is checked above to be non-null
allAbsenceKeys.push(cell.address); allAbsenceKeys.push(cell.address);
} }
}) })
let i = 0; const absenceRange = new Set(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L"])
for (const absenceKeyCur of allAbsenceKeys) { for (const absenceKeyCur of allAbsenceKeys) {
if (i >= 10) { if (!absenceRange.has(absenceKeyCur.substring(0, 1))) {
break; // stop once 10 items are added break;
} };
i++;
const cell = currentSheet.getCell(absenceKeyCur); const cell = currentSheet.getCell(absenceKeyCur);
const value = (cell.value || "").toString().trim(); const value = (cell.value || "").toString().trim();
@@ -190,7 +208,9 @@ export default async function parseV1V2(downloadedFilePath) {
} }
const data = parseAbsence(value, teacherMap); const data = parseAbsence(value, teacherMap);
final[finalIndex]["ABSENCE"].push(...data); if (final[finalIndex]["ABSENCE"]) {
final[finalIndex]["ABSENCE"]!.push(...data);
}
} }
} }
@@ -203,14 +223,14 @@ export default async function parseV1V2(downloadedFilePath) {
const data = { const data = {
schedule: final, schedule: final,
props: upcomingSheets.map((str) => { props: upcomingSheets.map((str) => {
const dateMatch = str.match(/(\d{1,2})\.\s*(\d{1,2})\.\s*(\d{4})/); const dateMatch = str.match(/(\d{1,2})\.\s*(\d{1,2})\.\s*(\d{4}|\d{2})/);
let date = null; let date = null;
if (dateMatch) { if (dateMatch) {
const day = Number.parseInt(dateMatch[1], 10); const day = Number.parseInt(dateMatch[1], 10);
const month = Number.parseInt(dateMatch[2], 10); const month = Number.parseInt(dateMatch[2], 10);
const year = Number.parseInt(dateMatch[3], 10); const year = dateMatch[3].length === 2 ? Number('20' + dateMatch[3]) : Number(dateMatch[3]);
date = new Date(year, month - 1, day); date = new Date(year, month - 1, day);
} }
@@ -238,10 +258,10 @@ export default async function parseV1V2(downloadedFilePath) {
// Modify the data for v1 // Modify the data for v1
const copy = JSON.parse(JSON.stringify(data)); const copy = JSON.parse(JSON.stringify(data));
copy.schedule.forEach(day => { copy.schedule.forEach((day: ScheduleDay) => {
if (!Array.isArray(day.ABSENCE)) return; if (!Array.isArray(day.ABSENCE)) return;
day.ABSENCE = day.ABSENCE.map(old => { day.ABSENCE = day.ABSENCE.map((old: any) => {
if (old.type === "zastoupen") { if (old.type === "zastoupen") {
return { return {
type: "invalid", type: "invalid",

427
scrape/parse/v3.ts Normal file
View File

@@ -0,0 +1,427 @@
/*
* 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, { Worksheet, Cell, Row } from "exceljs"
import JSZip from "jszip";
import { parseStringPromise } from "xml2js";
interface ThemeColors {
[key: number]: string | null;
}
interface Lesson {
text: string;
backgroundColor: string | null;
foregroundColor?: string;
willBeSpecified?: boolean;
}
interface ResolvedDay {
dateKey: string;
sheet: Worksheet;
}
/**
* Read theme colors from the workbook
*/
async function getThemeColors(filePath: string): Promise<ThemeColors | null> {
const data = fs.readFileSync(filePath);
const zip = await JSZip.loadAsync(data);
// list all files for debug
const themeFile = zip.file("xl/theme/theme1.xml");
if (!themeFile) {
return null;
}
const themeXml = await themeFile.async("text");
const theme = await parseStringPromise(themeXml);
const scheme = theme["a:theme"]?.["a:themeElements"]?.[0]?.["a:clrScheme"]?.[0];
if (!scheme) return null;
function getColor(node: any) {
if (node["a:srgbClr"]) return node["a:srgbClr"][0].$.val;
if (node["a:sysClr"]) return node["a:sysClr"][0].$.lastClr;
return null;
}
const colors: ThemeColors = {
0: getColor(scheme["a:dk1"]?.[0]),
1: getColor(scheme["a:lt1"]?.[0]),
2: getColor(scheme["a:dk2"]?.[0]),
3: getColor(scheme["a:lt2"]?.[0]),
4: getColor(scheme["a:accent1"]?.[0]),
5: getColor(scheme["a:accent2"]?.[0]),
6: getColor(scheme["a:accent3"]?.[0]),
7: getColor(scheme["a:accent4"]?.[0]),
8: getColor(scheme["a:accent5"]?.[0]),
9: getColor(scheme["a:accent6"]?.[0]),
};
return colors;
}
/**
* Apply Excel tint to a base hex color
*/
function applyTintToHex(hex: string, tint: number = 0) {
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
const tintChannel = (c: number) =>
tint > 0 ? Math.round(c + (255 - c) * tint) : Math.round(c * (1 + tint));
const nr = tintChannel(r);
const ng = tintChannel(g);
const nb = tintChannel(b);
return [nr, ng, nb]
.map((v) => v.toString(16).padStart(2, "0"))
.join("")
.toUpperCase();
}
/**
* Resolve final hex for a cell fill
*/
function resolveCellColor(cell: Cell, themeColors: ThemeColors | null) {
// @ts-ignore
if (!cell.fill?.fgColor) return null;
// @ts-ignore
const fg = cell.fill.fgColor;
if (fg.argb) return `#${fg.argb}`;
if (fg.theme !== undefined && themeColors) {
const base = themeColors[fg.theme];
if (!base) return null;
return `#${applyTintToHex(base, fg.tint ?? 0)}`;
}
return null;
}
export default async function parseV3(downloadedFilePath: string) {
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(downloadedFilePath);
const themeColors = await getThemeColors(downloadedFilePath);
const teacherMap = await parseTeachers();
const upcoming = getUpcomingSheets(workbook);
const resolvedDays = groupSheetsByDate(upcoming);
const schedule: any = {};
for (const { dateKey, sheet } of resolvedDays) {
const { changes, absence, inWork, takesPlace, reservedRooms } = extractDaySchedule(sheet, teacherMap, themeColors);
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: ExcelJS.Workbook): ResolvedDay[] {
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*(\d{4}|\d{2})/i;
const today = new Date();
const todayMidnight = new Date(today.getFullYear(), today.getMonth(), today.getDate());
const result: ResolvedDay[] = [];
for (const sheet of workbook.worksheets) {
const match = sheet.name.toLowerCase().match(dateRegex);
if (!match) continue;
const day = Number(match[2]);
const month = Number(match[3]) - 1;
const year = match[4].length === 2 ? Number('20' + match[4]) : 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: ResolvedDay[]) {
const map: Record<string, Worksheet[]> = {};
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: Worksheet, teacherMap: Record<string, string>, themeColors: ThemeColors | null) {
return {
changes: extractClassChanges(sheet, themeColors),
absence: extractAbsence(sheet, teacherMap),
inWork: isPripravaSheet(sheet.name.toLowerCase()),
takesPlace: extractTakesPlace(sheet),
reservedRooms: extractReservedRooms(sheet)
};
}
function isPripravaSheet(name: string) {
return name
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.includes("priprava");
}
//
// ────────────────────────────────────────────────────────────
// CLASS CHANGES
// ────────────────────────────────────────────────────────────
//
function extractClassChanges(sheet: Worksheet, themeColors: ThemeColors | null) {
const classRegex = /[AEC][0-4][a-c]?\s*\/.*/s;
const prefixRegex = /[AEC][0-4][a-c]?/;
const classes: string[] = [];
const classCells: string[] = [];
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: Record<string, (Lesson | null)[]> = {};
classCells.forEach((address, index) => {
const row = sheet.getRow(Number(sheet.getCell(address).row));
changes[classes[index]] = buildLessonArray(row, address, themeColors);
});
return changes;
}
function buildLessonArray(row: Row, ignoreAddress: string, themeColors: ThemeColors | null) {
const lessons: (Lesson | null)[] = [];
row.eachCell((cell) => {
if (cell.address === ignoreAddress) return;
const colIndex = letterToNumber(cell.address.replace(/[0-9]/g, ""));
lessons[colIndex] = parseLessonCell(cell, themeColors);
});
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: Cell, themeColors: ThemeColors | null): Lesson | null {
try {
const text = (cell.text || "").trim();
const cleanupRegex = /^úklid\s+(?:\d+\s+)?[A-Za-z]{2}$/;
// @ts-ignore
if (!text || cleanupRegex.test(text) || !cell.fill?.fgColor) return null;
const backgroundColor = resolveCellColor(cell, themeColors);
const foregroundColor = !backgroundColor ? undefined : (
cell.font?.color?.argb === undefined ? "#FF000000" : `#${cell.font.color.argb}`
);
return {
text,
backgroundColor,
foregroundColor,
// @ts-ignore
willBeSpecified: cell.fill.fgColor.argb === "FFFFFF00" ? true : undefined,
};
} catch {
return null;
}
}
function extractTakesPlace(sheet: Worksheet) {
const cell = sheet.getCell("B4");
if (!cell.isMerged) {
return "";
}
let str = "";
let i = 4;
while (true) {
const tryCells = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
const threshold = 20;
let con = false;
for (const cellTest of tryCells) {
const cellTry = sheet.getCell(`${cellTest}${i}`)
const cellValue = (typeof cellTry?.value === 'string' ? cellTry.value.trim() : "") || "";
if (cellValue.length >= threshold) {
str += `\n${cellValue}`;
con = true;
break;
}
}
if (con || i == 4) {
i++;
continue;
} else {
break;
}
}
return str.trim();
}
function extractReservedRooms(sheet: Worksheet) {
const result: (string | null)[] = [];
const cells: string[] = [];
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(Number(sheet.getCell(address).row));
row.eachCell((cell) => {
if (cell.address === address) return;
const val = cell.value?.toString().trim();
result.push(!val || val.length == 0 ? null : val)
});
});
while (result.length < 10) {
result.push(null);
}
return result;
}
//
// ────────────────────────────────────────────────────────────
// ABSENCE
// ────────────────────────────────────────────────────────────
//
function extractAbsence(sheet: Worksheet, teacherMap: Record<string, string>) {
let absenceAddress: string | null = null;
sheet.eachRow((row) => {
row.eachCell((cell) => {
if ((cell.value || "").toString().trim().toLowerCase() === "absence") {
absenceAddress = cell.address;
}
});
});
if (!absenceAddress) return [];
const row = sheet.getRow(Number(sheet.getCell(absenceAddress).row));
const results: any[] = [];
const absenceRange = new Set(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L"])
row.eachCell((cell) => {
if (cell.address === absenceAddress || !absenceRange.has(cell.address.substring(0, 1))) {
return
};
const value = (cell.value || "").toString().trim();
if (!value) return;
results.push(...parseAbsence(value, teacherMap));
});
return results;
}
//
// ────────────────────────────────────────────────────────────
// UTILS
// ────────────────────────────────────────────────────────────
//
function letterToNumber(letter: string) {
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

@@ -12,7 +12,7 @@
* GNU General Public License for more details. * GNU General Public License for more details.
*/ */
import puppeteer from 'puppeteer'; import puppeteer, { Page, Browser } from 'puppeteer';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import parseThisShit from './parse.js'; import parseThisShit from './parse.js';
@@ -32,7 +32,7 @@ async function clearDownloadsFolder() {
} }
} }
async function handleError(page, err) { async function handleError(page: Page, err: any) {
try { try {
const errorsDir = path.resolve('./errors'); const errorsDir = path.resolve('./errors');
if (!fs.existsSync(errorsDir)) fs.mkdirSync(errorsDir); if (!fs.existsSync(errorsDir)) fs.mkdirSync(errorsDir);
@@ -40,6 +40,7 @@ async function handleError(page, err) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filePath = path.join(errorsDir, `error-${timestamp}.png`); const filePath = path.join(errorsDir, `error-${timestamp}.png`);
// @ts-ignore
await page.screenshot({ path: filePath, fullPage: true }); await page.screenshot({ path: filePath, fullPage: true });
console.error(`❌ Error occurred. Screenshot saved: ${filePath}`); console.error(`❌ Error occurred. Screenshot saved: ${filePath}`);
@@ -64,14 +65,15 @@ async function handleError(page, err) {
} }
(async () => { (async () => {
let browser, page; let browser: Browser | undefined, page: Page | undefined;
try { try {
browser = await puppeteer.launch({ browser = await puppeteer.launch({
headless: 'new', headless: true,
userDataDir: VOLUME_PATH, userDataDir: VOLUME_PATH,
args: ['--no-sandbox', '--disable-setuid-sandbox'] args: ['--no-sandbox', '--disable-setuid-sandbox']
}); });
[page] = await browser.pages(); const pages = await browser.pages();
page = pages[0];
const downloadPath = path.resolve('./downloads'); const downloadPath = path.resolve('./downloads');
if (!fs.existsSync(downloadPath)) fs.mkdirSync(downloadPath); if (!fs.existsSync(downloadPath)) fs.mkdirSync(downloadPath);
@@ -91,7 +93,7 @@ async function handleError(page, err) {
try { try {
await page.waitForSelector('input[type="email"]', { timeout: 3000 }); await page.waitForSelector('input[type="email"]', { timeout: 3000 });
await page.type('input[type="email"]', EMAIL, { delay: 50 }); await page.type('input[type="email"]', EMAIL || "", { delay: 50 });
await page.keyboard.press('Enter'); await page.keyboard.press('Enter');
} catch { } catch {
try { try {
@@ -117,7 +119,7 @@ async function handleError(page, err) {
} }
await page.waitForSelector('input[type="password"]', { timeout: 100000 }); await page.waitForSelector('input[type="password"]', { timeout: 100000 });
await page.type('input[type="password"]', PASSWORD, { delay: 50 }); await page.type('input[type="password"]', PASSWORD || "", { delay: 50 });
await page.keyboard.press('Enter'); await page.keyboard.press('Enter');
try { try {
@@ -133,7 +135,9 @@ async function handleError(page, err) {
await new Promise(r => setTimeout(r, 5000)); await new Promise(r => setTimeout(r, 5000));
const frameHandle = await page.waitForSelector('iframe'); const frameHandle = await page.waitForSelector('iframe');
if (!frameHandle) throw new Error("Frame not found");
const frame = await frameHandle.contentFrame(); const frame = await frameHandle.contentFrame();
if (!frame) throw new Error("Frame content not found");
await frame.waitForSelector('button[title="File"]', { timeout: 60000 }); await frame.waitForSelector('button[title="File"]', { timeout: 60000 });
await frame.click('button[title="File"]'); await frame.click('button[title="File"]');
@@ -153,7 +157,7 @@ async function handleError(page, err) {
await new Promise(r => setTimeout(r, 10000)); await new Promise(r => setTimeout(r, 10000));
function waitForFile(filename, timeout = 30000) { function waitForFile(filename: string, timeout = 30000): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const start = Date.now(); const start = Date.now();
const interval = setInterval(() => { const interval = setInterval(() => {
@@ -168,7 +172,7 @@ async function handleError(page, err) {
}); });
} }
function getNewestFile(dir) { function getNewestFile(dir: string) {
const files = fs.readdirSync(dir) const files = fs.readdirSync(dir)
.map(f => ({ .map(f => ({
name: f, name: f,

View File

@@ -17,10 +17,28 @@ const LAST_HOUR = 10;
// ------------------------------- // -------------------------------
// Helpers // Helpers
// ------------------------------- // -------------------------------
export const cleanInput = (input) => (input ?? "").trim().replace(/\s+/g, " "); export const cleanInput = (input: string | null | undefined): string => (input ?? "").trim().replace(/\s+/g, " ");
export const isTeacherToken = (t) => /^[A-Za-z]+$/.test(t); export const isTeacherToken = (t: string): boolean => /^[A-Za-z]+$/.test(t);
export const parseSpec = (spec) => { interface Spec {
kind: "range" | "single";
value: { from: number; to: number } | number;
}
interface TeacherMap {
[key: string]: string;
}
export interface AbsenceResult {
teacher: string | null;
teacherCode: string | null;
type: string;
hours: { from: number; to: number } | number | null;
zastupuje?: { teacher: string | null; teacherCode: string | null };
original?: string;
}
export const parseSpec = (spec: string | null): Spec | null => {
if (!spec) return null; if (!spec) return null;
let m; let m;
@@ -55,12 +73,12 @@ export const parseSpec = (spec) => {
return null; return null;
}; };
export const resolveTeacher = (teacherCode, teacherMap = {}) => ({ export const resolveTeacher = (teacherCode: string, teacherMap: TeacherMap = {}): { code: string; name: string | null } => ({
code: teacherCode, code: teacherCode,
name: teacherMap?.[teacherCode.toLowerCase()] ?? null, name: teacherMap?.[teacherCode.toLowerCase()] ?? null,
}); });
const makeResult = (teacherCode, spec, teacherMap) => { const makeResult = (teacherCode: string, spec: Spec | null, teacherMap: TeacherMap): AbsenceResult => {
const { name } = resolveTeacher(teacherCode, teacherMap); const { name } = resolveTeacher(teacherCode, teacherMap);
const type = spec ? (spec.kind === "range" ? "range" : "single") : "wholeDay"; const type = spec ? (spec.kind === "range" ? "range" : "single") : "wholeDay";
const hours = spec ? spec.value : null; const hours = spec ? spec.value : null;
@@ -70,8 +88,8 @@ const makeResult = (teacherCode, spec, teacherMap) => {
// ------------------------------- // -------------------------------
// Teacher list processing (modular) // Teacher list processing (modular)
// ------------------------------- // -------------------------------
const processTeacherList = (teacherListStr, spec, teacherMap) => { const processTeacherList = (teacherListStr: string, spec: Spec | null, teacherMap: TeacherMap): AbsenceResult[] => {
let results = []; let results: AbsenceResult[] = [];
const teachers = teacherListStr.split(/[,;]\s*/).filter(Boolean); const teachers = teacherListStr.split(/[,;]\s*/).filter(Boolean);
if (teacherListStr.includes(";")) { if (teacherListStr.includes(";")) {
@@ -89,14 +107,14 @@ const processTeacherList = (teacherListStr, spec, teacherMap) => {
// ------------------------------- // -------------------------------
// Main parser // Main parser
// ------------------------------- // -------------------------------
export default function parseAbsence(input, teacherMap = {}) { export default function parseAbsence(input: string, teacherMap: TeacherMap = {}): AbsenceResult[] {
const s = cleanInput(input); const s = cleanInput(input);
if (!s) return []; if (!s) return [];
const results = []; const results: AbsenceResult[] = [];
const consumed = []; const consumed: [number, number][] = [];
const markConsumed = (start, end) => consumed.push([start, end]); const markConsumed = (start: number, end: number) => consumed.push([start, end]);
const isConsumed = (i) => consumed.some(([a, b]) => i >= a && i < b); const isConsumed = (i: number) => consumed.some(([a, b]) => i >= a && i < b);
// 1. Teachers with specific hours (e.g. "Ab 1-4") // 1. Teachers with specific hours (e.g. "Ab 1-4")
const teacherListThenSpecRe = const teacherListThenSpecRe =
@@ -116,6 +134,28 @@ export default function parseAbsence(input, teacherMap = {}) {
markConsumed(matchStart, matchEnd); markConsumed(matchStart, matchEnd);
} }
// 1a. Teachers with "-startHour-exk" suffix (e.g. "Sv-5-exk")
const teacherStartExkRe = /([A-Za-z]+)-(\d+)-exk/gi;
while ((m = teacherStartExkRe.exec(s)) !== null) {
const matchStart = m.index;
const matchEnd = teacherStartExkRe.lastIndex;
if (isConsumed(matchStart)) continue;
const teacherCode = m[1];
const from = Number(m[2]);
const { name } = resolveTeacher(teacherCode, teacherMap);
if (from >= 1 && from <= LAST_HOUR) {
results.push({
teacher: name,
teacherCode: teacherCode.toLowerCase(),
type: "exkurze",
hours: { from, to: LAST_HOUR },
});
markConsumed(matchStart, matchEnd);
}
}
// 2. Teachers with "-exk" suffix // 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) {
@@ -153,6 +193,7 @@ export default function parseAbsence(input, teacherMap = {}) {
teacher: missingResolved.name, teacher: missingResolved.name,
teacherCode: missingResolved.code.toLowerCase(), teacherCode: missingResolved.code.toLowerCase(),
type: "zastoupen", type: "zastoupen",
hours: null,
zastupuje: { zastupuje: {
teacher: subResolved.name, teacher: subResolved.name,
teacherCode: subResolved.code.toLowerCase() teacherCode: subResolved.code.toLowerCase()

View File

@@ -14,15 +14,16 @@
import * as cheerio from "cheerio"; import * as cheerio from "cheerio";
// @ts-ignore
globalThis.File = class File {}; globalThis.File = class File {};
export default async function parseTeachers() { export default async function parseTeachers(): Promise<Record<string, string>> {
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(); const data = await response.text();
const $ = cheerio.load(data); const $ = cheerio.load(data);
const map = {}; const map: Record<string, string> = {};
$("main .contentLeftColumn li, main .contentRightColumn li").each((_, el) => { $("main .contentLeftColumn li, main .contentRightColumn li").each((_, el) => {
const link = $(el).find("a"); const link = $(el).find("a");
@@ -30,8 +31,10 @@ export default async function parseTeachers() {
const text = link.text().trim(); // e.g. "Ing. Bc. Šárka Páltiková" const text = link.text().trim(); // e.g. "Ing. Bc. Šárka Páltiková"
if (href) { if (href) {
const key = href.split("/").pop().toLowerCase(); // get "pa" const key = href.split("/").pop()?.toLowerCase(); // get "pa"
map[key] = text; if (key) {
map[key] = text;
}
} }
}); });

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

@@ -12,7 +12,7 @@
* GNU General Public License for more details. * GNU General Public License for more details.
*/ */
import express from "express"; import express, { Request, Response } from "express";
import path from "path"; import path from "path";
const app = express(); const app = express();
import fs from "fs/promises"; import fs from "fs/promises";
@@ -20,9 +20,10 @@ import { getCurrentInterval } from "./scheduleRules.js";
import bodyParser from "body-parser"; import bodyParser from "body-parser";
import cors from "cors"; import cors from "cors";
const VERSIONS = ["v1", "v2"]; const VERSIONS = ["v1", "v2", "v3"];
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
// @ts-ignore
globalThis.File = class File {}; globalThis.File = class File {};
app.use(bodyParser.json()); app.use(bodyParser.json());
@@ -33,7 +34,7 @@ app.use(cors({
allowedHeaders: ["Content-Type"], allowedHeaders: ["Content-Type"],
})); }));
app.get('/', async (req, res) => { app.get('/', async (req: Request, res: Response) => {
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);
@@ -49,8 +50,17 @@ app.get('/', async (req, res) => {
} }
}); });
app.get('/versioned/v1', async (_: Request, res: Response) => {
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 (_: Request, res: Response) => {
try { try {
const filePath = path.join(process.cwd(), "db", `${version}.json`); const filePath = path.join(process.cwd(), "db", `${version}.json`);
const dataStr = await fs.readFile(filePath, "utf8"); const dataStr = await fs.readFile(filePath, "utf8");
@@ -66,7 +76,7 @@ VERSIONS.forEach((version) => {
}); });
}); });
app.get("/status", async (_, res) => { app.get("/status", async (_: Request, res: Response) => {
const dataStr = await fs.readFile(path.resolve("./volume/customState.json"), {encoding: "utf8"}); const dataStr = await fs.readFile(path.resolve("./volume/customState.json"), {encoding: "utf8"});
const data = JSON.parse(dataStr); const data = JSON.parse(dataStr);
@@ -77,17 +87,22 @@ app.get("/status", async (_, res) => {
} }
}) })
app.post("/report", async (req, res) => { app.post("/report", async (req: Request, res: Response): Promise<any> => {
const { class: className, location, content } = req.body; const { class: className, location, content } = req.body;
if (!className || !location || !content) { if (!className || !location || !content) {
return res.status(400).json({ error: "Missing required fields." }); return res.status(400).json({ error: "Missing required fields." });
} }
if (!["TIMETABLE", "ABSENCES", "OTHER"].includes(location)) { if (!["TIMETABLE", "ABSENCES", "ABSENCE", "TAKES_PLACE", "OTHER"].includes(location)) {
return res.status(400).json({ error: "Invalid location value." }); return res.status(400).json({ error: "Invalid location value." });
} }
const url = process.env.REPORT_WEBHOOK_URL; const url = process.env.REPORT_WEBHOOK_URL;
if (!url) {
console.error("REPORT_WEBHOOK_URL is not set.");
return res.status(500).json({ error: "Server configuration error." });
}
let resp; let resp;
try { try {
resp = await fetch(url, { resp = await fetch(url, {
@@ -97,7 +112,7 @@ app.post("/report", async (req, res) => {
}, },
body: `${content}\n\nClass: ${className}\nLocation: ${location}`, body: `${content}\n\nClass: ${className}\nLocation: ${location}`,
}); });
} catch (err) { } catch (err: any) {
console.error('Fetch failed:', err.message); console.error('Fetch failed:', err.message);
console.error(err); console.error(err);
throw err; throw err;

View File

@@ -15,7 +15,7 @@
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: Record<string, string> = JSON.parse(fs.readFileSync("./tests/teachermap.json", "utf8"));
test("Me", [ test("Me", [
{ {
@@ -232,6 +232,7 @@ test("za Vn zastupuje Jk", [
teacher: "Ing. Zdeněk Vondra", teacher: "Ing. Zdeněk Vondra",
teacherCode: "vn", teacherCode: "vn",
type: "zastoupen", type: "zastoupen",
hours: null,
zastupuje: { zastupuje: {
teacher: "David Janoušek", teacher: "David Janoušek",
teacherCode: "jk", teacherCode: "jk",
@@ -248,18 +249,27 @@ test("Vc 1. h", [
}, },
]); ]);
function test(input, expectedOutput) { test("Sv-5-exk", [
{
teacher: "Ing. Jan Šváb",
teacherCode: "sv",
type: "exkurze",
hours: { from: 5, to: 10 },
},
]);
function test(input: string, expectedOutput: any[]) {
const res = parseAbsence(input, teachermap); const res = parseAbsence(input, teachermap);
if (!deepEqual(res, expectedOutput)) { if (!deepEqual(res, expectedOutput)) {
console.error("ERROR for input: " + input); console.error("ERROR for input: " + input);
console.log(res); console.log(JSON.stringify(res, null, 2));
console.log(expectedOutput); console.log(JSON.stringify(expectedOutput, null, 2));
console.log(); console.log();
} }
} }
function deepEqual(a, b) { function deepEqual(a: any, b: any): boolean {
if (a === b) return true; if (a === b) return true;
if ( if (

20
tsconfig.json Normal file
View File

@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "./dist",
"rootDir": "./"
},
"include": [
"./**/*.ts"
],
"exclude": [
"node_modules",
"web"
]
}