@wilm-ai/wilma-client 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +5 -0
- package/dist/client.js +20 -0
- package/dist/parsers/messages.js +56 -3
- package/dist/parsers/schedule.d.ts +2 -0
- package/dist/parsers/schedule.js +83 -0
- package/package.json +9 -9
- package/LICENSE +0 -21
package/dist/client.d.ts
CHANGED
package/dist/client.js
CHANGED
|
@@ -5,6 +5,7 @@ import { parseNewsDetailHtml, parseNewsDetailJson, parseNewsList, parseNewsListH
|
|
|
5
5
|
import { parseExamsHtml } from "./parsers/exams.js";
|
|
6
6
|
import { parseAttendanceHtml } from "./parsers/attendance.js";
|
|
7
7
|
import { parseOverview } from "./parsers/overview.js";
|
|
8
|
+
import { parseScheduleHtml } from "./parsers/schedule.js";
|
|
8
9
|
import { parseStudentsFromHome } from "./parsers/students.js";
|
|
9
10
|
export class WilmaClient {
|
|
10
11
|
session;
|
|
@@ -138,6 +139,18 @@ export class WilmaClient {
|
|
|
138
139
|
return parseAttendanceHtml(text, opts?.date ?? "");
|
|
139
140
|
},
|
|
140
141
|
};
|
|
142
|
+
schedule = {
|
|
143
|
+
list: async (opts) => {
|
|
144
|
+
if (!opts?.date) {
|
|
145
|
+
return (await this.overview.get()).schedule;
|
|
146
|
+
}
|
|
147
|
+
const params = new URLSearchParams();
|
|
148
|
+
params.set("date", isoDateToFinnish(opts.date));
|
|
149
|
+
const resp = await this.session.get(`/schedule?${params.toString()}`);
|
|
150
|
+
const text = await resp.text();
|
|
151
|
+
return parseScheduleHtml(text);
|
|
152
|
+
},
|
|
153
|
+
};
|
|
141
154
|
overview = {
|
|
142
155
|
get: async () => {
|
|
143
156
|
const resp = await this.session.get("/overview");
|
|
@@ -154,3 +167,10 @@ function safeJson(text) {
|
|
|
154
167
|
return {};
|
|
155
168
|
}
|
|
156
169
|
}
|
|
170
|
+
function isoDateToFinnish(date) {
|
|
171
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date);
|
|
172
|
+
if (!match) {
|
|
173
|
+
return date;
|
|
174
|
+
}
|
|
175
|
+
return `${match[3]}.${match[2]}.${match[1]}`;
|
|
176
|
+
}
|
package/dist/parsers/messages.js
CHANGED
|
@@ -79,9 +79,53 @@ export function parseMessageDetailHtml(html, messageId) {
|
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
let content = null;
|
|
82
|
-
const
|
|
83
|
-
if (
|
|
84
|
-
|
|
82
|
+
const replyBoxes = $("div.m-replybox.hidden");
|
|
83
|
+
if (replyBoxes.length > 0) {
|
|
84
|
+
const otherReplies = replyBoxes.filter(function () {
|
|
85
|
+
return !$(this).hasClass("m-replybox-me");
|
|
86
|
+
});
|
|
87
|
+
if (otherReplies.length > 0) {
|
|
88
|
+
const latest = otherReplies.last();
|
|
89
|
+
const inner = latest.find("div.inner.hidden");
|
|
90
|
+
if (inner.length) {
|
|
91
|
+
content = inner.text().trim();
|
|
92
|
+
const replyHeaderText = latest.find("h2").first().text().trim();
|
|
93
|
+
const replySender = latest.find("a.profile-link").first();
|
|
94
|
+
if (replySender.length) {
|
|
95
|
+
senderName = replySender.text().trim();
|
|
96
|
+
sendersJson = {
|
|
97
|
+
senders: [
|
|
98
|
+
{
|
|
99
|
+
Name: senderName,
|
|
100
|
+
Href: replySender.attr("href"),
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
const replySenderName = extractReplySenderName(replyHeaderText);
|
|
107
|
+
if (replySenderName) {
|
|
108
|
+
senderName = replySenderName;
|
|
109
|
+
sendersJson = {
|
|
110
|
+
senders: [
|
|
111
|
+
{
|
|
112
|
+
Name: senderName,
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (replyHeaderText) {
|
|
119
|
+
sentAt = parseWilmaTimestamp(extractReplyTimestampText(replyHeaderText));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (!content) {
|
|
125
|
+
const hidden = $("div.ckeditor.hidden").first();
|
|
126
|
+
if (hidden.length) {
|
|
127
|
+
content = hidden.text().trim();
|
|
128
|
+
}
|
|
85
129
|
}
|
|
86
130
|
if (!content && panelBody.length) {
|
|
87
131
|
const clone = cheerio.load(panelBody.html() ?? "");
|
|
@@ -138,3 +182,12 @@ export function debugMessageFields(data) {
|
|
|
138
182
|
function compactText(value) {
|
|
139
183
|
return value.replace(/\s+/g, " ").trim();
|
|
140
184
|
}
|
|
185
|
+
function extractReplyTimestampText(headerText) {
|
|
186
|
+
const absoluteDateTime = /(\d{1,2}\.\d{1,2}\.\d{4}\s+\d{1,2}:\d{2})/.exec(headerText);
|
|
187
|
+
return absoluteDateTime?.[1] ?? headerText;
|
|
188
|
+
}
|
|
189
|
+
function extractReplySenderName(headerText) {
|
|
190
|
+
const match = /^(.+?)\s+(?:vastasi|svarade|replied)\b/i.exec(compactText(headerText));
|
|
191
|
+
const sender = match?.[1]?.trim();
|
|
192
|
+
return sender || null;
|
|
193
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as cheerio from "cheerio";
|
|
2
|
+
export function parseScheduleHtml(html) {
|
|
3
|
+
const $ = cheerio.load(html);
|
|
4
|
+
const scripts = $("script:not([src])")
|
|
5
|
+
.map((_, el) => $(el).html() ?? "")
|
|
6
|
+
.get();
|
|
7
|
+
const script = scripts.find((text) => text.includes("eventsJSON") && text.includes("Events"));
|
|
8
|
+
if (!script) {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
const match = /Events\s*:\s*(\[[\s\S]*?\])\s*,\s*ActiveTyyppi/.exec(script);
|
|
12
|
+
if (!match) {
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
let events;
|
|
16
|
+
try {
|
|
17
|
+
events = JSON.parse(match[1]);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
return events
|
|
23
|
+
.map(parseScheduleEvent)
|
|
24
|
+
.filter((lesson) => lesson !== null)
|
|
25
|
+
.sort((a, b) => a.date.localeCompare(b.date) || a.start.localeCompare(b.start));
|
|
26
|
+
}
|
|
27
|
+
function parseScheduleEvent(event) {
|
|
28
|
+
const date = finnishDateToIso(event.Date ?? "");
|
|
29
|
+
if (!date) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const subject = firstRecordValue(event.Text) || firstRecordValue(event.LongText);
|
|
33
|
+
const teacher = firstTeacher(event.OpeInfo);
|
|
34
|
+
const dateObj = new Date(`${date}T12:00:00`);
|
|
35
|
+
return {
|
|
36
|
+
date,
|
|
37
|
+
dayOfWeek: dateObj.getDay(),
|
|
38
|
+
start: minutesToTime(event.Start),
|
|
39
|
+
end: minutesToTime(event.End),
|
|
40
|
+
subject,
|
|
41
|
+
subjectCode: firstSubjectToken(subject),
|
|
42
|
+
teacher: teacher.name,
|
|
43
|
+
teacherCode: teacher.code,
|
|
44
|
+
groupId: Number(event.Id) || 0,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function finnishDateToIso(value) {
|
|
48
|
+
const match = /^(\d{1,2})\.(\d{1,2})\.(\d{4})$/.exec(value.trim());
|
|
49
|
+
if (!match) {
|
|
50
|
+
return "";
|
|
51
|
+
}
|
|
52
|
+
return `${match[3]}-${match[2].padStart(2, "0")}-${match[1].padStart(2, "0")}`;
|
|
53
|
+
}
|
|
54
|
+
function minutesToTime(value) {
|
|
55
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
56
|
+
return "";
|
|
57
|
+
}
|
|
58
|
+
const hours = Math.floor(value / 60);
|
|
59
|
+
const minutes = value % 60;
|
|
60
|
+
return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
|
|
61
|
+
}
|
|
62
|
+
function firstRecordValue(record) {
|
|
63
|
+
if (!record) {
|
|
64
|
+
return "";
|
|
65
|
+
}
|
|
66
|
+
return Object.values(record).find((value) => value.trim()) ?? "";
|
|
67
|
+
}
|
|
68
|
+
function firstTeacher(groups) {
|
|
69
|
+
if (!groups) {
|
|
70
|
+
return { name: "", code: "" };
|
|
71
|
+
}
|
|
72
|
+
for (const group of Object.values(groups)) {
|
|
73
|
+
for (const person of Object.values(group)) {
|
|
74
|
+
if (person?.nimi || person?.lyhenne) {
|
|
75
|
+
return { name: person.nimi ?? "", code: person.lyhenne ?? "" };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { name: "", code: "" };
|
|
80
|
+
}
|
|
81
|
+
function firstSubjectToken(subject) {
|
|
82
|
+
return subject.trim().split(/\s+/, 1)[0] ?? "";
|
|
83
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wilm-ai/wilma-client",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,6 +14,13 @@
|
|
|
14
14
|
"dist",
|
|
15
15
|
"tenant_list.json"
|
|
16
16
|
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"lint": "echo 'add lint'",
|
|
20
|
+
"test": "pnpm run build && node test/parser-messages.mjs && node test/parser-schedule.mjs && node test/client-schedule.mjs",
|
|
21
|
+
"test:smoke": "tsc -p tsconfig.json && node test/smoke.mjs",
|
|
22
|
+
"test:live": "tsc -p tsconfig.json && node test/live.spec.mjs"
|
|
23
|
+
},
|
|
17
24
|
"dependencies": {
|
|
18
25
|
"cheerio": "^1.0.0-rc.12",
|
|
19
26
|
"tough-cookie": "^4.1.4",
|
|
@@ -22,12 +29,5 @@
|
|
|
22
29
|
"devDependencies": {
|
|
23
30
|
"typescript": "^5.6.3",
|
|
24
31
|
"@types/tough-cookie": "^4.0.5"
|
|
25
|
-
},
|
|
26
|
-
"scripts": {
|
|
27
|
-
"build": "tsc -p tsconfig.json",
|
|
28
|
-
"lint": "echo 'add lint'",
|
|
29
|
-
"test": "echo 'add tests'",
|
|
30
|
-
"test:smoke": "tsc -p tsconfig.json && node test/smoke.mjs",
|
|
31
|
-
"test:live": "tsc -p tsconfig.json && node test/live.spec.mjs"
|
|
32
32
|
}
|
|
33
|
-
}
|
|
33
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Wilm.ai
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|