summaryrefslogtreecommitdiff
path: root/school/index.js
blob: 962cb7e9c3c66332288ae966045d590b949ad836 (plain)
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
const axios = require('axios');
const pronote = require('pronote-api');
const config = require('../includes/app.json');

const url = config.pronote.url;
const username = config.pronote.user;
const password = config.pronote.password;

async function getTimetable(offset) {
    const timetable = lessons;

    let nextClass = timetable.filter(i => new Date(i.from.toDateString()).getTime() === new Date(new Date(new Date().toDateString()).getTime() + 86400000 * offset).getTime()).filter(i => !i.isCancelled)[0];

    return nextClass ? nextClass["from"] : null;
}

global.wakeUpTime = null;
global.wakeUpTimeDiff = null;
global.sleepTime = null;
global.sleepTimeDiff = null;
global.sleepClockR = null;
global.sleepClockC = null;
global.wakeUpClockC = null;
global.wakeUpClockR = null;
global.firstClassTime = null;
global.firstClassClockC = null;
global.firstClassClockR = null;

async function nightWarning() {
    if (global.sleepTimeDiff) {
        await notification("🌙 Good evening!", "You are going to sleep " + global.sleepTimeDiff + ", " + global.sleepClockC + " BST (" + global.sleepClockR + " CEST) so you can wake up " + global.wakeUpTimeDiff + ", " + global.wakeUpClockC + " BST (" + global.wakeUpClockR + " CEST). You will be reminded when it's time to go to sleep.", 3);
    } else {
        await notification("🌙 Good evening!", "You don't need to wake up at a specific time tomorrow, enjoy!", 3);
    }
}

async function nightReminder() {
    if (global.sleepTimeDiff) {
        await notification("🌙 Now let's sleep!", "You should get ready to sleep now so you can wake up " + global.wakeUpTimeDiff + ", " + global.wakeUpClockC + " BST (" + global.wakeUpClockR + " CEST).", 3);
    }
}

function digits(input) {
    if (input < 10) {
        return "0" + input;
    } else {
        return input;
    }
}

global.tzOffset = Math.abs(new Date().getTimezoneOffset() / 60);

function formatHours(hours) {
    return hours >= 24 ? hours - 24 : hours;
}

async function updateSleepTime(offset) {
    let classTime = await getTimetable(offset);
    let classTime2 = await getTimetable(offset + 1);
    let tzOffset = global.tzOffset;

    if (classTime) {
        global.wakeUpTime = new Date(classTime.getTime() - 5100000);

        if (wakeUpTime.getHours() > 8 || (wakeUpTime.getHours() === 8 && wakeUpTime.getMinutes() > 0)) {
            global.wakeUpTime = new Date(wakeUpTime.toISOString().split("T")[0] + "T08:00:00+0" + (tzOffset) + ":00");
        }

        if (classTime.toTimeString().split(" ")[0] === "08:00:00") {
            global.wakeUpTime = new Date(wakeUpTime.toISOString().split("T")[0] + "T06:30:00+0" + (tzOffset) + ":00");
        }

        if (classTime.toTimeString().split(" ")[0] === "09:00:00") {
            global.wakeUpTime = new Date(wakeUpTime.toISOString().split("T")[0] + "T07:00:00+0" + (tzOffset) + ":00");
        }

        if (classTime.toTimeString().split(" ")[0] === "10:00:00") {
            global.wakeUpTime = new Date(wakeUpTime.toISOString().split("T")[0] + "T07:00:00+0" + (tzOffset) + ":00");
        }
        global.wakeUpClockR = formatHours(wakeUpTime.getUTCHours() + tzOffset) + ":" + digits(wakeUpTime.getUTCMinutes());
        global.wakeUpClockC = formatHours(wakeUpTime.getUTCHours() + (tzOffset - 1)) + ":" + digits(wakeUpTime.getUTCMinutes());

        global.firstClassTime = classTime;
        global.firstClassClockC = formatHours(firstClassTime.getUTCHours() + (tzOffset - 1)) + ":" + digits(firstClassTime.getUTCMinutes());
        global.firstClassClockR = formatHours(firstClassTime.getUTCHours() + tzOffset) + ":" + digits(firstClassTime.getUTCMinutes());
    } else {
        global.wakeUpTime = null;
        global.wakeUpClockR = null;
        global.wakeUpClockC = null;

        global.firstClassTime = null;
        global.firstClassClockC = null;
        global.firstClassClockR = null;
    }

    if (classTime2) {
        global.wakeUpTime2 = new Date(classTime2.getTime() - 5100000);

        if (wakeUpTime2.getHours() > 8 || (wakeUpTime2.getHours() === 8 && wakeUpTime2.getMinutes() > 0)) {
            global.wakeUpTime2 = new Date(wakeUpTime2.toISOString().split("T")[0] + "T08:00:00+0" + (tzOffset) + ":00");
        }

        if (classTime2.toTimeString().split(" ")[0] === "08:00:00") {
            global.wakeUpTime2 = new Date(wakeUpTime2.toISOString().split("T")[0] + "T06:30:00+0" + (tzOffset) + ":00");
        }

        if (classTime2.toTimeString().split(" ")[0] === "09:00:00") {
            global.wakeUpTime2 = new Date(wakeUpTime2.toISOString().split("T")[0] + "T07:00:00+0" + (tzOffset) + ":00");
        }

        if (classTime2.toTimeString().split(" ")[0] === "10:00:00") {
            global.wakeUpTime2 = new Date(wakeUpTime2.toISOString().split("T")[0] + "T07:00:00+0" + (tzOffset) + ":00");
        }

        global.sleepTime = new Date(global.wakeUpTime2.getTime() - 28800000);

        global.sleepClockR = formatHours(sleepTime.getUTCHours() + tzOffset) + ":" + digits(sleepTime.getUTCMinutes());
        global.sleepClockC = formatHours(sleepTime.getUTCHours() + (tzOffset - 1)) + ":" + digits(sleepTime.getUTCMinutes());
    } else {
        global.sleepTime = null;

        global.sleepClockR = null;
        global.sleepClockC = null;
    }
}

