From ae17dc241a57e3cd89cc789b7bdc2e45354f087c Mon Sep 17 00:00:00 2001 From: jzitnik-dev Date: Wed, 11 Feb 2026 08:20:56 +0100 Subject: [PATCH] refactor: Rewrite to typescript --- .gitignore | 1 + cron-runner.js => cron-runner.ts | 22 +- package-lock.json | 714 +++++++++++++++++- package.json | 18 +- scheduleRules.js => scheduleRules.ts | 12 +- scrape/{parse.js => parse.ts} | 2 +- scrape/parse/{v1_v2.js => v1_v2.ts} | 73 +- scrape/parse/{v3.js => v3.ts} | 93 ++- scrape/{scraper.js => scraper.ts} | 22 +- .../{parseAbsence.js => parseAbsence.ts} | 43 +- .../{parseTeachers.js => parseTeachers.ts} | 11 +- server.js => server.ts | 20 +- tests/{test.js => test.ts} | 11 +- tsconfig.json | 20 + 14 files changed, 941 insertions(+), 121 deletions(-) rename cron-runner.js => cron-runner.ts (71%) rename scheduleRules.js => scheduleRules.ts (85%) rename scrape/{parse.js => parse.ts} (90%) rename scrape/parse/{v1_v2.js => v1_v2.ts} (76%) rename scrape/parse/{v3.js => v3.ts} (81%) rename scrape/{scraper.js => scraper.ts} (90%) rename scrape/utils/{parseAbsence.js => parseAbsence.ts} (81%) rename scrape/utils/{parseTeachers.js => parseTeachers.ts} (80%) rename server.js => server.ts (85%) rename tests/{test.js => test.ts} (94%) create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 588021e..a51b950 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ volume/browser db downloads errors +dist # Web web/public diff --git a/cron-runner.js b/cron-runner.ts similarity index 71% rename from cron-runner.js rename to cron-runner.ts index ba311b6..f2232bd 100644 --- a/cron-runner.js +++ b/cron-runner.ts @@ -13,11 +13,25 @@ */ import cron from 'node-cron'; 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() { 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) { console.error(`Scraper error: ${error.message}`); return; @@ -27,11 +41,11 @@ function runScraper() { }); } -function createSchedules(rules) { +function createSchedules(rules: ScheduleRule[]) { rules.forEach(rule => { const startMin = toMinutes(rule.start); 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; for (let t = startMin; t < adjustedEnd; t += rule.interval) { diff --git a/package-lock.json b/package-lock.json index 84b557f..0b9b64b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,17 @@ "node-cron": "^4.2.1", "puppeteer": "^24.10.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" } }, "node_modules/@babel/code-frame": { @@ -45,6 +56,448 @@ "node": ">=6.9.0" } }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.27.3.tgz", + "integrity": "sha512-9fJMTNFTWZMh5qwrBItuziu834eOCUcEqymSH7pY+zoMVEZg3gcPuBNxH1EvfVYe9h0x/Ptw8KBzv7qxb7l8dg==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.27.3.tgz", + "integrity": "sha512-i5D1hPY7GIQmXlXhs2w8AWHhenb00+GxjxRncS2ZM7YNVGNfaMxgzSGuO8o8SJzRc/oZwU2bcScvVERk03QhzA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.27.3.tgz", + "integrity": "sha512-YdghPYUmj/FX2SYKJ0OZxf+iaKgMsKHVPF1MAq/P8WirnSpCStzKJFjOjzsW0QQ7oIAiccHdcqjbHmJxRb/dmg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.27.3.tgz", + "integrity": "sha512-IN/0BNTkHtk8lkOM8JWAYFg4ORxBkZQf9zXiEOfERX/CzxW3Vg1ewAhU7QSWQpVIzTW+b8Xy+lGzdYXV6UZObQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.27.3.tgz", + "integrity": "sha512-Re491k7ByTVRy0t3EKWajdLIr0gz2kKKfzafkth4Q8A5n1xTHrkqZgLLjFEHVD+AXdUGgQMq+Godfq45mGpCKg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.27.3.tgz", + "integrity": "sha512-vHk/hA7/1AckjGzRqi6wbo+jaShzRowYip6rt6q7VYEDX4LEy1pZfDpdxCBnGtl+A5zq8iXDcyuxwtv3hNtHFg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.27.3.tgz", + "integrity": "sha512-ipTYM2fjt3kQAYOvo6vcxJx3nBYAzPjgTCk7QEgZG8AUO3ydUhvelmhrbOheMnGOlaSFUoHXB6un+A7q4ygY9w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.27.3.tgz", + "integrity": "sha512-dDk0X87T7mI6U3K9VjWtHOXqwAMJBNN2r7bejDsc+j03SEjtD9HrOl8gVFByeM0aJksoUuUVU9TBaZa2rgj0oA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.27.3.tgz", + "integrity": "sha512-s6nPv2QkSupJwLYyfS+gwdirm0ukyTFNl3KTgZEAiJDd+iHZcbTPPcWCcRYH+WlNbwChgH2QkE9NSlNrMT8Gfw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.27.3.tgz", + "integrity": "sha512-sZOuFz/xWnZ4KH3YfFrKCf1WyPZHakVzTiqji3WDc0BCl2kBwiJLCXpzLzUBLgmp4veFZdvN5ChW4Eq/8Fc2Fg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.27.3.tgz", + "integrity": "sha512-yGlQYjdxtLdh0a3jHjuwOrxQjOZYD/C9PfdbgJJF3TIZWnm/tMd/RcNiLngiu4iwcBAOezdnSLAwQDPqTmtTYg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.27.3.tgz", + "integrity": "sha512-WO60Sn8ly3gtzhyjATDgieJNet/KqsDlX5nRC5Y3oTFcS1l0KWba+SEa9Ja1GfDqSF1z6hif/SkpQJbL63cgOA==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.27.3.tgz", + "integrity": "sha512-APsymYA6sGcZ4pD6k+UxbDjOFSvPWyZhjaiPyl/f79xKxwTnrn5QUnXR5prvetuaSMsb4jgeHewIDCIWljrSxw==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.27.3.tgz", + "integrity": "sha512-eizBnTeBefojtDb9nSh4vvVQ3V9Qf9Df01PfawPcRzJH4gFSgrObw+LveUyDoKU3kxi5+9RJTCWlj4FjYXVPEA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.27.3.tgz", + "integrity": "sha512-3Emwh0r5wmfm3ssTWRQSyVhbOHvqegUDRd0WhmXKX2mkHJe1SFCMJhagUleMq+Uci34wLSipf8Lagt4LlpRFWQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.27.3.tgz", + "integrity": "sha512-pBHUx9LzXWBc7MFIEEL0yD/ZVtNgLytvx60gES28GcWMqil8ElCYR4kvbV2BDqsHOvVDRrOxGySBM9Fcv744hw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.27.3.tgz", + "integrity": "sha512-Czi8yzXUWIQYAtL/2y6vogER8pvcsOsk5cpwL4Gk5nJqH5UZiVByIY8Eorm5R13gq+DQKYg0+JyQoytLQas4dA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.27.3.tgz", + "integrity": "sha512-sDpk0RgmTCR/5HguIZa9n9u+HVKf40fbEUt+iTzSnCaGvY9kFP0YKBWZtJaraonFnqef5SlJ8/TiPAxzyS+UoA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.27.3.tgz", + "integrity": "sha512-P14lFKJl/DdaE00LItAukUdZO5iqNH7+PjoBm+fLQjtxfcfFE20Xf5CrLsmZdq5LFFZzb5JMZ9grUwvtVYzjiA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.27.3.tgz", + "integrity": "sha512-AIcMP77AvirGbRl/UZFTq5hjXK+2wC7qFRGoHSDrZ5v5b8DK/GYpXW3CPRL53NkvDqb9D+alBiC/dV0Fb7eJcw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.27.3.tgz", + "integrity": "sha512-DnW2sRrBzA+YnE70LKqnM3P+z8vehfJWHXECbwBmH/CU51z6FiqTQTHFenPlHmo3a8UgpLyH3PT+87OViOh1AQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.27.3.tgz", + "integrity": "sha512-NinAEgr/etERPTsZJ7aEZQvvg/A6IsZG/LgZy+81wON2huV7SrK3e63dU0XhyZP4RKGyTm7aOgmQk0bGp0fy2g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.27.3.tgz", + "integrity": "sha512-PanZ+nEz+eWoBJ8/f8HKxTTD172SKwdXebZ0ndd953gt1HRBbhMsaNqjTyYLGLPdoWHy4zLU7bDVJztF5f3BHA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.27.3.tgz", + "integrity": "sha512-B2t59lWWYrbRDw/tjiWOuzSsFh1Y/E95ofKz7rIVYSQkUYBjfSgf6oeYPNWHToFRr2zx52JKApIcAS/D5TUBnA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.27.3.tgz", + "integrity": "sha512-QLKSFeXNS8+tHW7tZpMtjlNb7HKau0QDpwm49u0vUp9y1WOF+PEzkU84y9GqYaAVW8aH8f3GcBck26jh54cX4Q==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.27.3.tgz", + "integrity": "sha512-4uJGhsxuptu3OcpVAzli+/gWusVGwZZHTlS63hh++ehExkVT8SgiEf7/uC/PclrPPkLhZqGgCTjd0VWLo6xMqA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, "node_modules/@fast-csv/format": { "version": "4.3.5", "resolved": "https://registry.npmjs.org/@fast-csv/format/-/format-4.3.5.tgz", @@ -113,14 +566,139 @@ "integrity": "sha512-C5Mc6rdnsaJDjO3UpGW/CQTHtCKaYlScZTly4JIu97Jxo/odCiH0ITnDXSJPTOrEKk/ycSZ0AOgTmkDtkOsvIA==", "license": "MIT" }, - "node_modules/@types/node": { - "version": "22.15.29", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.15.29.tgz", - "integrity": "sha512-LNdjOkUDlU1RZb8e1kOIUpN1qQUlzGkEtbVNo53vbrwDg5om6oduhm4SiUaPW5ASTXhAiP0jInWG8Qx9fVlOeQ==", + "node_modules/@types/body-parser": { + "version": "1.19.6", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.6.tgz", + "integrity": "sha512-HLFeCYgz89uk22N5Qg3dvGvsv46B8GLvKKo1zKG4NybA8U2DiEO3w9lqGg29t/tfLRJpJ6iQxnVw4OnB7MoM9g==", + "dev": true, "license": "MIT", - "optional": true, "dependencies": { - "undici-types": "~6.21.0" + "@types/connect": "*", + "@types/node": "*" + } + }, + "node_modules/@types/connect": { + "version": "3.4.38", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz", + "integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/cors": { + "version": "2.8.19", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.19.tgz", + "integrity": "sha512-mFNylyeyqN93lfe/9CSxOGREz8cpzAhH+E93xJ4xWQf62V8sQ/24reV2nyzUWM6H6Xji+GGHpkbLe7pVoUEskg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/express": { + "version": "5.0.6", + "resolved": "https://registry.npmjs.org/@types/express/-/express-5.0.6.tgz", + "integrity": "sha512-sKYVuV7Sv9fbPIt/442koC7+IIwK5olP1KWeD88e/idgoJqDm3JV/YUiPwkoKK92ylff2MGxSz1CSjsXelx0YA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^5.0.0", + "@types/serve-static": "^2" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-5.1.1.tgz", + "integrity": "sha512-v4zIMr/cX7/d2BpAEX3KNKL/JrT1s43s96lLvvdTmza1oEvDudCqK9aF/djc/SWgy8Yh0h30TZx5VpzqFCxk5A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*", + "@types/send": "*" + } + }, + "node_modules/@types/http-errors": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.5.tgz", + "integrity": "sha512-r8Tayk8HJnX0FztbZN7oVqGccWgw98T/0neJphO91KkmOzug1KkofZURD4UaD5uH8AqcFLfdPErnBod0u71/qg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/jszip": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/@types/jszip/-/jszip-3.4.0.tgz", + "integrity": "sha512-GFHqtQQP3R4NNuvZH3hNCYD0NbyBZ42bkN7kO3NDrU/SnvIZWMS8Bp38XCsRKBT5BXvgm0y1zqpZWp/ZkRzBzg==", + "dev": true, + "license": "MIT", + "dependencies": { + "jszip": "*" + } + }, + "node_modules/@types/node": { + "version": "25.2.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-25.2.3.tgz", + "integrity": "sha512-m0jEgYlYz+mDJZ2+F4v8D1AyQb+QzsNqRuI7xg1VQX/KlKS0qT9r1Mo16yo5F/MtifXFgaofIFsdFMox2SxIbQ==", + "devOptional": true, + "license": "MIT", + "dependencies": { + "undici-types": "~7.16.0" + } + }, + "node_modules/@types/node-cron": { + "version": "3.0.11", + "resolved": "https://registry.npmjs.org/@types/node-cron/-/node-cron-3.0.11.tgz", + "integrity": "sha512-0ikrnug3/IyneSHqCBeslAhlK2aBfYek1fGo4bP4QnZPmiqSGRK+Oy7ZMisLWkesffJvQ1cqAcBnJC+8+nxIAg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/qs": { + "version": "6.14.0", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.14.0.tgz", + "integrity": "sha512-eOunJqu0K1923aExK6y8p6fsihYEn/BYuQ4g0CxAAgFc4b/ZLN4CrsRZ55srTdqoiLzU2B2evC+apEIxprEzkQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/range-parser": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz", + "integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/send": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@types/send/-/send-1.2.1.tgz", + "integrity": "sha512-arsCikDvlU99zl1g69TcAB3mzZPpxgw0UQnaHeC1Nwb015xp8bknZv5rIfri9xTOcMuaVgvabfIRA7PSZVuZIQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/serve-static": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-2.2.0.tgz", + "integrity": "sha512-8mam4H1NHLtu7nmtalF7eyBH14QyOASmcxHhSfEoRyr0nP/YdoesEtU+uSRvMe96TW/HPTtkoKqQLl53N7UXMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/http-errors": "*", + "@types/node": "*" + } + }, + "node_modules/@types/xml2js": { + "version": "0.4.14", + "resolved": "https://registry.npmjs.org/@types/xml2js/-/xml2js-0.4.14.tgz", + "integrity": "sha512-4YnrRemBShWRO2QjvUin8ESA41rH+9nQGLUGZV/1IDhi3SL9OhdpNC/MrulTWuptXKwhx/aDxE7toV0f/ypIXQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" } }, "node_modules/@types/yauzl": { @@ -1192,6 +1770,48 @@ "node": ">= 0.4" } }, + "node_modules/esbuild": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.3.tgz", + "integrity": "sha512-8VwMnyGCONIs6cWue2IdpHxHnAjzxnw2Zr7MkVxB2vjmQ2ivqGFb4LEG3SMnv0Gb2F/G/2yA8zUaiL1gywDCCg==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.27.3", + "@esbuild/android-arm": "0.27.3", + "@esbuild/android-arm64": "0.27.3", + "@esbuild/android-x64": "0.27.3", + "@esbuild/darwin-arm64": "0.27.3", + "@esbuild/darwin-x64": "0.27.3", + "@esbuild/freebsd-arm64": "0.27.3", + "@esbuild/freebsd-x64": "0.27.3", + "@esbuild/linux-arm": "0.27.3", + "@esbuild/linux-arm64": "0.27.3", + "@esbuild/linux-ia32": "0.27.3", + "@esbuild/linux-loong64": "0.27.3", + "@esbuild/linux-mips64el": "0.27.3", + "@esbuild/linux-ppc64": "0.27.3", + "@esbuild/linux-riscv64": "0.27.3", + "@esbuild/linux-s390x": "0.27.3", + "@esbuild/linux-x64": "0.27.3", + "@esbuild/netbsd-arm64": "0.27.3", + "@esbuild/netbsd-x64": "0.27.3", + "@esbuild/openbsd-arm64": "0.27.3", + "@esbuild/openbsd-x64": "0.27.3", + "@esbuild/openharmony-arm64": "0.27.3", + "@esbuild/sunos-x64": "0.27.3", + "@esbuild/win32-arm64": "0.27.3", + "@esbuild/win32-ia32": "0.27.3", + "@esbuild/win32-x64": "0.27.3" + } + }, "node_modules/escalade": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz", @@ -1446,6 +2066,21 @@ "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "license": "ISC" }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/fstream": { "version": "1.0.12", "resolved": "https://registry.npmjs.org/fstream/-/fstream-1.0.12.tgz", @@ -1532,6 +2167,19 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/get-tsconfig": { + "version": "4.13.6", + "resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.13.6.tgz", + "integrity": "sha512-shZT/QMiSHc/YBLxxOkMtgSid5HFoauqCE3/exfsEcwg1WkeqjG+V40yBbBrsD+jW2HDXcs28xOfcbm2jI8Ddw==", + "dev": true, + "license": "MIT", + "dependencies": { + "resolve-pkg-maps": "^1.0.0" + }, + "funding": { + "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1" + } + }, "node_modules/get-uri": { "version": "6.0.4", "resolved": "https://registry.npmjs.org/get-uri/-/get-uri-6.0.4.tgz", @@ -2572,6 +3220,16 @@ "node": ">=4" } }, + "node_modules/resolve-pkg-maps": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz", + "integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1" + } + }, "node_modules/rimraf": { "version": "2.7.1", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", @@ -3025,6 +3683,26 @@ "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "license": "0BSD" }, + "node_modules/tsx": { + "version": "4.21.0", + "resolved": "https://registry.npmjs.org/tsx/-/tsx-4.21.0.tgz", + "integrity": "sha512-5C1sg4USs1lfG0GFb2RLXsdpXqBSEhAaA/0kPL01wxzpMqLILNxIxIOKiILz+cdg/pLnOUxFYOR5yhHU666wbw==", + "dev": true, + "license": "MIT", + "dependencies": { + "esbuild": "~0.27.0", + "get-tsconfig": "^4.7.5" + }, + "bin": { + "tsx": "dist/cli.mjs" + }, + "engines": { + "node": ">=18.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + } + }, "node_modules/type-is": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.0.1.tgz", @@ -3066,6 +3744,20 @@ "integrity": "sha512-SbklCd1F0EiZOyPiW192rrHZzZ5sBijB6xM+cpmrwDqObvdtunOHHIk9fCGsoK5JVIYXoyEp4iEdE3upFH3PAg==", "license": "MIT" }, + "node_modules/typescript": { + "version": "5.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", + "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", + "devOptional": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/undici": { "version": "7.15.0", "resolved": "https://registry.npmjs.org/undici/-/undici-7.15.0.tgz", @@ -3076,11 +3768,11 @@ } }, "node_modules/undici-types": { - "version": "6.21.0", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", - "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", - "license": "MIT", - "optional": true + "version": "7.16.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz", + "integrity": "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==", + "devOptional": true, + "license": "MIT" }, "node_modules/unpipe": { "version": "1.0.0", diff --git a/package.json b/package.json index 302de91..b8d8179 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,10 @@ "type": "module", "main": "server.js", "scripts": { - "test": "node tests/test.js", - "start": "concurrently \"node server.js\" \"node cron-runner.js\"", - "build": "cd web && hugo --gc --minify", + "test": "tsx tests/test.ts", + "start": "concurrently \"NODE_ENV=development tsx server.ts\" \"NODE_ENV=development tsx cron-runner.ts\"", + "build": "tsc && cd web && hugo --gc --minify", + "serve": "concurrently \"node dist/server.js\" \"node dist/cron-runner.js\"", "dev-web": "cd web && hugo serve" }, "dependencies": { @@ -24,5 +25,16 @@ "node-cron": "^4.2.1", "puppeteer": "^24.10.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" } } diff --git a/scheduleRules.js b/scheduleRules.ts similarity index 85% rename from scheduleRules.js rename to scheduleRules.ts index b0ec9fb..02c775f 100644 --- a/scheduleRules.js +++ b/scheduleRules.ts @@ -13,7 +13,13 @@ */ // 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: "3:00", end: "4:00", interval: 60 }, { start: "5:00", end: "6:00", interval: 30 }, @@ -23,12 +29,12 @@ export const scheduleRules = [ { 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); 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(); for (const rule of scheduleRules) { diff --git a/scrape/parse.js b/scrape/parse.ts similarity index 90% rename from scrape/parse.js rename to scrape/parse.ts index 424ff77..326df04 100644 --- a/scrape/parse.js +++ b/scrape/parse.ts @@ -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); } diff --git a/scrape/parse/v1_v2.js b/scrape/parse/v1_v2.ts similarity index 76% rename from scrape/parse/v1_v2.js rename to scrape/parse/v1_v2.ts index 9d3a74d..c406a4d 100644 --- a/scrape/parse/v1_v2.js +++ b/scrape/parse/v1_v2.ts @@ -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 = {}; 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", diff --git a/scrape/parse/v3.js b/scrape/parse/v3.ts similarity index 81% rename from scrape/parse/v3.js rename to scrape/parse/v3.ts index f344f95..dd0bbee 100644 --- a/scrape/parse/v3.js +++ b/scrape/parse/v3.ts @@ -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 { 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 = {}; 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, 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 = {}; 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) { + 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") diff --git a/scrape/scraper.js b/scrape/scraper.ts similarity index 90% rename from scrape/scraper.js rename to scrape/scraper.ts index 1f932bb..8899af7 100644 --- a/scrape/scraper.js +++ b/scrape/scraper.ts @@ -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 { 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, diff --git a/scrape/utils/parseAbsence.js b/scrape/utils/parseAbsence.ts similarity index 81% rename from scrape/utils/parseAbsence.js rename to scrape/utils/parseAbsence.ts index 0bf037e..c322214 100644 --- a/scrape/utils/parseAbsence.js +++ b/scrape/utils/parseAbsence.ts @@ -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() diff --git a/scrape/utils/parseTeachers.js b/scrape/utils/parseTeachers.ts similarity index 80% rename from scrape/utils/parseTeachers.js rename to scrape/utils/parseTeachers.ts index 4d16e37..98d4739 100644 --- a/scrape/utils/parseTeachers.js +++ b/scrape/utils/parseTeachers.ts @@ -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> { 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 = {}; $("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; + } } }); diff --git a/server.js b/server.ts similarity index 85% rename from server.js rename to server.ts index 3bfa335..6f6f546 100644 --- a/server.js +++ b/server.ts @@ -12,7 +12,7 @@ * GNU General Public License for more details. */ -import express from "express"; +import express, { Request, Response } from "express"; import path from "path"; const app = express(); import fs from "fs/promises"; @@ -23,6 +23,7 @@ import cors from "cors"; const VERSIONS = ["v1", "v2", "v3"]; const PORT = process.env.PORT || 3000; +// @ts-ignore globalThis.File = class File {}; app.use(bodyParser.json()); @@ -33,7 +34,7 @@ app.use(cors({ allowedHeaders: ["Content-Type"], })); -app.get('/', async (req, res) => { +app.get('/', async (req: Request, res: Response) => { const userAgent = req.headers['user-agent'] || ''; const isBrowser = /Mozilla|Chrome|Firefox|Safari|Edg/.test(userAgent); @@ -49,7 +50,7 @@ app.get('/', async (req, res) => { } }); -app.get('/versioned/v1', async (_, 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); @@ -59,7 +60,7 @@ app.get('/versioned/v1', async (_, res) => { }); VERSIONS.forEach((version) => { - app.get(`/versioned/${version}`, async (_, res) => { + app.get(`/versioned/${version}`, async (_: Request, res: Response) => { try { const filePath = path.join(process.cwd(), "db", `${version}.json`); const dataStr = await fs.readFile(filePath, "utf8"); @@ -75,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 data = JSON.parse(dataStr); @@ -86,7 +87,7 @@ app.get("/status", async (_, res) => { } }) -app.post("/report", async (req, res) => { +app.post("/report", async (req: Request, res: Response): Promise => { const { class: className, location, content } = req.body; if (!className || !location || !content) { return res.status(400).json({ error: "Missing required fields." }); @@ -97,6 +98,11 @@ app.post("/report", async (req, res) => { 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; try { resp = await fetch(url, { @@ -106,7 +112,7 @@ app.post("/report", async (req, res) => { }, body: `${content}\n\nClass: ${className}\nLocation: ${location}`, }); - } catch (err) { + } catch (err: any) { console.error('Fetch failed:', err.message); console.error(err); throw err; diff --git a/tests/test.js b/tests/test.ts similarity index 94% rename from tests/test.js rename to tests/test.ts index d42e557..44c5100 100644 --- a/tests/test.js +++ b/tests/test.ts @@ -15,7 +15,7 @@ import fs from "fs"; import parseAbsence from "../scrape/utils/parseAbsence.js"; -const teachermap = JSON.parse(fs.readFileSync("./teachermap.json")); +const teachermap: Record = JSON.parse(fs.readFileSync("./tests/teachermap.json", "utf8")); test("Me", [ { @@ -232,6 +232,7 @@ test("za Vn zastupuje Jk", [ teacher: "Ing. Zdeněk Vondra", teacherCode: "vn", type: "zastoupen", + hours: null, zastupuje: { teacher: "David Janoušek", teacherCode: "jk", @@ -257,18 +258,18 @@ test("Sv-5-exk", [ }, ]); -function test(input, expectedOutput) { +function test(input: string, expectedOutput: any[]) { const res = parseAbsence(input, teachermap); if (!deepEqual(res, expectedOutput)) { console.error("ERROR for input: " + input); - console.log(res); - console.log(expectedOutput); + console.log(JSON.stringify(res, null, 2)); + console.log(JSON.stringify(expectedOutput, null, 2)); console.log(); } } -function deepEqual(a, b) { +function deepEqual(a: any, b: any): boolean { if (a === b) return true; if ( diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4211842 --- /dev/null +++ b/tsconfig.json @@ -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" + ] +}