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
|
const axios = require('axios');
const pronote = require('pronote-api');
const config = require('../../app.json');
const url = config.pronote.url;
const username = config.pronote.user;
const password = config.pronote.password;
process.on('unhandledRejection', (e) => {
console.error(e);
process.exit(2);
})
async function getTimetable(offset, last) {
const timetable = lessons;
if (last) {
let list = timetable.filter(i => new Date(i.from.toDateString()).getTime() === new Date(new Date(new Date().toDateString()).getTime() + 86400000 * offset).getTime()).filter(i => !i.isCancelled);
let lastClass = list[list.length - 1];
return lastClass ? lastClass["to"] : null;
} else {
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;
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());
global.lastClassTime = await getTimetable(offset, true);
} else {
global.wakeUpTime = null;
global.wakeUpClockR = null;
global.wakeUpClockC = null;
global.firstClassTime = null;
global.firstClassClockC = null;
global.firstClassClockR = null;
global.lastClassTime = 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 () => {
console.log("Logging in...");
console.log("URL is", url + ", username is", username + ", password is", password);
global.session = await pronote.login(url, username, password);
console.log("Gathering timetable...");
global.lessons = await session.timetable(new Date(new Date() - (86400000 * 3)), new Date(new Date().getTime() + (86400000 * 7)));
console.log("Processing...");
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] = {
end: null,
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;
json[date].end = lastClassTime ?? null;
}
console.log(json);
require('fs').writeFileSync("../../data/school.json", JSON.stringify(json));
})()
|