|
CURRENT_DATETIME = new Date().toJSON().replace(/[-:]/g, '').split(".")[0]; |
|
|
|
// holds multiple calendars |
|
out = {} |
|
|
|
// appends key and value to the output variable |
|
function append(cal, k, v) { |
|
if (out[cal] === undefined) { |
|
out[cal] = ""; |
|
append(cal, "BEGIN", "VCALENDAR"); |
|
append(cal, "VERSION", "2.0"); |
|
append(cal, "CALSCALE", "GREGORIAN"); |
|
append(cal, "PRODID", "//Tiago Dias//Fenix Timetables//EN") |
|
} |
|
|
|
out[cal] += k + ":" + v + '\r\n'; |
|
} |
|
|
|
// pads a string to length 2 with zeros |
|
function pad2(s) { |
|
return new String(s).padStart(2, 0); |
|
} |
|
|
|
// formats YYYY-MM-DD/HH:MM to YYYYMMDDTHHMMSS |
|
function dateformat(d, h) { |
|
return d.replace(/-/g, '') + |
|
"T" + h.replace(/:/g, '') + "00"; |
|
} |
|
|
|
function date2ical(date, hour) { |
|
return date.getFullYear() + pad2(date.getMonth()) + pad2(date.getDate()) + "T" + hour.replace(/:/g, '') + "00"; |
|
} |
|
|
|
function abr2name() { |
|
strings = [] // holds table strings in order |
|
table = {} |
|
|
|
$($(".mtop2 > table")[1]).find('td').each(function(i, x) {strings.push($(x).text())}); |
|
|
|
for (i = 0; i < strings.length; i += 3) { |
|
k = strings[i]; |
|
v = strings[i + 2]; |
|
|
|
table[k] = v; |
|
} |
|
|
|
return table; |
|
} |
|
|
|
// download ical |
|
function download(cal, filename) { |
|
append(cal, "END", "VCALENDAR"); |
|
|
|
var element = document.createElement('a'); |
|
element.setAttribute('href', 'data:text/calendar' + ',' + encodeURIComponent(out[cal])); |
|
element.setAttribute('download', filename); |
|
element.style.display = 'none'; |
|
document.body.appendChild(element); |
|
element.click(); |
|
document.body.removeChild(element); |
|
} |
|
|
|
names = abr2name(); |
|
|
|
$(".period-first-slot").each(function(i, slot) { |
|
// cell text |
|
// ["DCObj", "", "(T)", "8.2.30"] |
|
data = $(this).text().split('\xa0'); |
|
cal = data[2].replace(/[()]/g, ''); |
|
data[0] = names[data[0]]; |
|
|
|
append(cal, "BEGIN", "VEVENT"); |
|
append(cal, "SUMMARY", data[2] + " " + data[0]); |
|
append(cal, "LOCATION", data[3]); |
|
append(cal, "DTSTAMP", CURRENT_DATETIME); |
|
|
|
// cell title |
|
// line 0 HH:MM-HH:MM |
|
// line 1+ YYYY-MM-DD |
|
title = $(this).attr("title").split('\n'); |
|
|
|
hours = title.shift().split("-"); // hours ["HH:MM", "HH:MM"] |
|
r_dates = title; // remaining dates |
|
|
|
f_date = r_dates[0]; // first date |
|
l_date = r_dates[r_dates.length - 1]; // last date |
|
|
|
// unique ID |
|
append(cal, "UID", data[0] + "-" + data[3] + "-" + f_date + "-" + hours[0] + "-" + hours[1]); |
|
|
|
// first occurence |
|
append(cal, "DTSTART", dateformat(f_date, hours[0])); |
|
append(cal, "DTEND", dateformat(f_date, hours[1])); |
|
|
|
// recurrence rule |
|
append(cal, "RRULE", "FREQ=WEEKLY;COUNT=" + r_dates.length); |
|
|
|
// holiday exceptions |
|
limit = new Date(l_date); |
|
date = new Date(f_date); |
|
dates = r_dates.slice(); |
|
while (date < limit) { |
|
curr = new Date(dates.shift()); |
|
|
|
// holiday |
|
while (curr - date > 550000000) { |
|
append(cal, "EXDATE", date2ical(date, hours[0])); |
|
date.setDate(date.getDate() + 7); |
|
} |
|
|
|
date.setDate(date.getDate() + 7); |
|
} |
|
|
|
append(cal, "END", "VEVENT"); |
|
}); |
|
|
|
for (var cal in out) { |
|
download(cal, cal + '.ics'); |
|
} |