72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
import axios from "axios";
|
|
import { log } from "../logs.js";
|
|
|
|
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 DSB 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;
|
|
// Convert the timestamp to the correct
|
|
// format so new Date() accepts it
|
|
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;
|
|
}
|
|
|
|
// 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);
|
|
return [];
|
|
}
|
|
}
|
|
}
|