-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (117 loc) · 3.57 KB
/
index.js
File metadata and controls
127 lines (117 loc) · 3.57 KB
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
// Require dependencies
const puppeteer = require("puppeteer");
const Login = require("./pages/Login");
const StudentTimeTable = require("./pages/StudentTimeTable");
const StudyRegister = require("./pages/StudyRegister");
const StudentMark = require("./pages/StudentMark");
const { generateTimeline, generateTimelineByDay } = require("./util/timeline");
// Main
class StudentAPI {
// Khởi tạo
constructor() {
this._user = null;
this._browser = null;
this._timeTable = null;
//Config by user
this._puppeteer = undefined;
this._browserOfUser = undefined;
this._baseURL = undefined;
}
config(cfg) {
this._puppeteer = cfg.puppeteer || null;
this._baseURL = cfg.baseURL || null;
this._browserOfUser = cfg.browser || null;
}
// Kiểm tra user có tồn tại không => boolean
get isAuthenticated() {
return !!this._user;
}
// Trả về user
get user() {
return this._user;
}
// Khởi chạy browser
async browser() {
if (typeof this._baseURL === "undefined") {
throw new Error("Vui lòng nhập địa chỉ web trường");
}
// Nếu browser đã tồn tại thì trả về cái cũ , nếu không khởi tạo browser mới
if (!this._browser) {
if (this._browserOfUser) {
this._browser = this._browserOfUser;
} else if (this._puppeteer) {
this._browser = await puppeteer.launch(this._puppeteer);
} else {
this._browser = await puppeteer.launch();
}
}
return this._browser;
}
// Hàm đăng nhập
async login(user) {
try {
const browser = await this.browser();
// Nếu không phát sinh lỗi (đăng nhập thành công) thì this._user sẽ tồn tại
const name = await Login(browser, user, this._baseURL);
this._user = { name, id: user.idUser, password: user.passwordUser };
} catch (error) {
throw new Error("Không thể đăng nhập");
}
}
//Thời khóa biểu
async studentTimeTable(term) {
if (!this.isAuthenticated) {
throw new Error("Bạn chưa đăng nhập");
}
if (!this._timeTable) {
try {
const browser = await this.browser();
this._timeTable = await StudentTimeTable(browser, term, this._baseURL);
} catch (error) {
throw new Error("Không lấy được dữ liệu môn học");
}
}
return this._timeTable;
}
async studyRegister() {
if (!this.isAuthenticated) {
throw new Error("Bạn chưa đăng nhập");
}
if (!this._timeTable) {
try {
const browser = await this.browser();
let data = await StudyRegister(browser, this._baseURL);
this._timeTable = data.timeTable;
return data.term;
} catch (error) {
throw new Error("Không lấy được dữ liệu môn học");
}
}
return this._timeTable;
}
get timeLineByDay() {
if (!this._timeTable) throw new Error("Chưa có dữ liệu môn học");
let timelines = generateTimeline(this._timeTable);
let days = generateTimelineByDay(timelines);
return days;
}
// Điểm số
async getMarks(term = "---") {
if (!this.isAuthenticated) {
throw new Error("Bạn chưa đăng nhập");
}
try {
let marks = await StudentMark(this._browser, this._baseURL, term);
return marks;
} catch (error) {
throw new Error("Không lấy được dữ liệu điểm số");
}
}
async close() {
const browser = await this.browser();
await browser.close();
this._browser = null;
this._user = null;
}
}
module.exports = StudentAPI;