-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (190 loc) · 5.72 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
require('dotenv').config();
const puppeteer = require('puppeteer-extra');
const moment = require('moment');
// add stealth plugin and use defaults (all evasion techniques)
puppeteer.use(require('puppeteer-extra-plugin-stealth')());
puppeteer.use(require('puppeteer-extra-plugin-click-and-wait')());
puppeteer.use(require('puppeteer-extra-plugin-adblocker')());
const {
HEADLESS = false,
SLOWMO = 0,
FLVS_USERNAME,
FLVS_PASSWORD,
} = process.env;
const defaultPuppeteerOptions = {
dumpio: false,
headless: HEADLESS,
defaultViewport: {
width: 1920,
height: 1080,
},
slowMo: SLOWMO,
userDataDir: '/tmp',
ignoreHTTPSErrors: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
};
const students = [
{
name: 'Alexander Scragg',
id: '4959341',
flipbookUrl:
'https://learn.flvs.net/educator/common/ElementaryProgram/Flipbook/Flipbook_4.htm',
},
{
name: 'Julia Scragg',
id: '4959339',
flipbookUrl:
'https://learn.flvs.net/educator/common/ElementaryProgram/Flipbook/Flipbook_2.htm',
},
];
let page;
const getText = async (sel) => {
await page.waitForSelector(sel, { timeout: 10000 });
const element = await page.$(sel);
return await page.evaluate((el) => el.textContent, element);
};
const containsText = async (sel, text) => {
return (await getText(sel)).includes(text);
};
const login = async () => {
await page.goto('https://login.flvs.net/', { waitUntil: 'networkidle0' });
await page.type('#Username', FLVS_USERNAME);
await page.type('#Password', FLVS_PASSWORD);
await page.click('form input[type=submit]');
await page.waitForNavigation({ waitUntil: 'networkidle0' });
};
const gotoDashboard = async () => {
await page.goto('https://vsa.flvs.net', { waitUntil: 'networkidle0' });
};
const gotoStudentReport = async () => {
await page.clickAndWaitForNavigation('#idBar a');
};
const getStudentEnrollmentTable = async () => {
await page.waitForSelector('#Pane6 .flvs-table');
return await page.$eval('#Pane6 .flvs-table', (el) => {
const tableToJson = (table) => {
const data = [];
// first row needs to be headers
const headers = [];
for (let i = 0; i < table.rows[0].cells.length; i++) {
headers[i] = table.rows[0].cells[i].innerHTML
.toLowerCase()
.replace(/ /gi, '');
}
// go through cells
for (let i = 1; i < table.rows.length; i++) {
let tableRow = table.rows[i];
const rowData = {};
for (var j = 0; j < tableRow.cells.length; j++) {
rowData[headers[j]] = tableRow.cells[j].innerHTML;
}
data.push(rowData);
}
return data;
};
return tableToJson(el);
});
};
const selectFirstCourse = async () => {
await page.waitForSelector('#dashboard');
await page.clickAndWaitForNavigation('#dashboard .dashboard__item a');
};
const getCoursesFromDropdown = async () => {
await page.waitForSelector('#observerUl');
const links = await page.$$eval('#observerUl a', (el) =>
el.map((x) => x.getAttribute('href'))
);
const labels = [];
const els = await page.$$('#observerUl .dropdown-item');
for (const element of els) {
labels.push(
(await page.evaluate((el) => el.textContent, element))
.trim()
.split('\n')[0]
);
}
const courses = [];
for (let i = 0; i < links.length; i++) {
courses.push({
name: labels[i],
url: links[i],
});
}
return courses;
};
const selectStudent = async (student) => {
// check if student is already selected
if (await containsText('#idBar', student.name)) {
// already selected
return;
}
await page.waitForSelector('#Pane6 select');
await page.select('#Pane6 select', student.id);
await page.waitForNavigation({ waitUntil: 'networkidle0' });
};
const gotoGradebook = async () => {
await page.waitForSelector('.navbar-nav');
const navItems = await page.$$('.navbar-nav a');
for (const item of navItems) {
const textContent = await page.evaluate((el) => el.textContent, item);
if (textContent.includes('Gradebook')) {
await Promise.all([
item.click(),
page.waitForNavigation({ waitUntil: 'networkidle0' }),
]);
return;
}
}
};
const gotoFlipbookCourseUrls = async (flipbookUrl) => {
await page.goto(flipbookUrl, { waitUntil: 'networkidle0' });
const slideUrls = await page.$$eval('iframe', (el) =>
el.map((x) => x.getAttribute('src'))
);
console.log(slideUrls);
};
const main = async () => {
const output = [];
const browser = await puppeteer.launch(defaultPuppeteerOptions);
page = await browser.newPage();
await login();
await gotoDashboard();
for (const student of students) {
await selectStudent(student);
await selectFirstCourse();
const courses = await getCoursesFromDropdown();
for (const course of courses) {
let obj = {
name: student.name,
course: course.name,
};
await page.goto(course.url, { waitUntil: 'networkidle0' });
await gotoGradebook();
try {
obj.lastAssignment = await getText('#topsum .points-right-column a');
} catch (err) {
console.error(err);
}
try {
const lastSubmitted = (await getText('.last-submitted-date')).trim();
const momentObj = moment(new Date(lastSubmitted));
obj = {
...obj,
lastSubmitted,
fromNow: momentObj.fromNow(),
ts: momentObj.unix(),
};
} catch (err) {
console.error(err);
}
output.push(obj);
// console.log(output);
}
await gotoDashboard();
}
output.sort((a, b) => (a.ts > b.ts ? 1 : b.ts > a.ts ? -1 : 0));
console.log(JSON.stringify(output, null, 2));
await page.waitForTimeout(60000);
browser.close();
};
main();