-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
125 lines (112 loc) · 3.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const request = require("request");
const config = require("./config.json");
const parser = require("./parser");
const exporter = require("./exporter");
require("./expandDate");
let SESSION = "", TOKEN = "";
function parseSession(value) {
if (value.headers) {
if (value.headers["set-cookie"]) {
for (const cookies of value.headers["set-cookie"]) {
if (cookies.includes("JSESSIONID=")) {
SESSION = cookies.replace(/.*JSESSIONID=(.*?);.*/, "$1");
console.log("Session:", SESSION);
}
}
}
}
}
function auth() {
request("https://hektor.webuntis.com/WebUntis/j_spring_security_check", {
method: "POST",
form: {
school: config.schoolName,
j_username: config.username,
j_password: config.password
},
headers: {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"
}
}, (e, value) => {
parseSession(value);
const data = JSON.parse(value.body);
if (data.state === "SUCCESS") {
token();
} else {
console.error("Login not Succeeded", data);
}
});
}
function token() {
request("https://hektor.webuntis.com/WebUntis/api/token/new", {
methode: "GET",
headers: {
"Cookie": "JSESSIONID=" + SESSION
}
}, (e, value) => {
TOKEN = value.body;
console.log("Token:", TOKEN)
userData();
});
}
let PERSON_ID, PERSON_NAME;
function userData() {
request("https://hektor.webuntis.com/WebUntis/api/rest/view/v1/app/data", {
methode: "GET",
headers: {
"Cookie": "JSESSIONID=" + SESSION,
"Authorization": "Bearer " + TOKEN
}
}, async (e, value) => {
const data = JSON.parse(value.body);
PERSON_ID = data.user.person.id;
PERSON_NAME = data.user.person.displayName;
console.log("Person Id:", PERSON_ID);
console.log("Person Name:", PERSON_NAME);
for (const date of config.exportWeeks.sort()) {
await tableData(new Date(date));
}
});
}
function getInfo(start, end) {
start.setMinutes(start.getMinutes() - 15);
end.setMinutes(end.getMinutes() - 15);
return new Promise((resolve) =>
request("https://hektor.webuntis.com/WebUntis/api/rest/view/v1/calendar-entry/detail?elementType=5&homeworkOption=DUE&elementId=" + PERSON_ID + "&startDateTime=" + start.toISOString() + "&endDateTime=" + end.toISOString(), {
methode: "GET",
followRedirects: false,
headers: {
"Cookie": "JSESSIONID=" + SESSION,
"Authorization": "Bearer " + TOKEN
}
}, (e, v) => {
let data = JSON.parse(v.body);
if (data.calendarEntries) {
if (data.calendarEntries.length > 0) {
resolve(data.calendarEntries[0]);
return;
}
}
resolve({});
}));
}
function tableData(queryDate) {
return new Promise((resolve) => {
let weekStart = queryDate.getWeekStart();
request("https://hektor.webuntis.com/WebUntis/api/public/timetable/weekly/data?elementType=5&elementId=" + PERSON_ID + "&date=" + weekStart.toISOString().split("T")[0], {
methode: "GET",
followRedirects: false,
headers: {
"Cookie": "JSESSIONID=" + SESSION
}
}, async (e, value) => {
let data = JSON.parse(value.body);
data = await parser.parseTableData(data, getInfo, PERSON_ID, weekStart);
const sheetName = await exporter.generateSheet(data, PERSON_NAME, weekStart);
console.log("Exported Sheet:", sheetName);
resolve(sheetName);
});
});
}
auth();