Skip to content

Commit b47d0af

Browse files
authored
Enhance ICS generation: Add requested timetable parameter and improve lesson details formatting (#7)
1 parent 0c220e6 commit b47d0af

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

src/ics.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import ical, { ICalEventStatus } from "ical-generator";
22
import { Lesson } from "./types";
33

4-
export function lessonsToIcs(lessons: Lesson[], timezone: string): string {
4+
export function lessonsToIcs(
5+
lessons: Lesson[],
6+
timezone: string,
7+
requestedTimetable: string
8+
): string {
59
const cal = ical({ name: "WebUntis Timetable", timezone });
610

711
for (const l of lessons) {
@@ -10,6 +14,18 @@ export function lessonsToIcs(lessons: Lesson[], timezone: string): string {
1014
const endHour = Math.floor(l.endTime / 100);
1115
const endMinute = l.endTime % 100;
1216

17+
const teacherCount = l.teacher.length;
18+
const teacherList = l.teacher.slice(0, 3).join(", ");
19+
const teacherSummary =
20+
teacherCount > 3
21+
? `${teacherList} ...+${teacherCount - 3}`
22+
: teacherList;
23+
24+
const classCount = l.class.length;
25+
const classList = l.class.slice(0, 3).join(", ");
26+
const classSummary =
27+
classCount > 3 ? `${classList} ...+${classCount - 3}` : classList;
28+
1329
cal.createEvent({
1430
start: new Date(
1531
l.date.getFullYear(),
@@ -25,9 +41,14 @@ export function lessonsToIcs(lessons: Lesson[], timezone: string): string {
2541
endHour,
2642
endMinute
2743
),
28-
summary: `${l.subject} (${l.class})`,
29-
description: `Teacher: ${l.teacher}\nRoom: ${l.room}`,
44+
summary: `${l.subject} (${teacherSummary}) - ${classSummary}`,
3045
location: l.room,
46+
description: `Subject: ${l.subject}\nTeacher: ${l.teacher.join(
47+
", "
48+
)}\nRoom: ${l.room}\nClass: ${l.class.join(
49+
", "
50+
)}\nTimetable: ${requestedTimetable}`,
51+
3152
status: "CONFIRMED" as ICalEventStatus,
3253
});
3354
}

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ async function main() {
4747
const lessons = await fetchTimetable(user, startDate, endDate);
4848
const ics = lessonsToIcs(
4949
lessons,
50-
configManager.config.timezone || "Europe/Berlin"
50+
configManager.config.timezone || "Europe/Berlin",
51+
user.friendlyName
5152
);
5253

5354
return sendIcs(res, user.friendlyName, ics);
@@ -102,7 +103,8 @@ async function main() {
102103

103104
const ics = lessonsToIcs(
104105
lessons,
105-
configManager.config.timezone || "Europe/Berlin"
106+
configManager.config.timezone || "Europe/Berlin",
107+
`${user.friendlyName} - ${type || "own"} ${id || ""}`
106108
);
107109

108110
icsCache.set(cacheKey, ics);

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export interface Lesson {
1818
startTime: number;
1919
endTime: number;
2020
subject: string;
21-
teacher: string;
21+
teacher: string[];
2222
room: string;
23-
class: string;
23+
class: string[];
2424
date: Date;
2525
}
2626

src/webuntis.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,11 @@ export async function fetchTimetable(
138138
startTime: entry.startTime,
139139
endTime: entry.endTime,
140140
subject: entry.su?.[0]?.name || "Event",
141-
teacher: entry.te?.[0]?.name || "Unknown Teacher",
141+
teacher: entry.te?.map((t: any) => t.name) || [
142+
"Unknown Teacher",
143+
],
142144
room: entry.ro?.[0]?.name || "Unknown Room",
143-
class:
144-
entry.kl?.[1]?.longname ||
145-
entry.kl?.[0]?.longname ||
146-
"Unknown Class",
145+
class: entry.kl?.map((k: any) => k.name) || ["Unknown Class"],
147146
date: parseUntisDate(entry.date),
148147
}));
149148

0 commit comments

Comments
 (0)