-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·83 lines (65 loc) · 2.25 KB
/
index.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
#!/usr/bin/env node
const puppeteer = require("puppeteer");
const usage = `
Usage: grades [USER] [PASS]
Command line tool to scrape your grades from myconcordia`;
if (process.argv.length < 4) {
console.error(usage.trim());
process.exit(1);
}
const [user, pass] = process.argv.slice(2);
const main = async () => {
const browser = await puppeteer.launch({
// website times out when running headless
headless: false,
});
const page = await browser.newPage();
// navigate to mobile website because it shows all grades at once
await page.goto("https://m.myconcordia.ca/login.html", {
waitUntil: "networkidle0"
});
await page.type("#userid", user);
await page.type("#pwd", pass);
await page.click(".login-form input.form_button_submit");
await page.waitForNavigation({ waitUntil: "networkidle2" });
// detect if the login page has reloaded with an error message
if (await page.$("#login_error") !== null) {
await browser.close();
throw new Error("invalid credentials");
}
await page.click("#btnGrade");
await page.waitForNavigation({ waitUntil: "networkidle0" });
await page.click("#btnAllGrades");
// read data from page
const data = await page.evaluate(() => {
const courses = [];
let semester = "";
document.querySelector("#student-schedule").childNodes.forEach(node => {
if (node.nodeName === "H3") {
semester = node.textContent;
}
if (node.classList.contains("course")) {
courses.push({
semester,
title: node.childNodes[0].textContent,
grade: node.lastChild.textContent
});
}
});
return courses;
});
// print data
data.forEach((c) => {
const semester = c.semester.split(" ").reverse().join(" ");
const title = c.title.trim().split(" ").slice(0, 2).join("").padEnd(8);
let grade = c.grade.trim();
if (!grade.match(/[ABCDEF][+-]?|PASS|FAIL/g)) grade = "--";
grade = grade.padEnd(4);
console.log(`${title} ${grade} ${semester}`);
});
await browser.close();
};
main().catch(err => {
console.error(err.toString());
process.exit(2);
});