-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebuntis.pl
239 lines (225 loc) · 7.7 KB
/
webuntis.pl
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
232
233
234
235
236
237
238
239
#!/usr/bin/perl
# Copyright 2024 Nils Knieling
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# WebUntis Bot
# Get notified when there is a change in the school schedule.
# Debian:
# sudo apt update && \
# sudo apt install \
# libapp-options-perl \
# libdigest-sha-perl \
# libwww-perl \
# libjson-xs-perl
# curl -O "https://raw.githubusercontent.com/Cyclenerd/toolbox/master/webuntis.pl"
# perl webuntis.pl --help
BEGIN {
$VERSION = "1.1.2";
}
use utf8;
use strict;
use warnings;
use Data::Dumper;
use Digest::SHA qw(sha256_hex);
use Encode;
use HTML::Entities;
use HTTP::Request::Common;
use JSON::XS;
use LWP::UserAgent;
use POSIX qw(strftime);
use App::Options (
option => {
school => { type => "string", required => 1, description => "WebUntis School ID [required]" },
class => { type => "string", required => 1, description => "Filter class aka group ID [required]" },
class2 => { type => "string", required => 0, description => "Filter 2nd class aka group ID [optional]" },
offset => { type => "int", required => 1, description => "Date offset, look into the past (-1, ...) or future (1, ...) [default: 0 (today)]", default => 0 },
key => { type => "string", required => 0, description => "The Pushover key (not e-mail address) of your user or group [optional]" },
token => { type => "string", required => 0, description => "Your application's API token [optional]", secure => 1 },
},
);
binmode(STDOUT, ":utf8");
my $inputSchool = $App::options{school} || "";
my $inputClass = $App::options{class} || "";
my $inputClass2 = $App::options{class2} || "";
my $inputDateOffset = $App::options{offset} || "0";
my $inputPushoverKey = $App::options{key} || "";
my $inputPushoverToken = $App::options{token} || "";
print "Class: $inputClass\n";
print "2nd Class: $inputClass2\n" if $inputClass2;
# Backslash non-"word" ASCII characters for safer regex
my $filterClass = quotemeta($inputClass);
my $filterClass2 = quotemeta($inputClass2);
my $today = strftime "%Y%m%d", localtime;
# WebUntis JSON message body
# Source: https://ikarus.webuntis.com/WebUntis/monitor?school=[SCHOOL-ID]&monitorType=subst&format=SchuelerZweiTage
my $body = qq ~
{
"formatName": "SchuelerZweiTage",
"schoolName": "$inputSchool",
"date": $today,
"dateOffset": $inputDateOffset,
"strikethrough": true,
"mergeBlocks": true,
"showOnlyFutureSub": true,
"showBreakSupervisions": false,
"showTeacher": true,
"showClass": true,
"showHour": true,
"showInfo": true,
"showRoom": true,
"showSubject": true,
"groupBy": 1,
"hideAbsent": false,
"departmentIds": [],
"departmentElementType": -1,
"hideCancelWithSubstitution": true,
"hideCancelCausedByEvent": false,
"showTime": false,
"showSubstText": true,
"showAbsentElements": [],
"showAffectedElements": [],
"showUnitTime": true,
"showMessages": true,
"showStudentgroup": false,
"enableSubstitutionFrom": true,
"showSubstitutionFrom": 0,
"showTeacherOnEvent": false,
"showAbsentTeacher": true,
"strikethroughAbsentTeacher": true,
"activityTypeIds": [],
"showEvent": true,
"showCancel": true,
"showOnlyCancel": false,
"showSubstTypeColor": false,
"showExamSupervision": false,
"showUnheraldedExams": false
}
~;
print "Get WebUnits data...\n";
my $ua = LWP::UserAgent->new;
my $request = POST "https://ikarus.webuntis.com/WebUntis/monitor/substitution/data?school=$inputSchool";
$request->header('Content-Type' => 'application/json','Content-Length' => length($body));
$request->content($body);
my $response = $ua->request($request);
unless ($response->is_success) {
die "ERROR: Cannot access WebUnits API! Status: '". $response->status_line ."'\n";
}
my $json = JSON::XS->new->utf8->decode($response->content);
my $payload = $json->{'payload'} or die "ERROR: No data (payload) in JSON!";
my $lastUpdate = $payload->{'lastUpdate'} or die "ERROR: Last update date missing!\n";
my $date = $payload->{'date'} or die "ERROR: Date missing!\n";
die "ERROR: Date '$date' invalid!\n" unless ($date =~ /\d{4}\d{2}\d{2}/);
my $weekDay = $payload->{'weekDay'} or die "ERROR: Day of week missing!\n";
print "Date: $date\n";
print "Day: $weekDay\n";
print "Last update: $lastUpdate\n";
#print Dumper($json);
my @texts;
foreach my $row (@{$payload->{'rows'}}) {
my $group = $row->{'group'} || "";
my %cells;
# hour 0
# class 1
# subject 2
# room 3
# teacher 4
# info 5
# text 6
foreach my $cell (0..6) {
$cells{$cell} = $row->{'data'}[$cell] || "";
# Remove HTML elements
$cells{$cell} =~ s|<span class="substMonitorSubstElem">|⚠️ |g;
$cells{$cell} =~ s|<span class="cancelStyle">|❌ |g;
$cells{$cell} =~ s|<span class="[\d\w]+">||g;
$cells{$cell} =~ s|</span>||g;
$cells{$cell} = decode_entities($cells{$cell});
}
my $class = $cells{'1'};
# Skip if class from input not found
if ($filterClass2) {
unless ($group =~ /$filterClass/i || $group =~ /$filterClass2/i) {
next;
}
} else {
unless ($group =~ /$filterClass/i) {
next;
}
}
# Check if complete cell value is canceled
if ($row->{'cellClasses'}) {
foreach my $cell (keys %cells) {
if ($row->{'cellClasses'}->{$cell}) {
foreach $class (@{$row->{'cellClasses'}->{$cell}}) {
if ($class =~ /cancel/) {
$cells{$cell} = "❌ ". $cells{$cell};
}
}
}
}
}
my $hour = $cells{'0'} || "???";
# Format hours for better sort
if ($hour =~ /^(\d+)$/) { $hour = sprintf("%#.2d", $1) }
if ($hour =~ /^(\d+) - (\d+)$/) { $hour = sprintf("%#.2d - %#.2d", $1, $2) }
my $text = sprintf("<b>%s</b>", $hour);
$text .= sprintf(", %s", $cells{'2'}) if $cells{'2'};
$text .= sprintf(", %s", $cells{'3'}) if $cells{'3'};
$text .= sprintf(", %s", $cells{'4'}) if $cells{'4'};
$text .= sprintf(", <i>%s</i>", $cells{'5'}) if $cells{'5'};
$text .= sprintf(", <i>%s</i>", $cells{'6'}) if $cells{'6'};
push(@texts, "$text");
}
# Sort text
my $message;
foreach my $text (sort @texts) {
print "$text\n";
$message .= "$text\n\n";
}
# Notify via Pushover
if ($inputPushoverKey && $inputPushoverToken && $message) {
# Hash message to avoid duplicate notifications
my $messageHash = sha256_hex(encode_utf8("$message"));
my $filenameLastMessage = "/tmp/webuntis.$inputSchool.last.message.txt";
my $lastMessageHash = "";
if (-f $filenameLastMessage ) {
open(my $fhLastMessage, '<', $filenameLastMessage);
$lastMessageHash = <$fhLastMessage>;
close($fhLastMessage);
}
# Check last message
if ($lastMessageHash && $lastMessageHash eq $messageHash) {
print "SKIP: Already notified. No change to the last run.";
} else {
print "Notify via Pushover...\n";
my %post = (
"token" => $inputPushoverToken,
"user" => $inputPushoverKey,
"title" => $weekDay,
"message" => $message,
"html" => "1",
"url" => "https://ikarus.webuntis.com/WebUntis/monitor?school=$inputSchool&monitorType=subst&format=SchuelerZweiTage",
"url_title" => "WebUntis"
);
my $pushover = LWP::UserAgent->new()->post(
"https://api.pushover.net/1/messages.json", \%post
);
if ($pushover->is_success) {
print "OK: Message sent successfully.\n";
open(my $fhThisMessage, '>', "/tmp/webuntis.$inputSchool.last.message.txt");
print $fhThisMessage "$messageHash";
close($fhThisMessage);
} else {
die "ERROR: Message could not be sent! Status: '". $response->status_line ."'\n";
}
}
}