1
0

refactor: Rewrite to typescript
All checks were successful
Remote Deploy / deploy (push) Successful in 14s

This commit is contained in:
2026-02-11 08:20:56 +01:00
parent 138fa17e54
commit ae17dc241a
14 changed files with 941 additions and 121 deletions

View File

@@ -16,7 +16,7 @@ import parseV1V2 from "./parse/v1_v2.js";
import parseV3 from "./parse/v3.js";
export default async function parseThisShit(downloadedFilePath) {
export default async function parseThisShit(downloadedFilePath: string) {
await parseV1V2(downloadedFilePath);
await parseV3(downloadedFilePath);
}

View File

@@ -12,12 +12,22 @@
* GNU General Public License for more details.
*/
import ExcelJS from "exceljs"
import ExcelJS, { Worksheet, Cell } from "exceljs"
import fs from "fs"
import parseAbsence from "../utils/parseAbsence.js"
import parseAbsence, { AbsenceResult } from "../utils/parseAbsence.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();
await workbook.xlsx.readFile(downloadedFilePath);
const teacherMap = await parseTeachers();
@@ -32,7 +42,7 @@ export default async function parseV1V2(downloadedFilePath) {
const today = getCurrentDateObject();
const datedSheets = [];
const datedSheets: DatedSheet[] = [];
for (const sheet of workbook.worksheets) {
const match = sheet.name.toLowerCase().match(dateRegex);
@@ -53,7 +63,7 @@ export default async function parseV1V2(downloadedFilePath) {
});
}
const sheetsByDate = {};
const sheetsByDate: Record<string, Worksheet[]> = {};
for (const item of datedSheets) {
sheetsByDate[item.dateKey] ??= [];
sheetsByDate[item.dateKey].push(item.sheet);
@@ -61,20 +71,23 @@ export default async function parseV1V2(downloadedFilePath) {
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 found = sheets.find((s) => s.state !== "hidden");
return (found ?? sheets[0]).name;
});
const final = [];
const final: ScheduleDay[] = [];
let finalIndex = 0
for (const key of upcomingSheets) {
const currentSheet = workbook.getWorksheet(key);
if (!currentSheet) continue;
final.push({});
const regex = /[AEC][0-4][a-c]?\s*\/.*/s;
const prefixRegex = /[AEC][0-4][a-c]?/;
const classes = [];
const matchingKeys = [];
const classes: string[] = [];
const matchingKeys: string[] = [];
currentSheet.eachRow((row) => {
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);
}
@@ -104,17 +117,20 @@ export default async function parseV1V2(downloadedFilePath) {
for (const matchingKey of matchingKeys) {
const matchingCell = currentSheet.getCell(matchingKey);
const rowNumber = matchingCell.row;
const allKeys = [];
const allKeys: string[] = [];
// Get all cells in the same row
const row = currentSheet.getRow(rowNumber);
const row = currentSheet.getRow(Number(rowNumber));
row.eachCell((cell) => {
if (cell.address !== matchingKey) {
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) {
const cell = currentSheet.getCell(key);
@@ -124,13 +140,16 @@ export default async function parseV1V2(downloadedFilePath) {
try {
const regex = /^úklid\s+(?:\d+\s+)?[A-Za-z]{2}$/;
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;
}
} catch {}
if (d) {
let text = cell.text;
// @ts-ignore
if (cell.fill?.fgColor?.argb == "FFFFFF00") {
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));
while (final2.length < 10) {
final2.push(null);
const final2Array = Array.from(final2, (item) => (item === undefined ? null : item));
while (final2Array.length < 10) {
final2Array.push(null);
}
final[finalIndex][classes[classI]] = final2.slice(1, 11);
final[finalIndex][classes[classI]] = final2Array.slice(1, 11);
classI++;
}
// ABSENCE
final[finalIndex]["ABSENCE"] = [];
let absenceKey = null;
let absenceKey: string | null = null;
currentSheet.eachRow((row) => {
row.eachCell((cell) => {
@@ -166,13 +185,13 @@ export default async function parseV1V2(downloadedFilePath) {
if (absenceKey) {
const absenceCell = currentSheet.getCell(absenceKey);
const rowNumber = absenceCell.row;
const allAbsenceKeys = [];
const allAbsenceKeys: string[] = [];
// Get all cells in the same row as absence
const row = currentSheet.getRow(rowNumber);
const row = currentSheet.getRow(Number(rowNumber));
row.eachCell((cell) => {
if (cell.address !== absenceKey) {
allAbsenceKeys.push(cell.address);
if (cell.address !== absenceKey) { // absenceKey is checked above to be non-null
allAbsenceKeys.push(cell.address);
}
})
@@ -189,7 +208,9 @@ export default async function parseV1V2(downloadedFilePath) {
}
const data = parseAbsence(value, teacherMap);
final[finalIndex]["ABSENCE"].push(...data);
if (final[finalIndex]["ABSENCE"]) {
final[finalIndex]["ABSENCE"]!.push(...data);
}
}
}
@@ -237,10 +258,10 @@ export default async function parseV1V2(downloadedFilePath) {
// Modify the data for v1
const copy = JSON.parse(JSON.stringify(data));
copy.schedule.forEach(day => {
copy.schedule.forEach((day: ScheduleDay) => {
if (!Array.isArray(day.ABSENCE)) return;
day.ABSENCE = day.ABSENCE.map(old => {
day.ABSENCE = day.ABSENCE.map((old: any) => {
if (old.type === "zastoupen") {
return {
type: "invalid",

View File

@@ -15,14 +15,30 @@
import fs from "fs";
import parseAbsence from "../utils/parseAbsence.js"
import parseTeachers from "../utils/parseTeachers.js"
import ExcelJS from "exceljs"
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) {
async function getThemeColors(filePath: string): Promise<ThemeColors | null> {
const data = fs.readFileSync(filePath);
const zip = await JSZip.loadAsync(data);
@@ -39,13 +55,13 @@ async function getThemeColors(filePath) {
if (!scheme) return null;
function getColor(node) {
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 = {
const colors: ThemeColors = {
0: getColor(scheme["a:dk1"]?.[0]),
1: getColor(scheme["a:lt1"]?.[0]),
2: getColor(scheme["a:dk2"]?.[0]),
@@ -64,12 +80,12 @@ async function getThemeColors(filePath) {
/**
* Apply Excel tint to a base hex color
*/
function applyTintToHex(hex, tint = 0) {
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) =>
const tintChannel = (c: number) =>
tint > 0 ? Math.round(c + (255 - c) * tint) : Math.round(c * (1 + tint));
const nr = tintChannel(r);
@@ -85,9 +101,11 @@ function applyTintToHex(hex, tint = 0) {
/**
* Resolve final hex for a cell fill
*/
function resolveCellColor(cell, themeColors) {
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}`;
@@ -100,7 +118,7 @@ function resolveCellColor(cell, themeColors) {
return null;
}
export default async function parseV3(downloadedFilePath) {
export default async function parseV3(downloadedFilePath: string) {
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(downloadedFilePath);
const themeColors = await getThemeColors(downloadedFilePath);
@@ -110,7 +128,7 @@ export default async function parseV3(downloadedFilePath) {
const upcoming = getUpcomingSheets(workbook);
const resolvedDays = groupSheetsByDate(upcoming);
const schedule = {};
const schedule: any = {};
for (const { dateKey, sheet } of resolvedDays) {
const { changes, absence, inWork, takesPlace, reservedRooms } = extractDaySchedule(sheet, teacherMap, themeColors);
@@ -138,13 +156,13 @@ export default async function parseV3(downloadedFilePath) {
// ────────────────────────────────────────────────────────────
//
function getUpcomingSheets(workbook) {
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 = [];
const result: ResolvedDay[] = [];
for (const sheet of workbook.worksheets) {
const match = sheet.name.toLowerCase().match(dateRegex);
@@ -164,8 +182,8 @@ function getUpcomingSheets(workbook) {
return result;
}
function groupSheetsByDate(items) {
const map = {};
function groupSheetsByDate(items: ResolvedDay[]) {
const map: Record<string, Worksheet[]> = {};
for (const item of items) {
map[item.dateKey] ??= [];
@@ -188,7 +206,7 @@ function groupSheetsByDate(items) {
// ────────────────────────────────────────────────────────────
//
function extractDaySchedule(sheet, teacherMap, themeColors) {
function extractDaySchedule(sheet: Worksheet, teacherMap: Record<string, string>, themeColors: ThemeColors | null) {
return {
changes: extractClassChanges(sheet, themeColors),
absence: extractAbsence(sheet, teacherMap),
@@ -198,7 +216,7 @@ function extractDaySchedule(sheet, teacherMap, themeColors) {
};
}
function isPripravaSheet(name) {
function isPripravaSheet(name: string) {
return name
.toLowerCase()
.normalize("NFD")
@@ -212,12 +230,12 @@ function isPripravaSheet(name) {
// ────────────────────────────────────────────────────────────
//
function extractClassChanges(sheet, themeColors) {
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 = [];
const classCells = [];
const classes: string[] = [];
const classCells: string[] = [];
sheet.eachRow((row) => {
row.eachCell((cell) => {
@@ -230,18 +248,18 @@ function extractClassChanges(sheet, themeColors) {
});
});
const changes = {};
const changes: Record<string, (Lesson | null)[]> = {};
classCells.forEach((address, index) => {
const row = sheet.getRow(sheet.getCell(address).row);
const row = sheet.getRow(Number(sheet.getCell(address).row));
changes[classes[index]] = buildLessonArray(row, address, themeColors);
});
return changes;
}
function buildLessonArray(row, ignoreAddress, themeColors) {
const lessons = [];
function buildLessonArray(row: Row, ignoreAddress: string, themeColors: ThemeColors | null) {
const lessons: (Lesson | null)[] = [];
row.eachCell((cell) => {
if (cell.address === ignoreAddress) return;
@@ -256,11 +274,12 @@ function buildLessonArray(row, ignoreAddress, themeColors) {
return normalized.slice(1, 11);
}
function parseLessonCell(cell, themeColors) {
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);
@@ -272,6 +291,7 @@ function parseLessonCell(cell, themeColors) {
text,
backgroundColor,
foregroundColor,
// @ts-ignore
willBeSpecified: cell.fill.fgColor.argb === "FFFFFF00" ? true : undefined,
};
} catch {
@@ -280,7 +300,7 @@ function parseLessonCell(cell, themeColors) {
}
function extractTakesPlace(sheet) {
function extractTakesPlace(sheet: Worksheet) {
const cell = sheet.getCell("B4");
if (!cell.isMerged) {
@@ -296,7 +316,7 @@ function extractTakesPlace(sheet) {
for (const cellTest of tryCells) {
const cellTry = sheet.getCell(`${cellTest}${i}`)
const cellValue = cellTry?.value?.trim() || "";
const cellValue = (typeof cellTry?.value === 'string' ? cellTry.value.trim() : "") || "";
if (cellValue.length >= threshold) {
str += `\n${cellValue}`;
@@ -316,10 +336,10 @@ function extractTakesPlace(sheet) {
return str.trim();
}
function extractReservedRooms(sheet) {
const result = [];
function extractReservedRooms(sheet: Worksheet) {
const result: (string | null)[] = [];
const cells = [];
const cells: string[] = [];
sheet.eachRow((row) => {
row.eachCell((cell) => {
@@ -331,12 +351,13 @@ function extractReservedRooms(sheet) {
});
cells.forEach((address) => {
const row = sheet.getRow(sheet.getCell(address).row);
const row = sheet.getRow(Number(sheet.getCell(address).row));
row.eachCell((cell) => {
if (cell.address === address) return;
result.push(cell.value?.trim().length == 0 ? null : cell.value)
const val = cell.value?.toString().trim();
result.push(!val || val.length == 0 ? null : val)
});
});
@@ -353,8 +374,8 @@ function extractReservedRooms(sheet) {
// ────────────────────────────────────────────────────────────
//
function extractAbsence(sheet, teacherMap) {
let absenceAddress = null;
function extractAbsence(sheet: Worksheet, teacherMap: Record<string, string>) {
let absenceAddress: string | null = null;
sheet.eachRow((row) => {
row.eachCell((cell) => {
@@ -366,8 +387,8 @@ function extractAbsence(sheet, teacherMap) {
if (!absenceAddress) return [];
const row = sheet.getRow(sheet.getCell(absenceAddress).row);
const results = [];
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) => {
@@ -390,7 +411,7 @@ function extractAbsence(sheet, teacherMap) {
// ────────────────────────────────────────────────────────────
//
function letterToNumber(letter) {
function letterToNumber(letter: string) {
return letter.toLowerCase().charCodeAt(0) - 97;
}
@@ -403,4 +424,4 @@ function formatNowTime() {
);
}
//parseV3("db/current.xlsx")
parseV3("db/current.xlsx")

View File

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

View File

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

View File

@@ -14,15 +14,16 @@
import * as cheerio from "cheerio";
// @ts-ignore
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 response = await fetch(url);
const data = await response.text();
const $ = cheerio.load(data);
const map = {};
const map: Record<string, string> = {};
$("main .contentLeftColumn li, main .contentRightColumn li").each((_, el) => {
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á"
if (href) {
const key = href.split("/").pop().toLowerCase(); // get "pa"
map[key] = text;
const key = href.split("/").pop()?.toLowerCase(); // get "pa"
if (key) {
map[key] = text;
}
}
});