-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
288 lines (259 loc) · 8.66 KB
/
Copy pathscript.js
File metadata and controls
288 lines (259 loc) · 8.66 KB
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
document.getElementById("submitButton").addEventListener("click", function() {
try {
const rawData = document.getElementById("userInput").value;
const ics_events = genorateEvents(rawData);
if (ics_events === "") {
alert("No valid course data found. Please check your input.");
return;
}
const ics_file = wrapEvents(ics_events);
genorateDownloadLink(ics_file);
} catch (error) {
alert("An error occured! Check your input and try again.");
console.error(error)
}
});
function genorateEvents(rawData) {
const lines = rawData.split('\n');
const events = [];
let currentBlock = [];
for (const line of lines) {
if (line.trim() === '' && currentBlock.length === 0) {
continue;
}
currentBlock.push(line);
if (line.includes('CRN:')) {
const event = parseScheduleBlock(currentBlock.join('\n'));
if (event) {
events.push(formatEvent(event));
}
currentBlock = [];
}
}
return events.join('');
}
function parseScheduleBlock(blockText) {
const titleLine = blockText.split('\n').find(line => line.includes('Class Begin:')) || '';
const markdownTitleMatch = titleLine.match(/^\[([^\]]+)\]/);
const title = markdownTitleMatch ? markdownTitleMatch[1].trim() : titleLine.split('|')[0].trim();
const waitlistMatch = blockText.match(/Waitlist Position:\s*(\d+)/i);
if (waitlistMatch && parseInt(waitlistMatch[1], 10) > 0) {
return null;
}
const timeLine = blockText.split('\n').find(line => line.includes('Type: Class') && line.includes(' - ')) || '';
if (timeLine.includes('TBA')) {
return null;
}
const scheduleLine = blockText.split('\n').find(line => line.includes('--') && /Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday/.test(line)) || '';
const scheduleMatch = scheduleLine.match(/([0-9]{2}\/\d{2}\/\d{4})\s*--\s*([0-9]{2}\/\d{2}\/\d{4})\s+(.+)$/);
if (!scheduleMatch) {
return null;
}
const timeMatch = timeLine.match(/(\d{1,2}:\d{2}\s*[AP]M)\s*-\s*(\d{1,2}:\d{2}\s*[AP]M)/i);
if (!timeMatch) {
return null;
}
const locationMatch = timeLine.match(/Location:\s*(.+)$/i);
const location = locationMatch ? locationMatch[1].trim() : '';
const instructorLine = blockText.split('\n').find(line => line.startsWith('Instructor:')) || '';
const instructor = instructorLine
.replace(/^Instructor:\s*/i, '')
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
.replace(/\s*\([^)]+\)\s*$/g, '')
.trim();
const dayMap = {
monday: 'M',
tuesday: 'T',
wednesday: 'W',
thursday: 'R',
friday: 'F',
saturday: 'S',
sunday: 'U'
};
const days = scheduleMatch[3]
.split(',')
.map(day => dayMap[day.trim().toLowerCase()])
.filter(Boolean);
if (days.length === 0) {
const legacyDayMatch = blockText.match(/[MTWRFSU]+/g);
if (legacyDayMatch) {
days.push(...legacyDayMatch[0].split(''));
}
}
return {
title: title,
startTime: timeMatch[1].trim(),
endTime: timeMatch[2].trim(),
days: days,
location: location,
startDate: scheduleMatch[1].trim(),
endDate: scheduleMatch[2].trim(),
instructor: instructor
};
}
function firstOccurrenceOnOrAfter(dateStr, days) {
const dayIndices = {
U: 0,
M: 1,
T: 2,
W: 3,
R: 4,
F: 5,
S: 6
};
const [month, day, year] = dateStr.split('/').map(Number);
const date = new Date(Date.UTC(year, month - 1, day));
for (let i = 0; i < 7; i++) {
const letterForDow = Object.keys(dayIndices).find(letter => dayIndices[letter] === date.getUTCDay());
if (days.includes(letterForDow)) break;
date.setUTCDate(date.getUTCDate() + 1);
}
const adjMonth = String(date.getUTCMonth() + 1).padStart(2, '0');
const adjDay = String(date.getUTCDate()).padStart(2, '0');
return `${adjMonth}/${adjDay}/${date.getUTCFullYear()}`;
}
function formatEvent(event) {
const uid = generateUID()
const title = event.title;
const location = event.location;
const startDate = firstOccurrenceOnOrAfter(event.startDate, event.days);
const endDate = formatDate(event.endDate, event.endTime, true);
const startTime = formatDate(startDate, event.startTime, false);
const endTime = formatDate(startDate, event.endTime, false);
const byDay = event.days.map(d => ({
M: 'MO',
T: 'TU',
W: 'WE',
R: 'TH',
F: 'FR',
S: 'SA',
U: 'SU'
} [d])).join(',');
const instructor = event.instructor;
const icsContent = `
BEGIN:VEVENT
UID:${uid}
DTSTAMP:20240101T000000Z
SUMMARY:${title}
DESCRIPTION:Scheduled Class with ${instructor}
LOCATION:${location}
DTSTART;TZID=America/New_York:${startTime}
DTEND;TZID=America/New_York:${endTime}
RRULE:FREQ=WEEKLY;BYDAY=${byDay};UNTIL=${endDate}Z
STATUS:CONFIRMED
SEQUENCE:0
TRANSP:OPAQUE
BEGIN:VALARM
TRIGGER:-PT15M
ACTION:DISPLAY
END:VALARM
END:VEVENT`;
return icsContent;
}
function getEasternOffsetMinutes(utcGuess) {
const dtf = new Intl.DateTimeFormat('en-US', {
timeZone: 'America/New_York',
hourCycle: 'h23',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
const parts = dtf.formatToParts(utcGuess).reduce((acc, p) => {
if (p.type !== 'literal') acc[p.type] = p.value;
return acc;
}, {});
const asIfUTC = Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute, parts.second);
return (asIfUTC - utcGuess.getTime()) / 60000;
}
function formatDate(date, time, UTC) {
let year;
let month;
let day;
if (date.includes('/')) {
const dateParts = date.split('/');
month = dateParts[0].padStart(2, '0');
day = dateParts[1].padStart(2, '0');
year = dateParts[2];
} else {
const monthMap = {
Jan: '01',
Feb: '02',
Mar: '03',
Apr: '04',
May: '05',
Jun: '06',
Jul: '07',
Aug: '08',
Sep: '09',
Oct: '10',
Nov: '11',
Dec: '12'
};
const [monthStr, dayWithComma, parsedYear] = date.split(' ');
day = dayWithComma.replace(',', '').padStart(2, '0');
month = monthMap[monthStr];
year = parsedYear;
}
const [hour, minutePart] = time.split(':');
const [minute, meridian] = minutePart.split(' ');
let h = parseInt(hour, 10);
if (meridian.toLowerCase() === 'pm' && h !== 12) {
h += 12;
}
if (meridian.toLowerCase() === 'am' && h === 12) {
h = 0;
}
if (!UTC) {
const formattedDate = `${year}${month}${day}`;
const formattedTime = `${String(h).padStart(2, '0')}${minute}00`;
return `${formattedDate}T${formattedTime}`;
}
const naiveUTC = new Date(Date.UTC(Number(year), Number(month) - 1, Number(day), h, Number(minute)));
const offsetMinutes = getEasternOffsetMinutes(naiveUTC);
const actualUTC = new Date(naiveUTC.getTime() - offsetMinutes * 60000);
const y = actualUTC.getUTCFullYear();
const mo = String(actualUTC.getUTCMonth() + 1).padStart(2, '0');
const d = String(actualUTC.getUTCDate()).padStart(2, '0');
const hh = String(actualUTC.getUTCHours()).padStart(2, '0');
const mm = String(actualUTC.getUTCMinutes()).padStart(2, '0');
return `${y}${mo}${d}T${hh}${mm}00`;
}
function wrapEvents(events) {
var wrapped_events = `
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//CalendarGenerator//EN
CALSCALE:GREGORIAN
${events}
END:VCALENDAR`;
var lines = wrapped_events.split('\n');
var cleaned_content = '';
for (var line of lines) {
line = line.trim();
if (line !== '') {
cleaned_content += line + '\r\n';
}
}
return cleaned_content
}
function generateUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
}) + '@northeastern.edu';
}
function genorateDownloadLink(ics_file) {
const blob = new Blob([ics_file], {
type: 'text/calendar'
});
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'calendar.ics';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}