-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (74 loc) · 3 KB
/
app.js
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
const WebUntis = require('webuntis');
const osascript = require('node-osascript');
const macOSCalendarName = "Calendar";
const untis = new WebUntis(
'school',
'username',
'password',
'servername.webuntis.com'
);
var dateArg = process.argv.slice(2);
var firstDate = new Date(dateArg[0]);
var lastDate = new Date(new Date().setDate(firstDate.getDate() + 4));
untis
.login()
.then(() => {
return untis.getOwnTimetableForRange(firstDate, lastDate);
})
.then(timetable => {
timetable.sort((a, b) => a.date - b.date);
var days = [];
var currentDay = [];
var currentDate = timetable[0].date;
var dayIndex = 0;
timetable.forEach(function (lesson) {
if (lesson.date != currentDate) {
currentDay.sort((a, b) => a.startTime - b.startTime);
days[dayIndex] = currentDay;
currentDay = [];
dayIndex += 1;
currentDate = lesson.date;
currentDay.push(lesson);
} else {
currentDay.push(lesson);
}
});
currentDay.sort((a, b) => a.startTime - b.startTime);
days[dayIndex] = currentDay;
var weekDays = [];
days.forEach(function (lessons) {
var highestEndTime = 0;
lessons.forEach(function (lesson) {
if (lesson.endTime >= highestEndTime) {
highestEndTime = lesson.endTime;
}
});
weekDays.push(highestEndTime);
});
console.log("\n\n");
for (var i = 0; i < 5; i++) {
var today = new Date(new Date().setDate(firstDate.getDate() + i));
var endTime = weekDays[i].toString();
var endH = endTime.substring(0, 2);
var endM = endTime.substring(2, 4);
console.log(`Your last lesson on ${today.toLocaleDateString('en-EN', { weekday: 'long'})} ends at ${endH}:${endM}.`);
var eventCommand = `
Tell application "Calendar"
activate
tell calendar "${macOSCalendarName}"
set eventDate to (current date)
set day of eventDate to ${today.getDate()}
set month of eventDate to ${today.getMonth()+1}
set year of eventDate to ${today.getFullYear()}
set hours of eventDate to 7
set minutes of eventDate to 30
make new event at end with properties {description:"This event has been generated automatically by LastLesson for Untis (https://github.com/laurensk/webuntis-lastlesson).", summary:"Last lesson ends at ${endH}:${endM}", location:"LastLesson for Untis", start date:eventDate, end date:eventDate}
end tell
reload calendars
end tell`;
osascript.execute(eventCommand, function (err, result, raw) {
if (err) return console.error(err);
});
}
console.log("\n\n");
});