This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
script.js
190 lines (159 loc) · 6.73 KB
/
script.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
/*
** Created with <3 by James Middleton ([email protected])
** ------------------------------
** This script allows Loughborough University students to easily import all lectures as events into a calendar app.
**
** For instructions and PRs, please visit the GitHub repository (https://github.com/james2mid/timetable-vcs).
*/
// -- CONSTANTS
/** The number of milliseconds in one half-hour. */
const HALF_HOUR = 30 * 60 * 1000
/** The number of milliseconds in one hour. */
const HOUR = HALF_HOUR * 2
/** The number of milliseconds in one day. */
const DAY = HOUR * 24
/** Zero-based days of week array. */
const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
// -- UTILITY FUNCTIONS
/** Flattens an array by one level. Included for older browser compatibility (and Edge ;) ). */
function flat (arr) {
return [].concat.apply([], arr)
}
/** Pads a digit with zeroes to return a two-digit string. */
function padZeroes (s) {
return String(s).length === 1 ? `0${s}` : s
}
/** Converts milliseconds to the VCS date format. */
function msToVeventDate (ms) {
const date = new Date(ms)
return `${date.getFullYear()}${padZeroes(date.getUTCMonth() + 1)}${padZeroes(date.getUTCDate())}T${padZeroes(date.getUTCHours())}${padZeroes(date.getUTCMinutes())}${padZeroes(date.getUTCSeconds())}Z`
}
// -- FUNCTIONS USED IN MAIN
/** Gets the day of week (zero-based) of the given row. */
function getRowDay (row) {
let dayText = null
// sessions whose durations overlap, are placed on adjacent rows
// these rows do not contain data about the weekday
// so retrace rows until the current weekday is determined
while (!dayText && $(row).prev()[0] !== row) {
dayText = $(row).find('.weekday').text()
row = $(row).prev()
}
return DAYS.indexOf(dayText)
}
(function main () {
// -- Establish that the correct data is included in the table – prompt to change if not.
const semester =
$('#P2_MY_PERIOD').val() === 'sem1' ? 1 :
$('#P2_MY_PERIOD').val() === 'sem2' ? 2 :
null
if (!semester) {
alert('Ensure that the "Period" dropdown box is set as "Semester 1" or "Semester 2"')
return
}
// -- Parse the table HTML to get the session data – each containing one or multiple events.
const rows = $('.tt_info_row').get()
const sessions = rows.reduce((sessions, row) => {
const day = getRowDay(row)
const cells = $(row).children('td').not('.weekday_col').get()
const rowSessions = cells.reduce((acc, cell) => {
if (cell.classList.contains('new_row_tt_info_cell') || cell.classList.contains('tt_info_cell')) {
// session
acc.sessions.push({
moduleId: $(cell).find('.tt_module_id_row').text(),
moduleName: $(cell).find('.tt_module_name_row').text(),
type: $(cell).find('.tt_modtype_row').text(),
lecturerName: $(cell).find('.tt_lect_row').text(),
room: $(cell).find('.tt_room_row').first().text().replace('...', ''),
buildingName: $($(cell).find('.tt_room_row')[1]).text().replace(/\.\.\.|\(|\)/g, ''),
day,
timeOffset: (() => {
const prevSession = acc.sessions[acc.sessions.length - 1]
return (prevSession ? (prevSession.timeOffset + prevSession.duration) : 0) + acc.gap
})(),
duration: $(cell).attr('colspan') * HALF_HOUR,
weeks: flat(/Sem\s+\d:\s+(.*)$/.exec($(cell).find('.tt_weeks_row').text())[1].split(',').map(x => {
const r = /(\d{1,2})\s+-\s+(\d{1,2})/.exec(x)
return r
// a range, e.g '9 - 11' meaning weeks 9, 10 and 11
? Array.from(Array(r[2] - r[1] + 1)).map((_, i) => +r[1] + i) // expand range
// not a range, e.g. '6' meaning only week 6
: [ +x ]
}))
})
acc.gap = 0
} else {
// gap
acc.gap += HALF_HOUR // each gap is half an hour
}
return acc
}, { sessions: [], gap: 0 }).sessions
sessions.push(...rowSessions)
return sessions
}, [])
// -- Remove the potentially trashy parts of the sessions.
sessions.forEach(session => {
Object.keys(session).forEach(key => {
if (session[key] && typeof session[key] === 'string') {
session[key] = session[key].trim().replace(/\s+/g, ' ')
}
})
})
// -- Convert sessions into individual events.
/** The beginning of the each week as a `Date` in the current semester. Array index equal to week number. */
const weekStartDates = [null, ...$('#P2_MY_PERIOD > option')
.get()
.map(x => x.innerText)
.filter(x => x.includes(`Sem ${semester} - Wk`))
.map(x => /^Sem \d - Wk \d{1,2} \(starting (\d{1,2}-[A-Z]{3}-\d{4})\)$/.exec(x))
.map(x => new Date(x[1]).getTime())
]
/** The start of the days as a `Date` as displayed in the timetable (generally 9AM). */
const timetableStart = +$('.first_time_slot_col').first().text().split(':')[0] * HOUR
/** Every event for the current semester as objects whose keys match the VCS specification. */
const events = sessions.reduce((events, session) => {
events.push(...session.weeks.map(weekNumber => {
const startTime = weekStartDates[weekNumber] + (DAY * session.day) + timetableStart + session.timeOffset
return {
UID: `${startTime}-${session.moduleId}`,
DTSTART: msToVeventDate(startTime),
DTEND: msToVeventDate(startTime + session.duration),
SUMMARY: session.type ? `${session.moduleName} (${session.type})` : session.moduleName,
LOCATION: session.buildingName ? `${session.room} (${session.buildingName})` : session.room,
COMMENT: `${session.type} for ${session.moduleName} (${session.moduleId}) with ${session.lecturerName} in room ${session.room} of ${session.buildingName}`
}
}))
return events
}, [])
// -- Make a long string containing the events following the VCS specification.
const contents = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//James Middleton//NONSGML timetable-vcs V6//EN',
...flat(events.map(data => ([
'BEGIN:VEVENT',
'DTSTAMP:' + msToVeventDate(Date.now()),
...Object.entries(data).map(([key, value]) => `${key}:${value}`),
'END:VEVENT'
]))),
'END:VCALENDAR',
].join('\r\n')
// -- Download the string as a virtual file.
const blob = new Blob([ contents ], {
type: 'text/calendar'
})
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.setAttribute('download', 'lectures.vcs')
a.setAttribute('href', url)
document.body.appendChild(a)
// short delay for the DOM to update
setTimeout(() => {
a.click()
// uses setTimeout so that Edge can download the file
setTimeout(() => {
document.body.removeChild(a)
URL.revokeObjectURL(url)
}, 1000)
}, 100)
})()