-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
231 lines (202 loc) · 4.85 KB
/
index.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import axios from "axios";
import somtoday from "somtoday.js";
import dotenv from "dotenv";
import fs from "fs";
dotenv.config();
declare global {
namespace NodeJS {
interface ProcessEnv {
SCHOOL: string;
USERNAME: string;
PASSWORD: string;
ID: string;
NTFY_URL: string;
TOPIC: string;
}
}
}
async function main() {
const org = await somtoday.searchOrganisation({
name: process.env.SCHOOL,
});
if (!org) throw new Error("School not found");
const user = await org.authenticate({
username: process.env.USERNAME,
password: process.env.PASSWORD,
});
const token = user.accessToken;
const cijfers: Cijfer[] = [];
let i = 0;
while (true) {
// https://api.somtoday.nl/rest/v1
// /rest/v1/resultaten/huidigVoorLeerling/[id]
const res = await axios.get(
`https://api.somtoday.nl/rest/v1/resultaten/huidigVoorLeerling/${process.env.ID}`,
{
headers: {
Authorization: `Bearer ${token}`,
Range: `items=${i}-${i + 99}`,
Accept: "application/json",
},
params: {
additional: "cijferkolomId",
},
},
);
const data = res.data as Response;
const filteredData = data.items.filter(
(cijfer) => cijfer.type === "Toetskolom",
);
const mappedData: Cijfer[] = filteredData.map((cijfer) =>
parseData(cijfer),
);
cijfers.push(...mappedData);
i += 100;
if (data.items.length < 100) break;
}
const oldData = JSON.parse(
fs.readFileSync("cijfers.json", "utf-8"),
) as Cijfer[];
for (const cijfer of cijfers) {
const oldCijfer = oldData.find((oldCijfer) => oldCijfer.id === cijfer.id);
if (!oldCijfer) {
console.log(`Nieuw cijfer: ${cijfer.vak} - ${cijfer.cijfer}`);
// ntfy post request
await axios.post(
process.env.NTFY_URL,
{
topic: process.env.TOPIC,
message: `Je hebt een ${cijfer.cijfer} voor ${cijfer.naam}`,
},
{
headers: {
title: `Nieuw cijfer voor ${cijfer.vak}`,
},
},
);
} else if (oldCijfer.cijfer !== cijfer.cijfer) {
console.log(`Gewijzigd cijfer: ${cijfer.vak} - ${cijfer.cijfer}`);
// ntfy post request
await axios.post(
process.env.NTFY_URL,
{
topic: process.env.TOPIC,
message: `Je cijfer is veranderd van ${oldCijfer.cijfer} naar ${cijfer.cijfer} voor ${cijfer.naam}`,
},
{
headers: {
Title: `Nieuw cijfer voor ${cijfer.vak}`,
},
},
);
}
}
if (cijfers === oldData) console.log("Geen nieuwe cijfers");
fs.writeFileSync("cijfers.json", JSON.stringify(cijfers, null, 2));
}
main();
interface Cijfer {
vak: string;
cijfer: string;
naam: string;
datum: string;
id: string;
}
function parseData(data: Item) {
const cijfer: Cijfer = {
vak: data.vak.afkorting,
cijfer: data.resultaat || data.resultaatLabel || "-",
naam: data.omschrijving || "-",
datum: data.datumInvoer,
id: data.links[0].id.toString(),
};
return cijfer;
}
export interface Response {
items: Item[];
}
export interface Item {
$type: string;
links: Link[];
permissions: Permission[];
additionalObjects: AdditionalObjects;
herkansingstype: string;
datumInvoer: string;
teltNietmee: boolean;
toetsNietGemaakt: boolean;
leerjaar: number;
periode: number;
isExamendossierResultaat: boolean;
isVoortgangsdossierResultaat: boolean;
type: string;
vak: Vak;
leerling: Leerling;
vrijstelling: boolean;
volgnummer?: number;
resultaat?: string;
geldendResultaat?: string;
weging?: number;
examenWeging?: number;
omschrijving?: string;
resultaatLabel?: string;
resultaatLabelAfkorting?: string;
}
export interface Link {
id: number;
rel: string;
type: string;
href: string;
}
export interface Permission {
full: string;
type: string;
operations: string[];
instances: string[];
}
export interface AdditionalObjects {
toetssoortnaam?: string;
}
export interface Vak {
links: Link2[];
permissions: Permission2[];
additionalObjects: AdditionalObjects2;
afkorting: string;
naam: string;
UUID: string;
}
export interface Link2 {
id: number;
rel: string;
type: string;
href: string;
}
export interface Permission2 {
full: string;
type: string;
operations: string[];
instances: string[];
}
export interface AdditionalObjects2 {}
export interface Leerling {
links: Link3[];
permissions: Permission3[];
additionalObjects: AdditionalObjects3;
UUID: string;
leerlingnummer: number;
roepnaam: string;
voorvoegsel: string;
achternaam: string;
}
export interface Link3 {
id: number;
rel: string;
type: string;
href: string;
}
export interface Permission3 {
full: string;
type: string;
operations: string[];
instances: string[];
}
export interface AdditionalObjects3 {}