forked from tuhuynh27/fptu-mark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
197 lines (166 loc) · 5.23 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
#!/usr/bin/env node
const puppeteer = require("puppeteer");
const prompt = require("prompt-console");
const figlet = require("figlet");
const command = require("meow");
const fs = require("fs-extra");
const yaml = require("js-yaml");
// Command line definition.
const cli = command(`
Usage
$ fptu-mark <credential-yaml>
Examples
$ fptu-mark ./credential.yaml
`);
const [source] = cli.input;
if (!source) {
console.error("The data file is required!");
process.exit(1);
}
(async () => {
const loadYaml = await new Promise(function(resolve) {
fs.readFile(source, "utf-8")
.then(data => {
resolve(yaml.load(data));
})
.catch(error => {
if (error.code === "ENOENT") {
throw new Error(`${source} doesn't exist`);
}
throw error;
});
});
const { email, password, id, season, index } = await loadYaml;
const tradeMark = await new Promise(function(resolve, reject) {
figlet("FPTU Mark CLI", function(err, data) {
if (err) {
reject(err);
}
resolve(data);
});
});
await console.log(tradeMark);
console.log("Email:", email);
console.log(
"Mat khau:",
password ? "**Hidden**" : "Chua nhap mat khau trong file .env!?"
);
console.log("MSSV:", id);
console.log("Hoc ki:", season);
console.log(
"Xem diem mon thu:",
index || index === 0
? index + " (config trong file data.yml)"
: "Not provided (you can provide in data.yml file, ex: INDEX=1)",
"\n"
);
try {
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"]
});
const page = await browser.newPage();
await page.goto("http://fap.fpt.edu.vn/");
// Select FUHCM
await page.select("#ctl00_mainContent_ddlCampus", "4");
await page.waitFor(1000);
// Click Signin
await page.evaluate(() => {
document.querySelector(".abcRioButtonContentWrapper").click();
});
const newPagePromise = new Promise(x =>
browser.once("targetcreated", target => x(target.page()))
);
const popup = await newPagePromise;
await popup.waitFor("input[type=email]");
await popup.type("input[type=email]", email);
await popup.click("#identifierNext");
// Type password and Next
await popup.waitForSelector('#password input[type="password"]', {
visible: true
});
await popup.type('#password input[type="password"]', password, {
delay: 10
});
await popup.click("#passwordNext");
// Go to Mark Report
await page.waitFor('a[href^="Grade/StudentGrade.aspx"]');
await page.click('a[href^="Grade/StudentGrade.aspx"]');
// Select subject
await page.waitFor("[href*='?rollNumber']");
// Get subject index
let subIndex;
listSubject = await page.evaluate(
({ id, season }) => {
const queryString = `[href*='?rollNumber=${id}&term=${season}']`;
const listSubjectDOM = Array.from(
document.querySelectorAll(queryString)
);
return listSubjectDOM.map(e => e.innerText);
},
{ id, season }
);
console.log("List mon da co diem:");
listSubject.forEach((e, index) => {
console.log(`[${index}] - ${e}`);
});
if (index || index === 0) {
subIndex = parseInt(index) || 0;
} else {
subIndex = await new Promise(function(resolve) {
prompt.ask(
[
{
question: `Ban muon xem mon nao, nhap so [0 - ${listSubject.length -
1}]:`,
validator: "notNULL",
name: "index"
}
],
response => {
let parseData = parseInt(response.index) || 0;
if (parseData > listSubject.length - 1 || parseData < 0)
parseData = 0;
resolve(parseData);
}
);
});
}
try {
await page.evaluate(
({ id, season, subIndex }) => {
const queryString = `[href*='?rollNumber=${id}&term=${season}']`;
const listSubjectDOM = document.querySelectorAll(queryString);
listSubjectDOM[subIndex].click();
},
{ id, season, subIndex }
);
} catch (e) {
console.log("Co gi do sai sai, khong chon duoc mon, xem lai index thu!");
}
// Collect data
await page.waitFor("table[summary=Report] tr td");
const data = await page.evaluate(() => {
const tds = Array.from(
document.querySelectorAll("table[summary=Report] tr td")
);
return tds.map(td => td.innerHTML);
});
// Print result
const result = data.slice(Math.max(data.length - 30, 1));
const statusHTML = result[result.length - 1];
const status = statusHTML.replace(/<(?:.|\n)*?>/gm, "");
const average = result[result.length - 3];
const finalFirst = result[result.length - 16] || "Not yet";
const finalSecond = result[result.length - 7] || "Not yet";
console.log(
`\nDiem cua ${email}, mon ${listSubject[subIndex]}, ki ${season}`
);
console.log("====================");
console.log("Final: ", finalFirst);
console.log("Retake: ", finalSecond);
console.log("Average: ", average);
console.log("Status: ", status);
console.log("====================");
await browser.close();
} catch (e) {}
})();