✨ Only use substitutions of first day in file
This commit is contained in:
@ -15,73 +15,96 @@ export function parseSubstitutionPlan(html) {
|
|||||||
const infos = {};
|
const infos = {};
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
// Extract the date, weekday and a/b week from the title
|
|
||||||
const title = $(".mon_title").text().split(" ");
|
|
||||||
const rawDate = title[0];
|
|
||||||
const date = rawDate
|
|
||||||
.split(".")
|
|
||||||
.reverse()
|
|
||||||
.map((e) => e.padStart(2, 0))
|
|
||||||
.join("-");
|
|
||||||
infos.date = new Date(date).setUTCHours(0, 0, 0, 0);
|
|
||||||
infos.week = title[3];
|
|
||||||
|
|
||||||
// Get the export timestamp
|
|
||||||
const rawTimestamp = $(".mon_head td")
|
|
||||||
.text()
|
|
||||||
.split("Stand: ")[1]
|
|
||||||
.replace(/[\s\n]*$/g, "");
|
|
||||||
const exportDate = rawTimestamp.split(" ")[0].split(".").reverse().join("-");
|
|
||||||
const timestamp = exportDate + " " + rawTimestamp.split(" ")[1];
|
|
||||||
infos.updatedAt = new Date(timestamp).getTime();
|
|
||||||
|
|
||||||
const data = [];
|
const data = [];
|
||||||
|
const tables = $("table.mon_list");
|
||||||
|
tables.each((tableIndex, tableElement) => {
|
||||||
|
// Extract the date, weekday and a/b week from the title
|
||||||
|
const title = $(tableElement)
|
||||||
|
.parent()
|
||||||
|
.siblings(".mon_title")
|
||||||
|
.text()
|
||||||
|
.split(" ");
|
||||||
|
const rawDate = title[0];
|
||||||
|
const date = rawDate
|
||||||
|
.split(".")
|
||||||
|
.reverse()
|
||||||
|
.map((e) => e.padStart(2, 0))
|
||||||
|
.join("-");
|
||||||
|
|
||||||
const titles = [];
|
if (tableIndex == 0) {
|
||||||
const titleElements = $("table.mon_list tr.list th");
|
infos.date = new Date(date).setUTCHours(0, 0, 0, 0);
|
||||||
titleElements.each((index, titleElement) => {
|
infos.week = title[3];
|
||||||
const title = $(titleElement).text();
|
|
||||||
titles[index] = titleTranslations[title];
|
|
||||||
});
|
|
||||||
|
|
||||||
const subsitutionTable = $("table.mon_list tr.list");
|
// Get the export timestamp
|
||||||
// Loop through each table row
|
const rawTimestamp = $(tableElement)
|
||||||
subsitutionTable.each((_rowcnt, row) => {
|
.parent()
|
||||||
const rowData = {};
|
.parent()
|
||||||
|
.siblings(".mon_head")
|
||||||
|
.find("td")
|
||||||
|
.text()
|
||||||
|
.split("Stand: ")[1]
|
||||||
|
.replace(/[\s\n]*$/g, "");
|
||||||
|
const exportDate = rawTimestamp
|
||||||
|
.split(" ")[0]
|
||||||
|
.split(".")
|
||||||
|
.reverse()
|
||||||
|
.join("-");
|
||||||
|
const timestamp = exportDate + " " + rawTimestamp.split(" ")[1];
|
||||||
|
infos.updatedAt = new Date(timestamp).getTime();
|
||||||
|
} else {
|
||||||
|
// If there are multiple days in one file,
|
||||||
|
// ignore all except the first one
|
||||||
|
if (new Date(date).setUTCHours(0, 0, 0, 0) != infos.date) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Find the columns and ignore empty ones
|
const titles = [];
|
||||||
const columns = $(row).find("td");
|
const titleElements = $(tableElement).find("tr.list th");
|
||||||
if (columns.text() == "") return;
|
titleElements.each((index, titleElement) => {
|
||||||
// Ignore columns that include "Keine Vertretungen"
|
const title = $(titleElement).text();
|
||||||
// to have an empty array if there are no substitutions
|
titles[index] = titleTranslations[title];
|
||||||
if (columns.text().includes("Keine Vertretungen")) return;
|
|
||||||
|
|
||||||
columns.each((columncnt, column) => {
|
|
||||||
const text = $(column).text();
|
|
||||||
// Clean the text by removing new lines, tabs, ...
|
|
||||||
var cleantext = text.replace(/^\n\s*/g, "").replace(/\s*$/, "");
|
|
||||||
if (cleantext == "" || cleantext == "---") cleantext = null;
|
|
||||||
|
|
||||||
const columntitle = titles[columncnt];
|
|
||||||
rowData[columntitle] = cleantext;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Split change if it spans over multiple lessons
|
const subsitutionTable = $(tableElement).find("tr.list");
|
||||||
const rawLesson = rowData.lesson;
|
// Loop through each table row
|
||||||
const fromToLessons = rawLesson.match(/\d+/g).map(Number);
|
subsitutionTable.each((_rowcnt, row) => {
|
||||||
const from = fromToLessons[0];
|
const rowData = {};
|
||||||
const to = fromToLessons[1] || fromToLessons[0];
|
|
||||||
|
|
||||||
// Generate numbers from `from` to `to`
|
// Find the columns and ignore empty ones
|
||||||
const lessons = Array(to - from + 1)
|
const columns = $(row).find("td");
|
||||||
.fill()
|
if (columns.text() == "") return;
|
||||||
.map((_e, i) => i + from);
|
// Ignore columns that include "Keine Vertretungen"
|
||||||
|
// to have an empty array if there are no substitutions
|
||||||
|
if (columns.text().includes("Keine Vertretungen")) return;
|
||||||
|
|
||||||
// Create new change for each lesson the change spans over
|
columns.each((columncnt, column) => {
|
||||||
for (const lesson of lessons) {
|
const text = $(column).text();
|
||||||
rowData.lesson = lesson;
|
// Clean the text by removing new lines, tabs, ...
|
||||||
data.push({ ...rowData });
|
var cleantext = text.replace(/^\n\s*/g, "").replace(/\s*$/, "");
|
||||||
}
|
if (cleantext == "" || cleantext == "---") cleantext = null;
|
||||||
|
|
||||||
|
const columntitle = titles[columncnt];
|
||||||
|
rowData[columntitle] = cleantext;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Split change if it spans over multiple lessons
|
||||||
|
const rawLesson = rowData.lesson;
|
||||||
|
const fromToLessons = rawLesson.match(/\d+/g).map(Number);
|
||||||
|
const from = fromToLessons[0];
|
||||||
|
const to = fromToLessons[1] || fromToLessons[0];
|
||||||
|
|
||||||
|
// Generate numbers from `from` to `to`
|
||||||
|
const lessons = Array(to - from + 1)
|
||||||
|
.fill()
|
||||||
|
.map((_e, i) => i + from);
|
||||||
|
|
||||||
|
// Create new change for each lesson the change spans over
|
||||||
|
for (const lesson of lessons) {
|
||||||
|
rowData.lesson = lesson;
|
||||||
|
data.push({ ...rowData });
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
infos.changes = data;
|
infos.changes = data;
|
||||||
|
Reference in New Issue
Block a user