Skip to content

Commit b1f48d7

Browse files
committed
restructure
1 parent 6dba336 commit b1f48d7

File tree

1 file changed

+82
-32
lines changed

1 file changed

+82
-32
lines changed

timeTableToIcs.ps1

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
1+
<#
2+
.SYNOPSIS
3+
Converts a WebUntis timetable to an ICS calendar file.
4+
5+
.DESCRIPTION
6+
This script retrieves timetable data from the WebUntis API and converts it into an ICS calendar file format.
7+
It allows specifying a date range for the timetable data.
8+
9+
.PARAMETER baseUrl
10+
The base URL of the WebUntis API.
11+
12+
.PARAMETER elementType
13+
The type of element to filter by (default is 1, this should return a timetable).
14+
15+
.PARAMETER elementId
16+
The classes timetable ID.
17+
18+
.PARAMETER dates
19+
An array of dates (either as strings or DateTime objects) for which to retrieve timetable data.
20+
The default is the current week and the next three weeks.
21+
22+
.PARAMETER OutputFilePath
23+
The file path where the ICS file will be saved. The default is "calendar.ics".
24+
25+
.PARAMETER cookie
26+
The cookie value for the WebUntis session.
27+
28+
.PARAMETER tenantId
29+
The tenant ID for the WebUntis session.
30+
31+
.EXAMPLE
32+
.\timeTableToIcs.ps1 -baseUrl "your.webuntis.url" -elementType 1 -elementId 12345 -dates "2023-01-01", "2023-01-08" -OutputFilePath "mycalendar.ics" -cookie "your_cookie" -tenantId "your_tenant_id"
33+
34+
.NOTES
35+
Author: Markus Noack
36+
Date: 2024-09-28
37+
Version: 1.0
38+
#>
39+
140
param (
41+
[ValidateNotNullOrEmpty()]
42+
[Alias("URL")]
243
[string]$baseUrl,
344
[int]$elementType = 1,
45+
[Alias("TimeTableID")]
446
[int]$elementId,
547
[Parameter(Mandatory = $false)]
648
[Alias("Date")]
@@ -15,16 +57,24 @@ param (
1557
}
1658
$true
1759
})]
18-
[System.Object[]]$dates = @( (-7..14 | ForEach-Object { (Get-Date).AddDays($_) })[0, 7, 14] ),
60+
[System.Object[]]$dates = @( (@(-7, 0, 7, 14) | ForEach-Object { (Get-Date).AddDays($_) }) ),
61+
[ValidateNotNullOrEmpty()]
1962
[string]$OutputFilePath = "calendar.ics",
63+
[ValidateNotNullOrEmpty()]
2064
[string]$cookie,
65+
[ValidateNotNullOrEmpty()]
2166
[string]$tenantId
2267
)
2368

69+
$datesCount = 0
2470
# Convert any string inputs to DateTime objects
2571
$dates = $dates | ForEach-Object {
72+
$datescount++
2673
if ($_ -is [string]) { [datetime]::Parse($_) } else { $_ }
2774
}
75+
if ($datesCount -gt 4) {
76+
throw "The maximum number of weeks is 4. (Limit by WebUntis API)"
77+
}
2878

2979
function Get-SingleElement {
3080
param (
@@ -80,7 +130,6 @@ $session.Cookies.Add((New-Object System.Net.Cookie("traceId", "9de4710537aa09759
80130
$session.Cookies.Add((New-Object System.Net.Cookie("JSESSIONID", "B9ED9B2D36BE7D25A7A9EF21E8144D3F", "/", "$baseUrl")))
81131

82132
$periods = [System.Collections.Generic.List[PeriodEntry]]::new()
83-
84133
$courses = [System.Collections.Generic.List[Course]]::new()
85134
$rooms = [System.Collections.Generic.List[Room]]::new()
86135

@@ -178,6 +227,37 @@ $IcsEntries = [System.Collections.Generic.List[string]]::new()
178227
foreach ($icsEvent in $calendarEntries) {
179228
$IcsEntries += $icsEvent.ToIcsEntry()
180229
}
230+
231+
232+
# Create the .ics file content
233+
$icsContent = @"
234+
BEGIN:VCALENDAR
235+
VERSION:2.0
236+
PRODID:-//Chaos_02//WebUntisToIcs//EN
237+
X-WR-CALNAME:$($class.displayname)
238+
$(($IcsEntries -join "`n"))
239+
END:VCALENDAR
240+
"@
241+
242+
try {
243+
if ($OutputFilePath) {
244+
# Write the .ics content to a file
245+
Set-Content -Path $OutputFilePath -Value $icsContent
246+
Write-Output "ICS file created at $((Get-Item -Path $OutputFilePath).FullName)"
247+
}
248+
else {
249+
# Write the .ics content to a variable
250+
$icsVariable = $icsContent
251+
Write-Output $icsVariable
252+
return $icsVariable
253+
}
254+
}
255+
catch {
256+
Write-Error "An error occurred while creating the ICS file: $_"
257+
throw
258+
}
259+
260+
####### Class definitions #######
181261

182262
class IcsEvent {
183263
[string]$StartTime
@@ -224,36 +304,6 @@ END:VEVENT
224304
}
225305
}
226306

227-
228-
229-
# Create the .ics file content
230-
$icsContent = @"
231-
BEGIN:VCALENDAR
232-
VERSION:2.0
233-
PRODID:-//Chaos_02//WebUntisToIcs//EN
234-
X-WR-CALNAME:$($class.displayname)
235-
$(($IcsEntries -join "`n"))
236-
END:VCALENDAR
237-
"@
238-
239-
try {
240-
if ($OutputFilePath) {
241-
# Write the .ics content to a file
242-
Set-Content -Path $OutputFilePath -Value $icsContent
243-
Write-Output "ICS file created at $((Get-Item -Path $OutputFilePath).FullName)"
244-
}
245-
else {
246-
# Write the .ics content to a variable
247-
$icsVariable = $icsContent
248-
Write-Output $icsVariable
249-
return $icsVariable
250-
}
251-
}
252-
catch {
253-
Write-Error "An error occurred while creating the ICS file: $_"
254-
throw
255-
}
256-
257307
class rescheduleInfo {
258308
[datetime]$startTime
259309
[datetime]$endTime

0 commit comments

Comments
 (0)