This commit is contained in:
44
scrape/utils/parseAbsence.js
Normal file
44
scrape/utils/parseAbsence.js
Normal file
@@ -0,0 +1,44 @@
|
||||
function parseAbsence(str, teacherMap) {
|
||||
str = str.trim().replace(/\s+/g, " ");
|
||||
|
||||
const result = {
|
||||
teacher: null, // String or null
|
||||
teacherCode: null, // String or null
|
||||
type: null, // "wholeDay" | "range" | "single" | "invalid"
|
||||
hours: null // { from: number, to: number } or number
|
||||
};
|
||||
|
||||
// Regex patterns (with flexible spaces)
|
||||
const wholeDayPattern = /^([A-Za-z]+)$/;
|
||||
const rangePattern = /^(\d+)\s*-\s*(\d+)\s+([A-Za-z]+)$/;
|
||||
const singleHourPattern = /^(\d+)\s+([A-Za-z]+)$/;
|
||||
|
||||
if (rangePattern.test(str)) {
|
||||
const [, from, to, teacherCode] = str.match(rangePattern);
|
||||
result.teacher = teacherMap[teacherCode.toLowerCase()];
|
||||
result.teacherCode = teacherCode;
|
||||
result.type = "range";
|
||||
result.hours = { from: parseInt(from), to: parseInt(to) };
|
||||
}
|
||||
else if (singleHourPattern.test(str)) {
|
||||
const [, hour, teacherCode] = str.match(singleHourPattern);
|
||||
result.teacher = teacherMap[teacherCode.toLowerCase()];
|
||||
result.teacherCode = teacherCode;
|
||||
result.type = "single";
|
||||
result.hours = parseInt(hour);
|
||||
}
|
||||
else if (wholeDayPattern.test(str)) {
|
||||
const [, teacherCode] = str.match(wholeDayPattern);
|
||||
result.teacher = teacherMap[teacherCode.toLowerCase()];
|
||||
result.teacherCode = teacherCode;
|
||||
result.type = "wholeDay";
|
||||
}
|
||||
else {
|
||||
result.type = "invalid";
|
||||
result.original = str;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = parseAbsence;
|
||||
25
scrape/utils/parseTeachers.js
Normal file
25
scrape/utils/parseTeachers.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const { default: axios } = require("axios");
|
||||
const cheerio = require("cheerio");
|
||||
|
||||
async function parseTeachers() {
|
||||
const url = "https://spsejecna.cz/ucitel";
|
||||
const { data } = await axios.get(url);
|
||||
const $ = cheerio.load(data);
|
||||
|
||||
const map = {};
|
||||
|
||||
$("main .contentLeftColumn li, main .contentRightColumn li").each((_, el) => {
|
||||
const link = $(el).find("a");
|
||||
const href = link.attr("href"); // e.g. "/ucitel/PA"
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
module.exports = parseTeachers;
|
||||
Reference in New Issue
Block a user