@wilm-ai/wilma-client 0.0.1
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/LICENSE +21 -0
- package/dist/client.d.ts +21 -0
- package/dist/client.js +105 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/parsers/dates.d.ts +1 -0
- package/dist/parsers/dates.js +69 -0
- package/dist/parsers/exams.d.ts +2 -0
- package/dist/parsers/exams.js +69 -0
- package/dist/parsers/messages.d.ts +3 -0
- package/dist/parsers/messages.js +114 -0
- package/dist/parsers/news.d.ts +5 -0
- package/dist/parsers/news.js +138 -0
- package/dist/parsers/students.d.ts +6 -0
- package/dist/parsers/students.js +44 -0
- package/dist/session.d.ts +29 -0
- package/dist/session.js +205 -0
- package/dist/tenants.d.ts +5 -0
- package/dist/tenants.js +35 -0
- package/dist/types.d.ts +57 -0
- package/dist/types.js +1 -0
- package/package.json +33 -0
- package/tenant_list.json +5271 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Exam, Message, MessageFolder, NewsItem, WilmaProfile, StudentInfo } from "./types.js";
|
|
2
|
+
export declare class WilmaClient {
|
|
3
|
+
private session;
|
|
4
|
+
private constructor();
|
|
5
|
+
static login(profile: WilmaProfile): Promise<WilmaClient>;
|
|
6
|
+
static listStudents(profile: WilmaProfile): Promise<StudentInfo[]>;
|
|
7
|
+
messages: {
|
|
8
|
+
list: (folder?: MessageFolder) => Promise<Message[]>;
|
|
9
|
+
get: (messageId: number) => Promise<Message>;
|
|
10
|
+
};
|
|
11
|
+
news: {
|
|
12
|
+
list: () => Promise<NewsItem[]>;
|
|
13
|
+
get: (newsId: number) => Promise<NewsItem>;
|
|
14
|
+
};
|
|
15
|
+
exams: {
|
|
16
|
+
list: (opts?: {
|
|
17
|
+
start?: string;
|
|
18
|
+
end?: string;
|
|
19
|
+
}) => Promise<Exam[]>;
|
|
20
|
+
};
|
|
21
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { WilmaSession } from "./session.js";
|
|
2
|
+
import { parseWilmaTimestamp } from "./parsers/dates.js";
|
|
3
|
+
import { parseMessagesList, parseMessageDetailHtml } from "./parsers/messages.js";
|
|
4
|
+
import { parseNewsDetailHtml, parseNewsDetailJson, parseNewsList, parseNewsListHtml, } from "./parsers/news.js";
|
|
5
|
+
import { parseExamsHtml } from "./parsers/exams.js";
|
|
6
|
+
import { parseStudentsFromHome } from "./parsers/students.js";
|
|
7
|
+
export class WilmaClient {
|
|
8
|
+
session;
|
|
9
|
+
constructor(session) {
|
|
10
|
+
this.session = session;
|
|
11
|
+
}
|
|
12
|
+
static async login(profile) {
|
|
13
|
+
const session = new WilmaSession(profile.baseUrl, {
|
|
14
|
+
studentNumber: profile.studentNumber ?? null,
|
|
15
|
+
debug: profile.debug ?? false,
|
|
16
|
+
});
|
|
17
|
+
await session.login(profile.username, profile.password);
|
|
18
|
+
return new WilmaClient(session);
|
|
19
|
+
}
|
|
20
|
+
static async listStudents(profile) {
|
|
21
|
+
const session = new WilmaSession(profile.baseUrl);
|
|
22
|
+
await session.login(profile.username, profile.password);
|
|
23
|
+
const resp = await session.get("/");
|
|
24
|
+
const html = await resp.text();
|
|
25
|
+
return parseStudentsFromHome(html);
|
|
26
|
+
}
|
|
27
|
+
messages = {
|
|
28
|
+
list: async (folder = "inbox") => {
|
|
29
|
+
const folderPaths = {
|
|
30
|
+
inbox: "/messages/list",
|
|
31
|
+
archive: "/messages/list/archive",
|
|
32
|
+
outbox: "/messages/list/outbox",
|
|
33
|
+
drafts: "/messages/list/drafts",
|
|
34
|
+
appointments: "/messages/list/appointments",
|
|
35
|
+
};
|
|
36
|
+
const path = folderPaths[folder] ?? "/messages/list";
|
|
37
|
+
const resp = await this.session.get(path);
|
|
38
|
+
const text = await resp.text();
|
|
39
|
+
const data = safeJson(text);
|
|
40
|
+
return parseMessagesList(data, folder);
|
|
41
|
+
},
|
|
42
|
+
get: async (messageId) => {
|
|
43
|
+
const resp = await this.session.get(`/messages/${messageId}`);
|
|
44
|
+
const contentType = resp.headers.get("content-type")?.toLowerCase() ?? "";
|
|
45
|
+
const text = await resp.text();
|
|
46
|
+
if (contentType.includes("application/json")) {
|
|
47
|
+
const data = safeJson(text);
|
|
48
|
+
return {
|
|
49
|
+
wilmaId: messageId,
|
|
50
|
+
subject: String(data["Subject"] ?? data["subject"] ?? ""),
|
|
51
|
+
sentAt: parseWilmaTimestamp(data["TimeStamp"] ?? data["timestamp"]),
|
|
52
|
+
folder: String(data["Folder"] ?? "unknown"),
|
|
53
|
+
senderId: data["SenderId"] ?? null,
|
|
54
|
+
senderType: data["SenderType"] ?? null,
|
|
55
|
+
senderName: (data["Sender"] ?? data["sender"]),
|
|
56
|
+
sendersJson: (data["Senders"] ?? data["senders"]),
|
|
57
|
+
status: data["Status"] ?? null,
|
|
58
|
+
content: (data["Content"] ?? data["content"]),
|
|
59
|
+
fetchedAt: new Date(),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return parseMessageDetailHtml(text, messageId);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
news = {
|
|
66
|
+
list: async () => {
|
|
67
|
+
const resp = await this.session.get("/news");
|
|
68
|
+
const text = await resp.text();
|
|
69
|
+
const data = safeJson(text);
|
|
70
|
+
if (Array.isArray(data)) {
|
|
71
|
+
return parseNewsList(data);
|
|
72
|
+
}
|
|
73
|
+
return parseNewsListHtml(text);
|
|
74
|
+
},
|
|
75
|
+
get: async (newsId) => {
|
|
76
|
+
const resp = await this.session.get(`/news/${newsId}`);
|
|
77
|
+
const contentType = resp.headers.get("content-type")?.toLowerCase() ?? "";
|
|
78
|
+
const text = await resp.text();
|
|
79
|
+
if (!contentType.includes("text/html")) {
|
|
80
|
+
const data = safeJson(text);
|
|
81
|
+
if (Object.keys(data).length) {
|
|
82
|
+
return parseNewsDetailJson(newsId, data);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return parseNewsDetailHtml(text, newsId);
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
exams = {
|
|
89
|
+
list: async (opts) => {
|
|
90
|
+
const startQs = opts?.start ? `?start=${opts.start}` : "";
|
|
91
|
+
const endQs = opts?.end ? `&end=${opts.end}` : "";
|
|
92
|
+
const resp = await this.session.get(`/exams/calendar${startQs}${endQs}`);
|
|
93
|
+
const text = await resp.text();
|
|
94
|
+
return parseExamsHtml(text);
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function safeJson(text) {
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(text);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return {};
|
|
104
|
+
}
|
|
105
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { Exam, Message, MessageFolder, Municipality, NewsItem, TenantDiscoveryResponse, TenantInfo, WilmaProfile, StudentInfo, } from "./types.js";
|
|
2
|
+
export { WilmaClient } from "./client.js";
|
|
3
|
+
export { WilmaSession, AuthenticationError, APIError } from "./session.js";
|
|
4
|
+
export { loadTenantDiscovery, listTenants, searchTenantsByMunicipality, findTenantByUrl, } from "./tenants.js";
|
|
5
|
+
export { parseWilmaTimestamp } from "./parsers/dates.js";
|
|
6
|
+
export { parseStudentsFromHome } from "./parsers/students.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { WilmaClient } from "./client.js";
|
|
2
|
+
export { WilmaSession, AuthenticationError, APIError } from "./session.js";
|
|
3
|
+
export { loadTenantDiscovery, listTenants, searchTenantsByMunicipality, findTenantByUrl, } from "./tenants.js";
|
|
4
|
+
export { parseWilmaTimestamp } from "./parsers/dates.js";
|
|
5
|
+
export { parseStudentsFromHome } from "./parsers/students.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseWilmaTimestamp(value: unknown): Date;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export function parseWilmaTimestamp(value) {
|
|
2
|
+
if (value === null || value === undefined) {
|
|
3
|
+
return new Date();
|
|
4
|
+
}
|
|
5
|
+
if (typeof value === "number") {
|
|
6
|
+
return new Date(value * 1000);
|
|
7
|
+
}
|
|
8
|
+
const text = String(value)
|
|
9
|
+
.trim()
|
|
10
|
+
.toLowerCase()
|
|
11
|
+
.replace("klo", "")
|
|
12
|
+
.replace("julkaistu", "")
|
|
13
|
+
.trim();
|
|
14
|
+
const now = new Date();
|
|
15
|
+
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
16
|
+
const rel = {
|
|
17
|
+
"tänään": 0,
|
|
18
|
+
"eilen": 1,
|
|
19
|
+
today: 0,
|
|
20
|
+
yesterday: 1,
|
|
21
|
+
idag: 0,
|
|
22
|
+
"i dag": 0,
|
|
23
|
+
"igår": 1,
|
|
24
|
+
"i går": 1,
|
|
25
|
+
};
|
|
26
|
+
for (const [kw, daysAgo] of Object.entries(rel)) {
|
|
27
|
+
if (text.includes(kw)) {
|
|
28
|
+
const base = new Date(today);
|
|
29
|
+
base.setDate(base.getDate() - daysAgo);
|
|
30
|
+
const timeMatch = /(\d{1,2})[:.](\d{2})/.exec(text);
|
|
31
|
+
if (timeMatch) {
|
|
32
|
+
const h = Number(timeMatch[1]);
|
|
33
|
+
const m = Number(timeMatch[2]);
|
|
34
|
+
base.setHours(h, m, 0, 0);
|
|
35
|
+
}
|
|
36
|
+
return base;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const fmt of ["dateTime", "dateOnly"]) {
|
|
40
|
+
if (fmt === "dateTime") {
|
|
41
|
+
const match = /^(\d{1,2})\.(\d{1,2})\.(\d{4})\s+(\d{1,2}):(\d{2})$/.exec(text);
|
|
42
|
+
if (match) {
|
|
43
|
+
const [, d, m, y, h, mi] = match.map(Number);
|
|
44
|
+
return new Date(y, m - 1, d, h, mi, 0, 0);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const match = /^(\d{1,2})\.(\d{1,2})\.(\d{4})$/.exec(text);
|
|
49
|
+
if (match) {
|
|
50
|
+
const [, d, m, y] = match.map(Number);
|
|
51
|
+
return new Date(y, m - 1, d);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const shortMatch = /^(\d{1,2})\.(\d{1,2})\.$/.exec(text);
|
|
56
|
+
if (shortMatch) {
|
|
57
|
+
const [, dStr, mStr] = shortMatch;
|
|
58
|
+
const day = Number(dStr);
|
|
59
|
+
const month = Number(mStr);
|
|
60
|
+
const candidate = new Date(now.getFullYear(), month - 1, day);
|
|
61
|
+
const sixMonthsAhead = new Date(today);
|
|
62
|
+
sixMonthsAhead.setDate(sixMonthsAhead.getDate() + 180);
|
|
63
|
+
if (candidate > sixMonthsAhead) {
|
|
64
|
+
candidate.setFullYear(candidate.getFullYear() - 1);
|
|
65
|
+
}
|
|
66
|
+
return candidate;
|
|
67
|
+
}
|
|
68
|
+
return now;
|
|
69
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as cheerio from "cheerio";
|
|
2
|
+
export function parseExamsHtml(html) {
|
|
3
|
+
const $ = cheerio.load(html);
|
|
4
|
+
const exams = [];
|
|
5
|
+
let autoId = 1;
|
|
6
|
+
const now = new Date();
|
|
7
|
+
$("div.table-responsive.margin-bottom").each((_, block) => {
|
|
8
|
+
const table = $(block).find("table.table-grey").first();
|
|
9
|
+
if (!table.length) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const rows = table.find("tr");
|
|
13
|
+
if (!rows.length) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const firstCells = $(rows.get(0)).find("td");
|
|
17
|
+
if (firstCells.length < 2) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const dateText = $(firstCells.get(0)).text().trim();
|
|
21
|
+
const match = /(\d{1,2}\.\d{1,2}\.\d{4})/.exec(dateText);
|
|
22
|
+
if (!match) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const [d, m, y] = match[1].split(".").map(Number);
|
|
26
|
+
const examDate = new Date(y, m - 1, d);
|
|
27
|
+
const subjDesc = $(firstCells.get(1)).text().trim();
|
|
28
|
+
let subject = subjDesc;
|
|
29
|
+
let description = null;
|
|
30
|
+
if (subjDesc.includes(":")) {
|
|
31
|
+
const parts = subjDesc.split(":");
|
|
32
|
+
description = parts[0].trim();
|
|
33
|
+
subject = parts.slice(1).join(":").trim();
|
|
34
|
+
}
|
|
35
|
+
let teacher = null;
|
|
36
|
+
let notes = null;
|
|
37
|
+
rows.slice(1).each((_, row) => {
|
|
38
|
+
const th = $(row).find("th").first();
|
|
39
|
+
const td = $(row).find("td").first();
|
|
40
|
+
if (!th.length || !td.length) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const header = th.text().trim().toLowerCase();
|
|
44
|
+
const value = td.text().trim();
|
|
45
|
+
if (header.includes("opettaja")) {
|
|
46
|
+
const names = td
|
|
47
|
+
.find("a.profile-link")
|
|
48
|
+
.toArray()
|
|
49
|
+
.map((a) => $(a).text().trim())
|
|
50
|
+
.filter(Boolean);
|
|
51
|
+
teacher = names.length ? names.join(", ") : value;
|
|
52
|
+
}
|
|
53
|
+
else if (header.includes("lisätiedot") || header.includes("notes")) {
|
|
54
|
+
notes = value;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
exams.push({
|
|
58
|
+
wilmaId: autoId,
|
|
59
|
+
examDate,
|
|
60
|
+
subject: subject || "(N/A)",
|
|
61
|
+
description,
|
|
62
|
+
teacher,
|
|
63
|
+
notes,
|
|
64
|
+
fetchedAt: now,
|
|
65
|
+
});
|
|
66
|
+
autoId += 1;
|
|
67
|
+
});
|
|
68
|
+
return exams;
|
|
69
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as cheerio from "cheerio";
|
|
2
|
+
import { parseWilmaTimestamp } from "./dates.js";
|
|
3
|
+
export function parseMessagesList(data, folder) {
|
|
4
|
+
const now = new Date();
|
|
5
|
+
const list = normalizeMessagesList(data);
|
|
6
|
+
return list.flatMap((item) => {
|
|
7
|
+
try {
|
|
8
|
+
const wilmaId = Number(item["id"] ?? item["Id"]);
|
|
9
|
+
const subject = String(item["Subject"] ?? item["subject"] ?? "");
|
|
10
|
+
const timeValue = item["Time"] ?? item["time"];
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
wilmaId,
|
|
14
|
+
subject,
|
|
15
|
+
sentAt: parseWilmaTimestamp(timeValue),
|
|
16
|
+
folder,
|
|
17
|
+
fetchedAt: now,
|
|
18
|
+
},
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export function parseMessageDetailHtml(html, messageId) {
|
|
27
|
+
const $ = cheerio.load(html);
|
|
28
|
+
const panelBody = $("div#page-content-area.panel-body");
|
|
29
|
+
const subjElem = panelBody.length ? panelBody.children("h1").first() : $("h1, h2, .panel-title, .msg-subject").first();
|
|
30
|
+
const subject = subjElem.text().trim();
|
|
31
|
+
let senderName = null;
|
|
32
|
+
let sentAt = new Date();
|
|
33
|
+
let sendersJson = null;
|
|
34
|
+
const propTable = $("table.proptable");
|
|
35
|
+
if (propTable.length) {
|
|
36
|
+
propTable.find("tr").each((_, row) => {
|
|
37
|
+
const th = $(row).find("th").first();
|
|
38
|
+
const td = $(row).find("td").first();
|
|
39
|
+
if (!th.length || !td.length) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const header = th.text().trim().toLowerCase();
|
|
43
|
+
if (header.includes("lähettäjä") || header.includes("sender")) {
|
|
44
|
+
const senderLink = td.find("a.profile-link").first();
|
|
45
|
+
if (senderLink.length) {
|
|
46
|
+
senderName = senderLink.text().trim();
|
|
47
|
+
sendersJson = {
|
|
48
|
+
senders: [
|
|
49
|
+
{
|
|
50
|
+
Name: senderName,
|
|
51
|
+
Href: senderLink.attr("href"),
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
senderName = td.text().trim();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else if (header.includes("lähetetty") || header.includes("sent")) {
|
|
61
|
+
const sentText = td.text().trim();
|
|
62
|
+
sentAt = parseWilmaTimestamp(sentText);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
let content = null;
|
|
67
|
+
const hidden = $("div.ckeditor.hidden").first();
|
|
68
|
+
if (hidden.length) {
|
|
69
|
+
content = hidden.text().trim();
|
|
70
|
+
}
|
|
71
|
+
if (!content && panelBody.length) {
|
|
72
|
+
const clone = cheerio.load(panelBody.html() ?? "");
|
|
73
|
+
["table.proptable", "h1", "iframe", "script", "style"].forEach((sel) => {
|
|
74
|
+
clone(sel).remove();
|
|
75
|
+
});
|
|
76
|
+
const text = clone.root().text().trim();
|
|
77
|
+
if (text) {
|
|
78
|
+
content = text;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!content) {
|
|
82
|
+
const fallback = $(".message-body, .msg-content").first();
|
|
83
|
+
if (fallback.length) {
|
|
84
|
+
fallback.find("script, style").remove();
|
|
85
|
+
content = fallback.text().trim();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (!content || !content.trim()) {
|
|
89
|
+
content = "(Could not extract content body)";
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
wilmaId: messageId,
|
|
93
|
+
subject,
|
|
94
|
+
sentAt,
|
|
95
|
+
folder: "unknown",
|
|
96
|
+
senderName,
|
|
97
|
+
sendersJson,
|
|
98
|
+
content,
|
|
99
|
+
fetchedAt: new Date(),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function normalizeMessagesList(data) {
|
|
103
|
+
if (Array.isArray(data)) {
|
|
104
|
+
return data;
|
|
105
|
+
}
|
|
106
|
+
if (data && typeof data === "object") {
|
|
107
|
+
const obj = data;
|
|
108
|
+
const list = (obj["messages"] ?? obj["Messages"]);
|
|
109
|
+
if (Array.isArray(list)) {
|
|
110
|
+
return list;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { NewsItem } from "../types.js";
|
|
2
|
+
export declare function parseNewsList(data: unknown): NewsItem[];
|
|
3
|
+
export declare function parseNewsDetailJson(newsId: number, data: Record<string, unknown>): NewsItem;
|
|
4
|
+
export declare function parseNewsDetailHtml(html: string, newsId: number): NewsItem;
|
|
5
|
+
export declare function parseNewsListHtml(html: string): NewsItem[];
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import * as cheerio from "cheerio";
|
|
2
|
+
import { parseWilmaTimestamp } from "./dates.js";
|
|
3
|
+
export function parseNewsList(data) {
|
|
4
|
+
if (Array.isArray(data)) {
|
|
5
|
+
const now = new Date();
|
|
6
|
+
return data.flatMap((item) => {
|
|
7
|
+
try {
|
|
8
|
+
const wilmaId = Number(item["id"] ?? item["Id"]);
|
|
9
|
+
const title = String(item["Title"] ?? item["title"] ?? "");
|
|
10
|
+
const published = parseWilmaTimestamp(item["Published"] ?? item["published"]);
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
wilmaId,
|
|
14
|
+
title,
|
|
15
|
+
published,
|
|
16
|
+
fetchedAt: now,
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
export function parseNewsDetailJson(newsId, data) {
|
|
28
|
+
return {
|
|
29
|
+
wilmaId: newsId,
|
|
30
|
+
title: String(data["title"] ?? data["Title"] ?? ""),
|
|
31
|
+
subtitle: (data["subtitle"] ?? data["Subtitle"]),
|
|
32
|
+
author: (data["author"] ?? data["Author"]),
|
|
33
|
+
published: parseWilmaTimestamp(data["Published"] ?? data["published"]),
|
|
34
|
+
content: (data["content"] ?? data["Content"]),
|
|
35
|
+
fetchedAt: new Date(),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function parseNewsDetailHtml(html, newsId) {
|
|
39
|
+
const $ = cheerio.load(html);
|
|
40
|
+
let title = $("title").text().trim();
|
|
41
|
+
if (title.endsWith(" - Wilma")) {
|
|
42
|
+
title = title.slice(0, -8).trim();
|
|
43
|
+
}
|
|
44
|
+
if (!title) {
|
|
45
|
+
const titleElem = $("#page-content-area h1, #main-content h1, h1").first();
|
|
46
|
+
title = titleElem.text().trim();
|
|
47
|
+
}
|
|
48
|
+
const subtitleElem = $("p.sub-text, .subtitle").first();
|
|
49
|
+
const subtitle = subtitleElem.length ? subtitleElem.text().trim() : null;
|
|
50
|
+
let content = null;
|
|
51
|
+
const contentElem = $(".panel-body, .news-content, .content, .ckeditor, article").first();
|
|
52
|
+
if (contentElem.length) {
|
|
53
|
+
contentElem.find("script, style").remove();
|
|
54
|
+
content = contentElem.text().trim();
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
wilmaId: newsId,
|
|
58
|
+
title,
|
|
59
|
+
subtitle,
|
|
60
|
+
author: null,
|
|
61
|
+
published: null,
|
|
62
|
+
content,
|
|
63
|
+
fetchedAt: new Date(),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export function parseNewsListHtml(html) {
|
|
67
|
+
const $ = cheerio.load(html);
|
|
68
|
+
const now = new Date();
|
|
69
|
+
const newsItems = [];
|
|
70
|
+
const seenIds = new Set();
|
|
71
|
+
const headers = $("div.left h2.no-border, h2.no-border");
|
|
72
|
+
if (!headers.length) {
|
|
73
|
+
return newsItems;
|
|
74
|
+
}
|
|
75
|
+
headers.each((_, header) => {
|
|
76
|
+
const headerEl = $(header);
|
|
77
|
+
const timestampRaw = headerEl.text().trim();
|
|
78
|
+
const published = parseWilmaTimestamp(timestampRaw);
|
|
79
|
+
let sib = headerEl.next();
|
|
80
|
+
while (sib.length) {
|
|
81
|
+
if (sib.is("h2") && sib.hasClass("no-border")) {
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
if (sib.is("div") && (sib.hasClass("well") || sib.hasClass("margin-bottom"))) {
|
|
85
|
+
const container = sib;
|
|
86
|
+
const linkTag = container.find("a[href*='/news/']").first();
|
|
87
|
+
const href = linkTag.attr("href") ?? "";
|
|
88
|
+
const match = /\/news\/(\d+)/.exec(href);
|
|
89
|
+
const newsId = match ? Number(match[1]) : null;
|
|
90
|
+
if (!newsId || seenIds.has(newsId)) {
|
|
91
|
+
sib = sib.next();
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
seenIds.add(newsId);
|
|
95
|
+
const titleElem = container.find("h1, h2, h3, h4").first();
|
|
96
|
+
const title = titleElem.text().trim() || linkTag.text().trim() || "Untitled News";
|
|
97
|
+
const subtitleElem = container.find("p.sub-text").first();
|
|
98
|
+
const subtitle = subtitleElem.length ? subtitleElem.text().trim() : null;
|
|
99
|
+
let author = null;
|
|
100
|
+
const metaP = container.find("p.small").first();
|
|
101
|
+
if (metaP.length) {
|
|
102
|
+
const authorLink = metaP.find("a.profile-link").first();
|
|
103
|
+
if (authorLink.length) {
|
|
104
|
+
author = authorLink.attr("title") ?? authorLink.text().trim();
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
const tooltip = metaP.find("span.tooltip").first();
|
|
108
|
+
if (tooltip.length) {
|
|
109
|
+
author = tooltip.attr("title") ?? tooltip.text().trim();
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
const metaSpan = metaP.find("span.horizontal-link-container.small").first();
|
|
113
|
+
if (metaSpan.length) {
|
|
114
|
+
const metaText = metaSpan.text().trim();
|
|
115
|
+
if (metaText.toLowerCase().includes("ylläpidon tiedote")) {
|
|
116
|
+
author = "Ylläpito";
|
|
117
|
+
}
|
|
118
|
+
else if (metaText.split(/\s+/).length < 5 && metaText !== linkTag.text().trim()) {
|
|
119
|
+
author = metaText;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
newsItems.push({
|
|
126
|
+
wilmaId: newsId,
|
|
127
|
+
title,
|
|
128
|
+
subtitle,
|
|
129
|
+
author,
|
|
130
|
+
published,
|
|
131
|
+
fetchedAt: now,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
sib = sib.next();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return newsItems;
|
|
138
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as cheerio from "cheerio";
|
|
2
|
+
const NAV_KEYWORDS = [
|
|
3
|
+
"messages",
|
|
4
|
+
"viestit",
|
|
5
|
+
"schedule",
|
|
6
|
+
"lukujärjestys",
|
|
7
|
+
"gradebook",
|
|
8
|
+
"assessments",
|
|
9
|
+
"exams",
|
|
10
|
+
"attendance",
|
|
11
|
+
"poissaolot",
|
|
12
|
+
"printouts",
|
|
13
|
+
"news",
|
|
14
|
+
];
|
|
15
|
+
export function parseStudentsFromHome(html) {
|
|
16
|
+
const $ = cheerio.load(html);
|
|
17
|
+
const students = new Map();
|
|
18
|
+
$("a[href^='/!']").each((_, anchor) => {
|
|
19
|
+
const href = $(anchor).attr("href") ?? "";
|
|
20
|
+
const match = /\/!(\d+)\//.exec(href);
|
|
21
|
+
if (!match) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const studentNumber = match[1];
|
|
25
|
+
const cloned = $(anchor).clone();
|
|
26
|
+
cloned.find("small, span.lem").remove();
|
|
27
|
+
const text = cloned.text().trim();
|
|
28
|
+
if (!text) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const lower = text.toLowerCase();
|
|
32
|
+
if (NAV_KEYWORDS.some((kw) => lower.includes(kw))) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!students.has(studentNumber)) {
|
|
36
|
+
students.set(studentNumber, {
|
|
37
|
+
studentNumber,
|
|
38
|
+
name: text,
|
|
39
|
+
href,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return [...students.values()];
|
|
44
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type RequestInit, type Response } from "undici";
|
|
2
|
+
export declare class AuthenticationError extends Error {
|
|
3
|
+
}
|
|
4
|
+
export declare class APIError extends Error {
|
|
5
|
+
status: number;
|
|
6
|
+
constructor(message: string, status: number);
|
|
7
|
+
}
|
|
8
|
+
export declare class WilmaSession {
|
|
9
|
+
private baseUrl;
|
|
10
|
+
private cookieJar;
|
|
11
|
+
private loggedIn;
|
|
12
|
+
private username?;
|
|
13
|
+
private password?;
|
|
14
|
+
private studentNumber?;
|
|
15
|
+
private debug;
|
|
16
|
+
constructor(baseUrl: string, opts?: {
|
|
17
|
+
studentNumber?: string | null;
|
|
18
|
+
debug?: boolean;
|
|
19
|
+
});
|
|
20
|
+
get urlPrefix(): string | null;
|
|
21
|
+
getPrefixedPath(path: string): string;
|
|
22
|
+
login(username: string, password: string): Promise<void>;
|
|
23
|
+
private getLoginFormFields;
|
|
24
|
+
request(path: string, init?: RequestInit): Promise<Response>;
|
|
25
|
+
get(path: string, init?: RequestInit): Promise<Response>;
|
|
26
|
+
post(path: string, init?: RequestInit): Promise<Response>;
|
|
27
|
+
private getLoginToken;
|
|
28
|
+
private rawRequest;
|
|
29
|
+
}
|