-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
163 lines (150 loc) · 4.98 KB
/
index.ts
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
import axios from "axios";
import express from 'express';
const app = express();
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true }))
let port = process.env.PORT || 3000;
app.set('views', './views');
app.set('view engine', 'pug');
const router = express.Router();
app.get('/', (req, res) => {
res.sendFile(process.cwd() + '/views/index.html');
});
app.post('/', async (req, res) => {
let email = req.body.email;
let password = req.body.password;
try {
const account = await login(email, password);
if(!account || account.token === null){
res.render('error', {error: 'Tài khoản không hợp lệ!'});
}else{
const name = account.name;
const token = account.token;
let exams: Exam[] = await getListExam(token);
if(exams.length === 0) res.render('error', {error: 'Bạn hiện không có kì thi nào.'});
else{
for(const exam of exams){
exam.tests = [];
const tests = await getTests(token, exam.id);
let sumS = 0;
let sumC = 0;
let sumI = 0;
for(const test of tests){
const testInfo = await getTestInfo(token, test.id);
if(testInfo){
if(testInfo.maxScore > 0){
sumS += testInfo.totalScore;
sumC += testInfo.totalCorrect;
sumI += testInfo.totalIncorrect;
exam.tests.push(testInfo);
}
}
}
if(exam.tests.length > 1){
exam.tests.push({
title: 'Tổng',
maxScore: 0,
totalCorrect: sumC,
totalIncorrect: sumI,
totalScore: sumS
});
}
}
res.render('score', { title: `Danh sách các kỳ thi của học sinh ${name}:`, exams: exams});
}
}
} catch (e) {
res.send(e);
}
});
app.listen(port, () => console.log(`GetScoreOnLuyen is listening on port ${port}`));
async function login(email: string, password: string): Promise<Account|undefined> {
try {
const { data } = await axios.post('https://oauth.onluyen.vn/api/account/login', {
"phoneNumber": email,
"password": password,
"rememberMe": false,
"socialType": "Email",
"userName": email
}, {
headers: {
'Content-Type': 'application/json'
},
});
return {token: data.access_token, name: data.display_name};
} catch (e) {
return undefined;
}
}
async function getListExam(token: string): Promise<Exam[]> {
try {
const { data } = await axios.get('https://api-elb.onluyen.vn/api/school-online/exam/list', {
headers: {
authorization: `Bearer ${token}`,
"content-type": "application/json"
}
});
let exams: Exam[] = [];
for (const exam of data) {
exams.push({ id: exam.id, title: exam.title });
}
return exams;
} catch (e) {
return [];
}
}
async function getTests(token: string, examId: string): Promise<Test[]> {
try {
const { data } = await axios.get(`https://api-elb.onluyen.vn/api/school-online/exam/detail/${examId}`, {
headers: {
authorization: `Bearer ${token}`,
"content-type": "application/json"
}
});
let subjects: Test[] = [];
for (const subject of data.data) {
subjects.push({ id: subject.id });
}
return subjects;
} catch (e) {
return [];
}
}
async function getTestInfo(token: string, testId: string): Promise<TestInfo | undefined> {
let testInfo: TestInfo | undefined = undefined;
try {
const { data } = await axios.get(`https://api-elb.onluyen.vn/api/school-online/exam/test/info/${testId}`, {
headers: {
authorization: `Bearer ${token}`,
"content-type": "application/json"
}
});
testInfo = {
maxScore: data.maxScore,
title: data.title,
totalCorrect: data.totalCorrect,
totalIncorrect: data.totalIncorrect,
totalScore: data.totalScore
}
} catch (e) { }
return testInfo;
}
interface Account{
token: string,
name: string
}
interface Exam {
id: string,
title: string,
tests?: TestInfo[]
}
interface Test {
id: string
}
interface TestInfo {
maxScore: number,
title: string,
totalCorrect: number,
totalIncorrect: number,
totalScore: number
}