🎨 Make the parser modular

- Move some DSB related functions from parser/index to parser/dsbmobile
- Make the dsb parser a fileProvider with the DSBClient class
- Initialize the parser with a fileProvider and documentParser instance
This commit is contained in:
2023-06-02 16:59:20 +02:00
parent 54fa25f3dd
commit c6120e5f68
3 changed files with 48 additions and 42 deletions

View File

@ -1,4 +1,6 @@
import axios from "axios";
import { log } from "../logs.js";
const baseUrl = "https://mobileapi.dsbcontrol.de";
export async function getAuthtoken(username, password) {
@ -32,3 +34,37 @@ export async function getTimetables(authtoken) {
return urls;
}
// List of files that include timetable data
const dsbFiles = ["Schüler_Monitor - subst_001", "Schüler Morgen - subst_001"];
export class DSBClient {
constructor(dsbUser, dsbPassword) {
this.dsbUser = dsbUser;
this.dsbPassword = dsbPassword;
}
async getFiles() {
try {
// Get authtoken
const token = await getAuthtoken(this.dsbUser, this.dsbPassword);
// Fetch available files
const response = await getTimetables(token);
// Filter files that should be parsed
const timetables = response.filter((e) => dsbFiles.includes(e.title));
// Fetch the contents
const files = [];
for (let timetable of timetables) {
const result = await axios.request({
method: "GET",
url: timetable.url,
responseEncoding: "binary",
});
files.push(result.data);
}
return files;
} catch (error) {
log("Parser / DSB Mobile", "Error getting data: " + error);
}
}
}