Add parser scripts

This commit is contained in:
2022-05-02 00:47:04 +02:00
parent 69c601ebe8
commit 9bd8132119
2 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import axios from "axios";
const baseurl = "https://mobileapi.dsbcontrol.de";
export async function getAuthtoken(username, password) {
const response = await axios.get(
`${baseurl}/authid?user=${username}&password=${password}&bundleid&appversion&osversion&pushid`
);
if (response.data == "") throw "Wrong username or password";
return response.data;
}
export async function getTimetables(authtoken) {
const response = await axios.get(
`${baseurl}/dsbtimetables?authid=${authtoken}`
);
const timetables = response.data;
const urls = [];
timetables.forEach((timetable) => {
const rawTimestamp = timetable.Date;
const date = rawTimestamp.split(" ")[0].split(".").reverse().join("-");
const time = rawTimestamp.split(" ")[1];
const timestamp = date + " " + time;
urls.push({
title: timetable.Title,
url: timetable.Childs[0].Detail,
updatedAt: new Date(timestamp),
});
});
return urls;
}

66
server/parser/untis.js Normal file
View File

@ -0,0 +1,66 @@
import * as cheerio from "cheerio";
const titles = [
"class",
"date",
"lesson",
"teacher",
"changedTeacher",
"subject",
"room",
"type",
"notes",
];
export function parseSubstitutionPlan(html) {
const infos = {};
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 subsitutionTable = $("table.mon_list tr.list");
// Loop through each table row
subsitutionTable.each((_rowcnt, row) => {
const rowData = {};
// Find the columns and ignore empty ones
const columns = $(row).find("td");
if (columns.text() == "") return;
// Ignore columns that include "Keine Vertretungen"
// to have an empty array if there are no substitutions
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;
});
data.push(rowData);
});
infos.changes = data;
return infos;
}