-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
386 lines (346 loc) · 10.7 KB
/
script.js
File metadata and controls
386 lines (346 loc) · 10.7 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
386
/** biome-ignore-all lint/correctness/noUnusedFunctionParameters: bypass for now */
import util from "node:util";
import {
ZUpsertProgramWithRequirements,
ZUpsertRequirements,
} from "@albert-plus/server/convex/http";
import { DrizzleD1Database } from "drizzle-orm/d1";
import * as z from "zod/mini";
export async function bulletinDiscoverSchools(url) {
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
const html = await res.text();
const regex = /<ul[^>]*class=["']nav levelone["'][^>]*>[\s\S]*?<\/ul>/gi;
const matches = html.match(regex) || [];
const linkRegex = /<a[^>]*href=["']([^"']+)["'][^>]*>(.*?)<\/a>/gi;
const hrefs = [];
const validSchools = Object.values(
ZUpsertProgramWithRequirements.shape.school.options
);
for (const section of matches) {
let match;
while (true) {
match = linkRegex.exec(section);
if (match === null) break;
const url = match[1].trim();
const name = match[2].replace(/<[^>]+>/g, "").trim();
if (!validSchools.includes(name)) {
continue;
}
hrefs.push([name, url]);
}
}
return hrefs;
}
export async function scrapeProgramsListFromSchool(urlarray, db, env) {
const base = "https://bulletins.nyu.edu";
let target;
const schoolbaseinfo = ZUpsertProgramWithRequirements.parse({
name: "",
level: "undergraduate",
school: urlarray[0],
programUrl: "",
requirements: [],
});
const programlist = [];
if (urlarray[1].startsWith("/undergraduate")) {
schoolbaseinfo.level = "undergraduate";
} else {
schoolbaseinfo.level = "graduate";
}
try {
try {
target = new URL(urlarray[1], base);
} catch {
target = new URL(base);
}
const res = await fetch(target);
const html = await res.text();
const sitemapMatch = html.match(
/<div\s+class=["']sitemap["'][^>]*>[\s\S]*?<\/div>/i
);
if (!sitemapMatch) {
return [schoolbaseinfo, []];
}
const sitemapHtml = sitemapMatch[0];
const linkRe = /<a[^>]*href=["']([^"']+)["'][^>]*>(.*?)<\/a>/gi;
let m;
while (true) {
m = linkRe.exec(sitemapHtml);
if (m === null) break;
const href = m[1].trim();
const name = m[2]
.replace(/<[^>]*>/g, "")
.replace(/&/g, "&")
.trim();
const absolute = new URL(href, base).toString();
programlist.push([name, absolute]);
}
} catch (err) {
console.error("Error scraping program:", err);
}
return [schoolbaseinfo, programlist];
}
export async function scrapeProgramsFromProgramsList(programarray, db, env) {
const [schoolbaseinfo, programlist] = programarray;
const programs = programlist.map(([name, href]) => ({
name,
level: schoolbaseinfo.level,
school: schoolbaseinfo.school,
programUrl: href,
requirements: [],
}));
return programs;
}
export async function scrapeRequirements(programdet, db, env) {
const base = "https://bulletins.nyu.edu";
let target;
try {
target = new URL(programdet.programUrl, base);
} catch {
target = new URL(base);
}
const res = await fetch(target);
const html = await res.text();
const stripTags = (s) => s.replace(/<[^>]*>/g, "");
const htmlunescape = (s) =>
s
.replace(/ | /g, " ")
.replace(/&/g, "&")
.replace(/–|–/g, "–")
.replace(/—|—/g, "—")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/'/g, "'");
const cleanText = (s) =>
htmlunescape(stripTags(s || ""))
.replace(/\s+/g, " ")
.trim();
const curId = 'id="curriculumtextcontainer"';
let start = html.indexOf(curId);
start = html.lastIndexOf("<div", start);
let end =
html.indexOf('id="sampleplanofstudytextcontainer"', start) !== -1
? html.indexOf('id="sampleplanofstudytextcontainer"', start)
: html.indexOf('id="outcomestextcontainer"', start);
if (end === -1) end = html.indexOf('id="policiestextcontainer"', start);
if (end === -1) end = html.length;
const curriculumHtml = html.slice(start, end);
let totalCredits = null;
const curriculumText = cleanText(curriculumHtml);
const totalMatch = curriculumText.match(/total credits[^0-9]*([0-9]+)/i);
if (totalMatch) {
totalCredits = Number.parseInt(totalMatch[1], 10);
}
const tableRe =
/<table\b[^>]*class=["'][^"']*\bsc_courselist\b[^"']*["'][^>]*>[\s\S]*?<\/table>/gi;
const getPart = (src, pat) => {
const m = src.match(pat);
return m ? m[0] : "";
};
const colgroupRe = /<colgroup[\s\S]*?<\/colgroup>/i;
const theadRe = /<thead[\s\S]*?<\/thead>/i;
const tbodyRe = /<tbody[\s\S]*?<\/tbody>/i;
const trRe = /<tr\b[^>]*>[\s\S]*?<\/tr>/gi;
const isBoldHeaderRow = (trHtml) => {
if (/\bclass=["'][^"']*\bareaheader\b[^"']*["']/.test(trHtml)) return true;
if (/<strong\b[^>]*>[\s\S]*?<\/strong>/i.test(trHtml)) return true;
if (/<b\b[^>]*>[\s\S]*?<\/b>/i.test(trHtml)) return true;
if (
/<span\b[^>]*\bcourselistcomment\b[^"']*\bareaheader\b[^"']*["'][^>]*>[\s\S]*?<\/span>/i.test(
trHtml
)
)
return true;
return false;
};
const headerNameFromTr = (trHtml) => {
let m = trHtml.match(/<strong\b[^>]*>([\s\S]*?)<\/strong>/i);
if (m) return cleanText(m[1]);
m = trHtml.match(/<b\b[^>]*>([\s\S]*?)<\/b>/i);
if (m) return cleanText(m[1]);
m = trHtml.match(
/<span\b[^>]*\bcourselistcomment\b[^"']*\bareaheader\b[^"']*["'][^>]*>([\s\S]*?)<\/span>/i
);
if (m) return cleanText(m[1]);
return cleanText(trHtml);
};
const rebuildTable = (tableOpen, colgroup, thead, rowsHtml) => {
const openTag = (tableOpen.match(/<table\b[^>]*>/i) || ["<table>"])[0];
return `${openTag}${colgroup || ""}${
thead || ""
}<tbody>${rowsHtml}</tbody></table>`;
};
const sections = [];
let m;
while (true) {
m = tableRe.exec(curriculumHtml);
if (m === null) break;
const tableHtml = m[0];
const tableOpenTag = (tableHtml.match(/<table\b[^>]*>/i) || ["<table>"])[0];
const colgroup = getPart(tableHtml, colgroupRe);
const thead = getPart(tableHtml, theadRe);
const tbodyFull = getPart(tableHtml, tbodyRe) || tableHtml;
const tbodyInner = (tbodyFull.match(/<tbody[^>]*>([\s\S]*?)<\/tbody>/i) || [
null,
"",
])[1];
const trs = tbodyInner.match(trRe) || [];
let currentName = null;
let currentRows = [];
const flush = () => {
if (currentName && currentRows.length) {
const sectionTable = rebuildTable(
tableOpenTag,
colgroup,
thead,
currentRows.join("")
);
sections.push([currentName, sectionTable]);
}
currentName = null;
currentRows = [];
};
for (let i = 0; i < trs.length; i++) {
const tr = trs[i];
if (isBoldHeaderRow(tr)) {
flush();
currentName = headerNameFromTr(tr) || "Requirements";
currentRows.push(tr);
} else if (currentName) {
currentRows.push(tr);
}
}
flush();
}
const extractPieces = (tableHtml) => {
const getPart = (src, pat) => {
const m = src.match(pat);
return m ? m[0] : "";
};
const tbodyFull = getPart(tableHtml, /<tbody[\s\S]*?<\/tbody>/i) || "";
const tbodyInner = (tbodyFull.match(/<tbody[^>]*>([\s\S]*?)<\/tbody>/i) || [
null,
"",
])[1];
const trs = tbodyInner.match(/<tr\b[^>]*>[\s\S]*?<\/tr>/gi) || [];
return trs;
};
const mergedMap = new Map();
for (const [name, tableHtml] of sections) {
const trs = extractPieces(tableHtml);
if (!mergedMap.has(name)) {
mergedMap.set(name, trs);
} else {
const arr = mergedMap.get(name);
mergedMap.set(name, arr.concat(trs));
}
}
const categorizedSections = [];
for (const [sectionName, trs] of mergedMap.entries()) {
const courses = [];
for (const tr of trs) {
if (/areaheader|listsum/i.test(tr)) continue;
const codeMatch = tr.match(/<a[^>]*>([^<]+)<\/a>/i);
const code = codeMatch ? cleanText(codeMatch[1]) : "";
const titleMatch = tr.match(/<td[^>]*>(?!<a)[\s\S]*?<\/td>/i);
let title = "";
if (titleMatch) title = cleanText(titleMatch[0]);
const creditsMatch = tr.match(
/<td[^>]*class=["'][^"']*\bhourscol\b[^"']*["'][^>]*>([\s\S]*?)<\/td>/i
);
const creditsRaw = creditsMatch ? cleanText(creditsMatch[1]) : "";
const credits = creditsRaw ? parseFloat(creditsRaw) || null : null;
let type = "course";
if (/orclass/i.test(tr)) {
type = "alternative";
}
if (/courselistcomment/i.test(tr) && !code) {
if (credits !== null) {
type = "options";
} else {
type = "comment";
}
}
if (type === "comment" && !code && credits === null) {
continue;
}
courses.push({
type,
code,
title,
credits,
});
}
for (let i = 0; i < courses.length; i++) {
const row = courses[i];
if (
(row.type === "comment" || row.type === "options") &&
row.title.toLowerCase().includes("select")
) {
let j = i + 1;
while (j < courses.length && courses[j].type === "course") {
courses[j].type = "alternative";
j++;
}
}
}
for (const row of courses) {
if (row.type === "course") {
row.type = "required";
}
}
categorizedSections.push([sectionName, courses]);
}
const filteredSections = categorizedSections.filter(
([sectionName]) => sectionName !== "General Education Requirements"
);
if (totalCredits !== null) {
filteredSections.push([
"Total Credits",
[
{
type: "",
code: "",
title: "Total Credits",
credits: totalCredits,
},
],
]);
}
programdet.requirements = filteredSections;
return programdet;
}
async function main() {
const message = await bulletinDiscoverSchools("https://bulletins.nyu.edu");
const finalarray = [];
for (const item of message) {
const plist = await scrapeProgramsListFromSchool(item);
const programs = await scrapeProgramsFromProgramsList(plist);
finalarray.push(programs);
}
const flatArray = finalarray.flat();
//Test
let i = 0;
for (i = 0; i < flatArray.length; i++) {
console.log(
util.inspect(await scrapeRequirements(flatArray[i]), {
depth: null,
maxArrayLength: null,
colors: true,
})
);
}
}
//33 is Classical Civilization Minor
//39 is CS/DS
//40 is CS (BA), 42 is Computer Science / Computer Engineering
//53 is Economics (BA)
//85 is History (BA), 89 is Italian (BA)
//120 is MCB Minor
main().catch(console.error);