-
Notifications
You must be signed in to change notification settings - Fork 2
/
gunn.js
403 lines (381 loc) · 14.2 KB
/
gunn.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
(() => {
/* PARSING */
const EARLIEST_AM_HOUR = 6;
const PASSING_PERIOD_LENGTH = 10;
const DOUBLE_FLEX_LENGTH = 80; // longer flex might not be double - see 2018-10-10
const HTMLnewlineRegex = /<\/?(p|div|br).*?>|\),? *(?=[A-Z0-9])/g;
const noHTMLRegex = /<.*?>/g;
const noNbspRegex = / /g;
const timeGetterRegex = /\(?(1?[0-9])(?::([0-9]{2}))? *(?:am)? *(?:-|–) *(1?[0-9])(?::([0-9]{2}))? *(noon|pm)?\)?/;
const newLineRegex = /\r?\n/g;
// Detect PeriodE etc (2020-03-31)
const getPeriodLetterRegex = /(?:\b|PERIOD)([A-G1-7])\b/;
const selfGradeRegex = /(1?[9012](?:\s*-\s*1?[9012])?)(?:th)?|(freshmen|sophomore|junior|senior|all)/gi;
const periodSelfGradeRegex = /self for (.+?) grade|self for (freshmen|sophomore|junior|senior|all)/gi;
const gradeToInt = {'9': 1, '10': 2, '11': 4, '12': 8, freshmen: 1, sophomore: 2, junior: 4, senior: 8, all: 15};
const defaultSelf = 0b1111;
function getSELFGrades(text) {
let grades = 0;
text.replace(selfGradeRegex, (match, grade) => {
if (grade && grade.includes('-')) {
const [start, finish] = grade.split('-').map(Number);
for (let i = start; i <= finish; i++) {
grades += gradeToInt[i];
}
} else {
grades += gradeToInt[grade || match] || 0;
}
});
return grades;
}
function parseAlternate(summary, description) {
if (/schedule|extended|lunch/i.test(summary)) {
if (!description) return;
description = '\n' + description.replace(HTMLnewlineRegex, '\n').replace(noHTMLRegex, '').replace(noNbspRegex, ' ');
const periods = [];
description.split(newLineRegex).forEach(str => {
let times;
let name = str.replace(timeGetterRegex, (...matches) => {
times = matches;
return '';
}).trim();
if (!times) return;
let [, sH, sM = 0, eH, eM = 0, pm] = times;
sH = +sH; sM = +sM; eH = +eH; eM = +eM;
if (sH < EARLIEST_AM_HOUR || pm === 'pm') sH += 12;
if (eH < EARLIEST_AM_HOUR || pm === 'pm') eH += 12;
let startTime = sH * 60 + sM,
endTime = eH * 60 + eM;
// handle duplicate periods
const isRedundant = periods.find(pd => {
if (pd.retire) return false;
if (pd.start === startTime && pd.end === endTime) {
// duplicate period
pd.raw += '\n' + name;
return true;
} else if (pd.start <= startTime && pd.end >= endTime) {
// longer period takes place during this period
name += '\n' + pd.raw;
// assumes there's passing period
const beforeEnd = startTime - PASSING_PERIOD_LENGTH;
const afterStart = endTime + PASSING_PERIOD_LENGTH;
// TEMP: assumes that there can't be both a beforePart and an afterPart
if (beforeEnd - pd.start > 0) {
pd.end = beforeEnd;
} else if (pd.end - afterStart > 0) {
pd.start = afterStart;
} else {
pd.retire = true;
}
} else if (pd.start >= startTime && pd.end <= endTime) {
// shorter period takes place during this period
pd.raw += '\n' + name;
// same problems here as above
const beforePart = pd.start - PASSING_PERIOD_LENGTH;
const afterPart = pd.end + PASSING_PERIOD_LENGTH;
if (beforePart - startTime > 0) {
endTime = beforePart;
} else if (endTime - beforePart > 0) {
startTime = beforePart;
} else {
return true;
}
}
// assumes there won't be a case like (8:40-9:10) and (8:30 - 9:00)
});
if (!isRedundant) periods.push({
raw: name,
start: startTime,
end: endTime
});
});
return periods.filter(pd => {
if (pd.retire) return false;
const period = identifyPeriod(pd.raw);
pd.period = period;
if (period === 's') {
periods.selfInSchedule = (periods.selfInSchedule || 0) + 1;
periodSelfGradeRegex.lastIndex = 0;
const selfSlice = periodSelfGradeRegex.exec(pd.raw);
if (selfSlice) {
pd.selfGrades = getSELFGrades(selfSlice[1] || selfSlice[2]) || defaultSelf;
} else {
pd.selfGrades = defaultSelf;
}
}
delete pd.raw;
return period;
}).sort((a, b) => a.start - b.start);
} else if (/holiday|no\s(students|school)|break|development/i.test(summary)) {
if (description) return;
return null;
}
}
function identifyPeriod(name) {
name = name.toUpperCase();
if (~name.indexOf('PERIOD')) {
let letter = getPeriodLetterRegex.exec(name);
if (letter) {
return isNaN(+letter[1])
? // Letter period
letter[1]
: // Number period
' ABCDEFG'[letter[1]];
}
}
if (~name.indexOf('SELF')) return 's';
// Ignore staff classes (for now); should be before flex so that
// "Staff Meeting, CAASPP training for all" (2020-03-11) isn't interpreted
// as flex
else if (~name.indexOf('STAFF') || ~name.indexOf('MEETING')) return;
else if (~name.indexOf('FLEX')
|| ~name.indexOf('ASSEMBL') // assembly, assemblies
|| ~name.indexOf('ATTEND') // HACK to detect PSAT day (2018-10-10)
|| ~name.indexOf('TUTORIAL')
|| ~name.indexOf('CAASPP')) // CAASPP week (2020-03-17 ish)
return 'f';
else if (~name.indexOf('BRUNCH') || ~name.indexOf('BREAK')) return 'b';
// 'UNCH' intentional - misspelling on 2019-03-26
else if (~name.indexOf('UNCH') || ~name.indexOf('TURKEY')) return 'l';
else if (~name.indexOf('TOGETHER')) return 'g';
else return name;
}
function parseEvents(events, dateObj) {
const dateName = dateObj.toISOString().slice(5, 10);
const weekDay = dateObj.getUTCDay();
const oldEntry = scheduleData[dateName];
let self, alternate, overrideSELF = false;
// duplicate events happen infrequently; they're not a concern
events.forEach(({summary = '', description}) => {
if (summary.includes('SELF') && summary.includes('grade')) {
const grades = getSELFGrades(summary);
if (grades > 0) {
self = grades || defaultSelf;
return;
}
}
// HACK to prevent back-to-school schedule from overriding alternate schedule (2018-08-30)
if (summary.includes('Back-to-School')) return;
const schedule = parseAlternate(summary, description);
if (schedule !== undefined) {
if (schedule) {
if (schedule.selfInSchedule) overrideSELF = true;
for (let i = 0; i < schedule.length; i++) {
const pd = schedule[i];
if (pd.period === 'b' || pd.period === 'l') {
// remove passing periods from breaks
if (i === 0) schedule.splice(i--, 1);
else if (i === schedule.length - 1) schedule.splice(i--, 1);
else {
pd.end = schedule[i + 1].start - PASSING_PERIOD_LENGTH;
}
}
}
}
// latter part is so no school days on a weekend isn't considered alternate
if (schedule || normalSchedules[weekDay].length)
alternate = schedule || [];
}
});
if (alternate) {
if (self && !overrideSELF) {
alternate.forEach(pd => {
if (pd.period === 'f') {
pd.period = 's';
pd.selfGrades = self;
}
});
}
scheduleData[dateName] = alternate;
return JSON.stringify(oldEntry) !== JSON.stringify(alternate);
} else {
/*
// this is hardcoded, so if say SELF gets moved to Wednesday, this will need changing
if (weekDay === 4 && self !== defaultSelf) {
// if it's freshmen-only SELF or no SELF on Thursday :O
const clone = JSON.parse(JSON.stringify(normalSchedules[weekDay]));
if (self) {
clone[2].selfGrades = self;
} else {
clone[2].period = 'f';
}
scheduleData[dateName] = clone;
return !oldEntry;
} else if (weekDay === 2 && self) {
// when there's SELF on Tuesday
const clone = JSON.parse(JSON.stringify(normalSchedules[weekDay]));
clone[2].period = 's'; // hardcoded
clone[2].selfGrades = self;
scheduleData[dateName] = clone;
return !oldEntry;
} else { */
scheduleData[dateName] = null;
return oldEntry;
// }
}
}
/* ENCODING */
const selfCharOffset = 72;
const alternateRegex = /([A-GblfgI-W])([\dab]{3})([\dab]{3})/g;
function decodeStoredAlternates(string) {
const lines = string.split('|');
const schedules = {};
lines.forEach(m => {
const month = String(parseInt(m[0], 36) + 1).padStart(2, '0');
m.slice(1).split('!').forEach(d => {
const schedule = [];
if (d.length > 1) {
d.slice(1).replace(alternateRegex, (_, period, start, end) => {
const periodData = {
period: period,
start: parseInt(start, 12),
end: parseInt(end, 12)
};
if (period >= 'I' && period <= 'W') {
periodData.period = 's';
periodData.selfGrades = period.charCodeAt() - selfCharOffset;
}
schedule.push(periodData);
});
}
schedules[month + '-' + String(parseInt(d[0], 36)).padStart(2, '0')] = schedule;
});
});
return schedules;
}
function encodeStoredAlternates(schedules) {
const schedMonths = {};
Object.keys(schedules).forEach(day => {
if (!schedules[day]) return;
let [month, date] = day.split('-').map(Number);
month = (month - 1).toString(36);
date = date.toString(36);
if (schedMonths[month]) schedMonths[month] += '!';
else schedMonths[month] = month;
schedMonths[month] += date;
schedMonths[month] += schedules[day].map(({period, start, end, selfGrades}) => {
if (period.length !== 1) return '';
else return (period === 's' ? String.fromCharCode((selfGrades || defaultSelf) + selfCharOffset) : period)
+ start.toString(12).padStart(3, '0') + end.toString(12).padStart(3, '0')
}).join('');
});
return Object.values(schedMonths).join('|');
}
/* NORMAL SCHEDULE */
const normalSchedules = [
[],
[
{period: 'A', start: 10 * 60 + 0, end: 10 * 60 + 30},
{period: 'B', start: 10 * 60 + 40, end: 11 * 60 + 10},
{period: 'C', start: 11 * 60 + 20, end: 11 * 60 + 50},
{period: 'D', start: 12 * 60 + 0, end: 12 * 60 + 35},
{period: 'l', start: 12 * 60 + 35, end: 13 * 60 + 5},
{period: 'E', start: 13 * 60 + 15, end: 13 * 60 + 45},
{period: 'F', start: 13 * 60 + 55, end: 14 * 60 + 25},
{period: 'G', start: 14 * 60 + 35, end: 15 * 60 + 5}
],
[
{period: 'A', start: 9 * 60 + 0, end: 10 * 60 + 15},
{period: 'B', start: 10 * 60 + 25, end: 11 * 60 + 40},
{period: 'l', start: 11 * 60 + 40, end: 12 * 60 + 10},
{period: 'C', start: 12 * 60 + 20, end: 13 * 60 + 40},
{period: 'D', start: 13 * 60 + 50, end: 15 * 60 + 5},
{period: 'f', start: 15 * 60 + 10, end: 15 * 60 + 40}
],
[
{period: 'E', start: 9 * 60 + 40, end: 10 * 60 + 55},
{period: 'g', start: 11 * 60 + 5, end: 11 * 60 + 40},
{period: 'l', start: 11 * 60 + 40, end: 12 * 60 + 10},
{period: 'F', start: 12 * 60 + 20, end: 13 * 60 + 40},
{period: 'G', start: 13 * 60 + 50, end: 15 * 60 + 5},
{period: 'f', start: 15 * 60 + 10, end: 15 * 60 + 40}
],
[
{period: 'A', start: 9 * 60 + 0, end: 10 * 60 + 15},
{period: 'B', start: 10 * 60 + 25, end: 11 * 60 + 40},
{period: 'l', start: 11 * 60 + 40, end: 12 * 60 + 10},
{period: 'C', start: 12 * 60 + 20, end: 13 * 60 + 40},
{period: 'D', start: 13 * 60 + 50, end: 15 * 60 + 5},
{period: 'f', start: 15 * 60 + 10, end: 15 * 60 + 40}
],
[
{period: 'E', start: 9 * 60 + 40, end: 10 * 60 + 55},
{period: 's', start: 11 * 60 + 5, end: 11 * 60 + 40, selfGrades: defaultSelf},
{period: 'l', start: 11 * 60 + 40, end: 12 * 60 + 10},
{period: 'F', start: 12 * 60 + 20, end: 13 * 60 + 40},
{period: 'G', start: 13 * 60 + 50, end: 15 * 60 + 5}
],
[]
];
/* GET SCHEDULE */
const FIRST_DAY = Date.UTC(2020, 7, 17);
const LAST_DAY = Date.UTC(2021, 5, 3);
let scheduleData = {};
function getSchedule(dateObj) {
if (dateObj.getTime() < FIRST_DAY || dateObj.getTime() > LAST_DAY) {
const schedule = [];
schedule.noSchool = true;
schedule.summer = true;
schedule.date = dateObj;
return schedule;
}
const dateName = dateObj.toISOString().slice(5, 10);
let schedule = JSON.parse(JSON.stringify(
scheduleData[dateName] || normalSchedules[dateObj.getUTCDay()],
(_, value) => typeof value === 'function' ? value(dateObj) : value
));
if (schedule.length === 0) {
schedule.noSchool = true;
} else if (!options.showSELF) {
schedule.forEach(pd => {
if (pd.period === 's') pd.period = 'f';
});
}
if (scheduleData[dateName]) {
schedule.alternate = true;
}
schedule.date = dateObj;
return schedule;
}
/* PERIOD CARD NOTES */
const gradeName = ['freshmen', 'sophomores', 'juniors', 'seniors'];
function getNote(periodData) {
return periodData.period === 's' ? 'For ' + (periodData.selfGrades || defaultSelf).toString(2).split('').reverse().map((n, i) => n === '1' ? gradeName[i] : '').filter(n => n).join(', ') : null;
}
window.ugwishaOptions = {
parseEvents,
getSchedule,
getNote,
saveScheduleData() {
return encodeStoredAlternates(scheduleData);
},
prepareScheduleData(storedSchedules) {
scheduleData = decodeStoredAlternates(storedSchedules);
},
SCHEDULE_DATA_KEY: '[ugwisha] alternates-2020-21-v2', // change when new school year
/* FETCHING */
SCHEDULES_CALENDAR_ID: '[email protected]',
EVENTS_CALENDAR_ID: '[email protected]',
CALENDAR_KEYWORDS: ['self', 'schedule', 'extended', 'holiday', 'no students', 'no school', 'break', 'development'],
// please set this to your own if you fork Ugwisha, thanks
GOOGLE_API_KEY: 'AIzaSyDBYs4DdIaTjYx5WDz6nfdEAftXuctZV0o',
FIRST_DAY, LAST_DAY,
/* DEFAULTS */
DEFAULT_NAMES: {
A: 'Period 1', B: 'Period 2', C: 'Period 3', D: 'Period 4',
E: 'Period 5', F: 'Period 6', G: 'Period 7',
b: 'Brunch', l: 'Lunch', f: 'Tutorial', s: 'SELF', g: 'Gunn Together'
},
DEFAULT_COLOURS: {
A: 'de935f', B: '81a2be', C: 'cc6666', D: 'f0c674',
E: 'b294bb', F: 'b5bd68', G: '8abeb7',
b: null, l: null, f: 'e0e0e0', s: '282a2e', g: '282a2e'
},
/* THEME */
THEME_COLOUR: '#ff5959',
DEFAULT_FAVICON_URL: './images/logo/192.png',
APP_NAME: 'Ugwisha',
PERIOD_OPTION_PREFIX: '',
UPDATER_URL: '/ugwisha-updater.html'
};
})();