-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlorisAPI.js
More file actions
385 lines (345 loc) · 14.5 KB
/
Copy pathlorisAPI.js
File metadata and controls
385 lines (345 loc) · 14.5 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
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
// For support, contact: qfaizaan@gmail.com
require("dotenv").config({ path: ".env.local" });
const axios = require("axios");
const puppeteer = require("puppeteer");
const { createClient } = require("@supabase/supabase-js");
const qs = require("qs");
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY
);
const apiUrl = "https://loris.wlu.ca/register/ssb/registration/";
const coursesURL = "https://loris.wlu.ca/register/ssb/courseSearchResults/courseSearchResults/";
const courseDetailsURL = "https://loris.wlu.ca/register/ssb/searchResults/searchResults/";
const professorAndMeetingTimesURL = "https://loris.wlu.ca/register/ssb/searchResults/getFacultyMeetingTimes";
// Returns the list of terms to scrape based on the current date.
// Winter (Jan–Apr): current Winter + Spring
// Spring (May–Aug): current Spring + Fall + next Winter + next Spring
// Fall (Sep–Dec): current Fall + next Winter + next Spring
function getTermsToScrape() {
const now = new Date();
const month = now.getMonth() + 1;
const year = now.getFullYear();
const term = (y, suffix, season) => ({ code: `${y}${suffix}`, name: `${season} ${y}` });
if (month <= 4) {
return [
term(year, "01", "Winter"),
term(year, "05", "Spring"),
];
} else if (month <= 8) {
return [
term(year, "05", "Spring"),
term(year, "09", "Fall"),
term(year + 1, "01", "Winter"),
term(year + 1, "05", "Spring"),
];
} else {
return [
term(year, "09", "Fall"),
term(year + 1, "01", "Winter"),
term(year + 1, "05", "Spring"),
];
}
}
// Navigates to LORIS, selects the given term by its display name (e.g. "Winter 2026")
// in the select2 dropdown, and returns the session cookies.
async function getCookies(page, termName) {
await page.goto(apiUrl);
await page.waitForSelector("#catalogSearchLink");
await page.click("#catalogSearchLink");
await page.waitForSelector("a.select2-choice");
await new Promise(resolve => setTimeout(resolve, 2000));
await page.click("a.select2-choice");
// Wait for the dropdown to open, then wait for actual results (not the "Searching..." placeholder)
await page.waitForSelector(".select2-drop-active", { visible: true });
await page.waitForSelector(".select2-drop-active li.select2-result", { visible: true });
// Use Puppeteer element handles so the real mouse event fires and select2 registers it
const options = await page.$$(".select2-drop-active li.select2-result");
let clicked = false;
for (const option of options) {
const text = await page.evaluate(el => el.textContent.trim(), option);
if (text === termName) {
await option.click();
clicked = true;
break;
}
}
if (!clicked) {
const available = await page.evaluate(() =>
[...document.querySelectorAll(".select2-drop-active li.select2-result")]
.map(li => li.textContent.trim())
);
throw new Error(`Term "${termName}" not found. Available: ${available.join(", ")}`);
}
await page.click("button#term-go");
await new Promise(resolve => setTimeout(resolve, 5000));
await page.click("button#search-go");
await new Promise(resolve => setTimeout(resolve, 5000));
const buttons = await page.$$("table#table1 button.form-button.search-section-button");
await buttons[0].click();
const hijackedCookies = await page.cookies();
return hijackedCookies.slice(0, 5).map(c => `${c.name}=${c.value}`);
}
async function reset(axiosInstance, retryCount = 0) {
try {
await axiosInstance.post(
"https://loris.wlu.ca/register/ssb/courseSearch/resetDataForm",
"resetCourses=false&resetSections=true"
);
} catch (error) {
if (retryCount < 5) {
console.error(`Reset failed, retrying (attempt ${retryCount + 1})...`);
await new Promise(resolve => setTimeout(resolve, Math.pow(2, retryCount) * 1000));
await reset(axiosInstance, retryCount + 1);
} else {
console.error("Reset failed after 5 retries");
}
}
}
function removeAllSpaces(str) {
return str.replace(/\s/g, "");
}
function cleanCourseTitle(courseTitle) {
const entities = { "&": "&", """: '"', "<": "<", ">": ">" };
let title = courseTitle;
for (const [entity, replacement] of Object.entries(entities)) {
title = title.replace(new RegExp(entity, "g"), replacement);
}
return title.replace(/[$+,:;=?@#|'<>.^*()%!-]/g, " ").replace(/\s+/g, " ").trim();
}
async function getCoursesTotalCount(term, axiosInstance, retryCount = 0) {
await reset(axiosInstance);
const payload = qs.stringify({
txt_term: term, pageOffset: 0, pageMaxSize: 1,
sortColumn: "subjectDescription", sortDirection: "asc",
});
try {
const response = await axiosInstance.post(coursesURL, payload, {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
return response.data.totalCount;
} catch (error) {
if (error?.response?.status === 500) return 0;
if (retryCount < 5) {
await new Promise(resolve => setTimeout(resolve, Math.pow(2, retryCount) * 1000));
return getCoursesTotalCount(term, axiosInstance, retryCount + 1);
}
throw error;
}
}
async function getCoursesByPage(term, pageOffset, pageMaxSize, axiosInstance, retryCount = 0) {
await reset(axiosInstance);
const payload = qs.stringify({
txt_term: term, pageOffset, pageMaxSize,
sortColumn: "subjectDescription", sortDirection: "asc",
});
try {
const response = await axiosInstance.post(coursesURL, payload, {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
return (response.data.data ?? []).map(el => ({
courseCode: `${el.departmentCode} ${el.courseNumber}`,
courseTitle: cleanCourseTitle(el.courseTitle),
}));
} catch (error) {
if (error?.response?.status === 500) return [];
if (retryCount < 5) {
await new Promise(resolve => setTimeout(resolve, Math.pow(2, retryCount) * 1000));
return getCoursesByPage(term, pageOffset, pageMaxSize, axiosInstance, retryCount + 1);
}
throw error;
}
}
async function getSectionTotalCount(term, courseCode, axiosInstance, retryCount = 0) {
await reset(axiosInstance);
const payload = {
txt_subjectcoursecombo: removeAllSpaces(courseCode),
txt_term: term, pageOffset: 0, pageMaxSize: 1,
sortColumn: "subjectDescription", sortDirection: "asc",
};
try {
const response = await axiosInstance.post(courseDetailsURL, payload, {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
return response.data.totalCount;
} catch (error) {
if (error?.response?.status === 500) return 0;
if (retryCount < 5) {
await new Promise(resolve => setTimeout(resolve, Math.pow(2, retryCount) * 1000));
return getSectionTotalCount(term, courseCode, axiosInstance, retryCount + 1);
}
throw error;
}
}
// Returns an array of CRNs for one page of a course's sections.
async function getCourseCRNsByPage(courseCode, term, pageOffset, pageMaxSize, axiosInstance, retryCount = 0) {
await reset(axiosInstance);
const payload = {
txt_subjectcoursecombo: removeAllSpaces(courseCode),
txt_term: term, pageOffset, pageMaxSize,
sortColumn: "subjectDescription", sortDirection: "asc",
};
try {
const response = await axiosInstance.post(courseDetailsURL, payload, {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
return (response.data.data ?? []).map(el => el.courseReferenceNumber);
} catch (error) {
if (error?.response?.status === 500) return [];
if (retryCount < 5) {
await new Promise(resolve => setTimeout(resolve, Math.pow(2, retryCount) * 1000));
return getCourseCRNsByPage(courseCode, term, pageOffset, pageMaxSize, axiosInstance, retryCount + 1);
}
throw error;
}
}
async function getProfessorByCRN(term, CRN, axiosInstance, retryCount = 0) {
try {
const response = await axiosInstance.get(professorAndMeetingTimesURL, {
params: { term, courseReferenceNumber: CRN },
});
if (!response?.data?.fmt?.[0]?.faculty) return [];
return response.data.fmt[0].faculty.map(f => ({
displayName: f.displayName,
emailAddress: f.emailAddress,
}));
} catch (error) {
if (error?.response?.status === 500) return [];
if (retryCount < 5) {
await new Promise(resolve => setTimeout(resolve, Math.pow(2, retryCount) * 1000));
return getProfessorByCRN(term, CRN, axiosInstance, retryCount + 1);
}
throw error;
}
}
// Fetches all CRNs for a course across all pages. Uses pageMaxSize=500 to minimize round-trips.
async function getAllCRNsForCourse(term, courseCode, axiosInstance) {
const total = await getSectionTotalCount(term, courseCode, axiosInstance);
if (total === 0) return [];
const pageSize = 500;
const pages = Math.ceil(total / pageSize);
const crns = [];
for (let i = 0; i < pages; i++) {
const batch = await getCourseCRNsByPage(courseCode, term, i * pageSize, pageSize, axiosInstance);
crns.push(...batch);
}
return crns;
}
// Scrapes all courses and sections for one term.
// Each term gets its own browser session so terms can safely run in parallel.
async function scrapeTerm({ code, name }) {
// p-limit v5 is ESM-only, use dynamic import
const { default: pLimit } = await import("p-limit");
const limit = pLimit(10);
console.log(`[${name}] Starting scrape...`);
const browser = await puppeteer.launch({
headless: true,
args: process.env.CI ? ["--no-sandbox", "--disable-setuid-sandbox"] : [],
});
const page = await browser.newPage();
const cookies = await getCookies(page, name);
await browser.close();
const axiosInstance = axios.create({ headers: { Cookie: cookies.join("; ") } });
const totalCourses = await getCoursesTotalCount(code, axiosInstance);
const pageSize = 500;
const totalPages = Math.ceil(totalCourses / pageSize);
console.log(`[${name}] ${totalCourses} courses, ${totalPages} page(s)`);
for (let i = 0; i < totalPages; i++) {
const courses = await getCoursesByPage(code, i * pageSize, pageSize, axiosInstance);
const courseUpserts = [];
const sectionUpserts = [];
const instructorUpserts = [];
for (const course of courses) {
const crns = await getAllCRNsForCourse(code, course.courseCode, axiosInstance);
if (crns.length === 0) {
console.log(`[${name}] ${course.courseCode} — no sections, skipping`);
continue;
}
courseUpserts.push({ course_code: course.courseCode, course_title: course.courseTitle, total_reviews: 0 });
console.log(`[${name}] ${course.courseCode} "${course.courseTitle}" — ${crns.length} section(s), fetching profs...`);
// getProfessorByCRN is a stateless GET — safe to parallelize with p-limit
const profResults = await Promise.all(
crns.map(crn => limit(() => getProfessorByCRN(code, crn, axiosInstance)))
);
for (let j = 0; j < crns.length; j++) {
const crn = crns[j];
const profData = profResults[j];
let profName = null, profEmail = null;
for (const prof of profData) {
profName = prof.displayName.replace(/[.$#/[\]]/g, "");
profEmail = prof.emailAddress;
}
console.log(`[${name}] CRN ${crn} → ${profName ?? "no instructor"}`);
sectionUpserts.push({
course_registration_number: crn,
term: code,
instructor_name_fk: profName,
course_code_fk: course.courseCode,
});
if (profName) {
instructorUpserts.push({
instructor_name: profName,
instructor_email: profEmail,
total_reviews: 0,
});
}
}
}
// Batch all upserts for this page in 3 round-trips instead of 3 × N
console.log(`[${name}] Page ${i + 1}/${totalPages} — upserting ${courseUpserts.length} courses, ${sectionUpserts.length} sections, ${instructorUpserts.length} instructors...`);
const succeededCourseCodes = new Set();
if (courseUpserts.length) {
const { error } = await supabase.from("courses").upsert(courseUpserts, { ignoreDuplicates: true });
if (error) {
// Fall back to individual upserts to identify which row(s) are bad
const failedCourses = [];
for (const course of courseUpserts) {
const { error: e } = await supabase.from("courses").upsert(course, { ignoreDuplicates: true });
if (e) failedCourses.push(`${course.course_code} (${e.message})`);
else succeededCourseCodes.add(course.course_code);
}
if (failedCourses.length) {
console.error(`[${name}] Failed courses:\n ${failedCourses.join("\n ")}`);
}
} else {
courseUpserts.forEach(c => succeededCourseCodes.add(c.course_code));
console.log(`[${name}] courses OK`);
}
}
// Instructors must be upserted before sections due to FK constraint
if (instructorUpserts.length) {
const { error } = await supabase.from("instructors").upsert(instructorUpserts, { ignoreDuplicates: true });
if (error) {
console.error(`[${name}] Instructor batch error — falling back to individual upserts`);
for (const instructor of instructorUpserts) {
const { error: e } = await supabase.from("instructors").upsert(instructor, { ignoreDuplicates: true });
if (e) console.error(`[${name}] skipping instructor "${instructor.instructor_name}": ${e.message}`);
}
} else {
console.log(`[${name}] instructors OK`);
}
}
const validSections = sectionUpserts.filter(s => succeededCourseCodes.has(s.course_code_fk));
if (validSections.length) {
const { error } = await supabase.from("sections").upsert(validSections, { ignoreDuplicates: true });
if (error) {
console.error(`[${name}] Section batch error — falling back to individual upserts`);
for (const section of validSections) {
const { error: e } = await supabase.from("sections").upsert(section, { ignoreDuplicates: true });
if (e) console.error(`[${name}] skipping CRN ${section.course_registration_number} (${section.course_code_fk}): ${e.message}`);
}
} else {
console.log(`[${name}] sections OK (${validSections.length}/${sectionUpserts.length})`);
}
}
console.log(`[${name}] Page ${i + 1}/${totalPages} done`);
}
console.log(`[${name}] Scrape complete.`);
}
(async () => {
const terms = getTermsToScrape();
console.log(`Scraping terms: ${terms.map(t => t.name).join(", ")}`);
// Each term runs in parallel with its own LORIS session
await Promise.all(terms.map(scrapeTerm));
console.log("All terms scraped.");
})();