async function display(offset) {
    await updateSleepTime(offset);
    console.log(new Date(new Date().getTime() + 86400000 * offset).toDateString())
    console.log("    First:   " + firstClassTime + " (" + firstClassClockC + ", " + firstClassClockR + ")");
    console.log("    Wake up: " + wakeUpTime + " (" + wakeUpClockC + ", " + wakeUpClockR + ")");
    console.log("    Sleep:   " + sleepTime + " (" + sleepClockC + ", " + sleepClockR + ")");
}

(async () => {
    global.session = await pronote.login(url, username, password);
    global.lessons = await session.timetable(new Date(new Date() - (86400000 * 3)), new Date(new Date().getTime() + (86400000 * 7)));

    await updateSleepTime(0);
    await display(-2);
    console.log("-------------------");
    await display(-1);
    console.log("-------------------");
    await display(0);
    console.log("-------------------");
    await display(1);
    console.log("-------------------");
    await display(2);
    console.log("-------------------");
    await display(3);
    console.log("-------------------");
    await display(4);
    console.log("-------------------");
    await display(5);
    console.log("-------------------");
    await display(6);
    console.log("-------------------");
    await display(7);

    let json = {};

    for (let i = -2; i < 8; i++) {
        let date = new Date(new Date().getTime() + 86400000 * i).toISOString().split("T")[0];

        json[date] = {
            firstClass: {
                timestamp: null,
                cloudburst: null,
                raindrops: null
            },
            wakeUp: {
                timestamp: null,
                cloudburst: null,
                raindrops: null
            },
            sleep: {
                timestamp: null,
                cloudburst: null,
                raindrops: null
            }
        };

        let time = await updateSleepTime(i);

        json[date].firstClass.timestamp = firstClassTime ? firstClassTime.getTime() : null;
        json[date].firstClass.cloudburst = firstClassClockC ?? null;
        json[date].firstClass.raindrops = firstClassClockR ?? null;

        json[date].sleep.timestamp = sleepTime ? sleepTime.getTime() : null;
        json[date].sleep.cloudburst = sleepClockC ?? null;
        json[date].sleep.raindrops = sleepClockR ?? null;

        json[date].wakeUp.timestamp = wakeUpTime ? wakeUpTime.getTime() : null;
        json[date].wakeUp.cloudburst = wakeUpClockC ?? null;
        json[date].wakeUp.raindrops = wakeUpClockR ?? null;
    }

    console.log(json);
    require('fs').writeFileSync("../includes/data/school.json", JSON.stringify(json));
})()