-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconcile.js
44 lines (36 loc) · 1.66 KB
/
reconcile.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
import { writeFileSync } from "node:fs";
import fetch from "node-fetch";
import { csvParse, autoType, csvFormat } from "d3-dsv";
import { max, index, rollup, sum } from "d3-array";
const csv = async (url) => {
const req = await fetch(url);
const res = await req.text();
return csvParse(res, autoType);
}
const main = async () => {
const bucket = "stash.michigandaily.com";
const prefix = "course-tracker/winter-2023";
const cache = await csv(`https://${bucket}/${prefix}/cache-courses.csv`);
const overview = await csv(`https://${bucket}/${prefix}/overview.csv`);
const courseIndex = index(overview, d => d.department + d.number);
for await (const course of cache) {
const department = course.course.slice(0, -3).toLowerCase();
const number = course.course.slice(-3);
const url = `https://${bucket}/${prefix}/courses/${department}/${department}-${number}.csv`;
const file = await csv(url);
const sections = file.filter(d => d.Section.includes("LEC") || d.Section.includes("SEM") || d.Section.includes("REC") || d.Section.includes("IND"))
const potential = max(rollup(sections, v => sum(v, d => +d["Open Seats"]), d => d.Time).values());
const slug = department.toUpperCase() + number;
if (potential !== undefined && courseIndex.has(slug)) {
const c = courseIndex.get(slug);
const maximum = c.capacity;
const available = c.available;
if (potential > maximum) {
console.log(slug, potential, maximum);
courseIndex.set(slug, { ...c, capacity: potential, percent_available: available / potential })
}
}
}
writeFileSync("./overview.csv", csvFormat(Array.from(courseIndex.values())))
}
main();