-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·287 lines (256 loc) · 7.69 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
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
#!/usr/bin/env node
import puppeteer from "puppeteer";
import chalk from "chalk";
import inquirer from "inquirer";
import { createSpinner } from "nanospinner";
import figlet from "figlet";
let run = true;
let assignments = [];
let stayInModule = true;
const welcome = () => {
console.clear();
console.log(figlet.textSync("IGLOO"));
console.log(
chalk.bold("💪 Keep track of your Moodle Assignments!\n ") +
chalk.italic("Created by: Adam Byrne \n") +
chalk.gray("For CSIS students at the University of Limerick \n")
);
};
const login = async (page) => {
const username = await inquirer.prompt({
type: "input",
name: "username",
message: "Enter your username/email",
});
const password = await inquirer.prompt({
type: "password",
name: "password",
message: "Enter your password",
mask: true,
});
const spinner = createSpinner("Logging in ...").start();
if (page.url() != "https://moodle2.csis.ul.ie/login/index.php") {
await page.goto("https://moodle2.csis.ul.ie/login/index.php");
}
await page.type("#username", username.username);
await page.type("#password", password.password);
await page.click("#loginbtn");
await page.waitForNavigation();
if (page.url() === "https://moodle2.csis.ul.ie/login/index.php") {
spinner.error({ text: chalk.red("Login failed, please try again.") });
await login(page);
return;
} else {
spinner.success({ text: chalk.green("Logged in as " + username.username) });
}
};
const profile = async (page) => {
const profileURLSelector = "#page-footer > div > div.logininfo > a";
await page.waitForSelector(profileURLSelector);
const profileURL = await page.evaluate((profileURLSelector) => {
return document.querySelector(profileURLSelector).href;
}, profileURLSelector);
return profileURL;
};
const loadModuleSelection = async (page, profileURL) => {
const spinner = createSpinner("Loading module selection").start();
await page.goto(profileURL);
const moduleListSelector =
"#region-main > div > div > div > section:nth-child(3) > ul > li > dl > dd > ul";
await page.waitForSelector(moduleListSelector);
const moduleList = await page.evaluate((moduleListSelector) => {
const modules = [];
const list = document.querySelector(moduleListSelector).childNodes;
list.forEach((item) => {
modules.push({ name: item.innerText, url: item.firstChild.href });
});
return modules;
}, moduleListSelector);
spinner.success({ text: chalk.green("Loaded modules") });
return moduleList;
};
const chooseModule = async (moduleList) => {
const moduleNames = moduleList.map((k) => k.name);
const moduleLinks = moduleList.map((k) => k.url);
const moduleSelection = await inquirer.prompt({
type: "list",
name: "choice",
message: "Select a module",
choices: moduleNames,
});
const url =
moduleLinks[moduleNames.indexOf(moduleSelection.choice)].split(
"course="
)[1];
return "https://moodle2.csis.ul.ie/course/view.php?id=" + url;
};
const loadAssignmentsList = async (page, moduleURL) => {
await page.goto(moduleURL);
const spinner = createSpinner("Finding assignments").start();
const assignmentsSelector = ".modtype_assign";
await page.waitForSelector(assignmentsSelector);
const assignments = await page.evaluate((assignmentsSelector) => {
return [...document.querySelectorAll(assignmentsSelector)].map((anchor) => {
const assignment = anchor
.querySelector(".instancename")
.innerText.slice(0, -11);
const link = anchor.querySelector("a").href;
const id = link.slice(link.indexOf("id=") + 3);
return { id, assignment, link };
});
}, assignmentsSelector);
spinner.success({ text: chalk.green("Assignments found.") });
return assignments;
};
const fetchAssignmentsDetails = async (page, assignments) => {
const spinner = createSpinner("Gathering assignment details").start();
for (const assignment of assignments) {
await page.goto(assignment.link);
const table = await page.$(".generaltable");
await page.waitForSelector(".generaltable");
assignment.deadline = 0;
const tableData = await page.evaluate((table) => {
return [...table.querySelectorAll("tr")].map((row) => {
return [...row.querySelectorAll("td")]
.map((cell) => cell.innerText)
.toString();
});
}, table);
for (const col of tableData) {
if (
assignment.deadline == 0 &&
Date.parse(col) &&
Date.parse(col) != 978307200000 &&
!col.includes("Group")
) {
let idx = tableData.indexOf(col);
assignment.deadline = col;
assignment.submission = tableData[idx + 1];
assignment.graded = tableData[idx - 1];
assignment.status = tableData[idx - 2];
break;
}
}
}
spinner.success({ text: chalk.green("Assignments loaded") });
return assignments;
};
const todos = (assignments) => {
let result = [];
result = assignments.filter(
(assignment) =>
assignment.status == "No attempt" &&
assignment.status !=
"This assignment does not require you to submit anything online" &&
assignment.graded != "Graded"
);
result.sort((a, b) => {
return Date.parse(a.deadline) - Date.parse(b.deadline);
});
return result;
};
const graded = (assignments) => {
let result = [];
result = assignments.filter((assignment) => assignment.graded == "Graded");
result.sort((a, b) => {
return Date.parse(b.deadline) - Date.parse(a.deadline);
});
return result;
};
const completed = (assignments) => {
let result = [];
result = assignments.filter(
(assignment) => assignment.status == "Submitted for grading"
);
result.sort((a, b) => {
return Date.parse(b.deadline) - Date.parse(a.deadline);
});
return result;
};
const displayAssignments = async (assignments) => {
const view = await inquirer.prompt({
type: "list",
name: "view",
message: "View:",
choices: [
"📝 To Do",
"📋 Graded",
"📄 Completed",
"📚 All",
"⏪ Back",
"🚪 Exit",
],
});
const tableHeaders = ["assignment", "deadline", "status", "graded"];
console.clear();
console.log(chalk.bold("\n" + view.view));
switch (view.view) {
case "📝 To Do":
stayInModule = true;
const todoData = todos(assignments);
if (todoData.length == 0) {
console.log("Nothing to do! \n");
break;
}
console.table(todoData, tableHeaders);
break;
case "📋 Graded":
stayInModule = true;
const gradedData = graded(assignments);
if (gradedData.length == 0) {
console.log("Nothing graded! \n");
break;
}
console.table(gradedData, tableHeaders);
break;
case "📄 Completed":
stayInModule = true;
const completedData = completed(assignments);
if (completedData.length == 0) {
console.log("Nothing submitted! \n");
break;
}
console.table(completedData, tableHeaders);
break;
case "📚 All":
stayInModule = true;
if (assignments.length == 0) {
console.log("Nothing to see here! \n");
break;
}
console.table(assignments, tableHeaders);
break;
case "⏪ Back":
stayInModule = false;
break;
case "🚪 Exit":
run = false;
process.exit(0);
}
};
const chooseAssignmentView = async (assignments) => {
await displayAssignments(assignments);
};
const chooseModuleView = async (page) => {
const _profileURL = await profile(page);
const _moduleList = await loadModuleSelection(page, _profileURL);
const _moduleURL = await chooseModule(_moduleList);
const _assignmentList = await loadAssignmentsList(page, _moduleURL);
assignments = await fetchAssignmentsDetails(page, _assignmentList);
};
(async () => {
welcome();
const browser = await puppeteer.launch();
const page = await browser.newPage();
await login(page);
await chooseModuleView(page);
while (run) {
if (!stayInModule) {
await chooseModuleView(page);
await chooseAssignmentView(assignments);
} else {
await chooseAssignmentView(assignments);
}
}
await browser.close();
})();