-
Notifications
You must be signed in to change notification settings - Fork 2
/
ceiba.js
131 lines (126 loc) · 3.3 KB
/
ceiba.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
var rp = require('request-promise');
var Promise = require('bluebird');
var iconv = require('iconv-lite');
var _ = require('lodash');
var urlbuilder = require('./urlbuilder');
var constants = require('./constants');
var Semester = require('./semester');
var Ceiba = function(username, password, cb) {
var self = this;
self.current_semester = 0;
self.semester = [];
self.login(username, password).then(function() {
return rp({
headers: self.headers,
jar: self.jar,
json: true,
method: 'GET',
url: urlbuilder.api({
mode: 'semester'
})
});
}).then(function(data) {
data.semester.forEach(function(semester) {
self.semester.push(new Semester(semester.semester, self.jar));
if(semester.now) {
self.current_semester = semester.semester;
self.semester[self.semester.length - 1].init(data);
}
});
}).then(function() {
return rp({
jar: self.jar,
encoding: null,
method: 'GET',
url: constants.urls.ceiba_web_home
});
}).then(function(result) {
cb(null, result);
}).catch(function(e) {
cb(e, null);
});
};
Ceiba.prototype.login = function(username, password) {
var self = this;
self.jar = rp.jar();
self.headers = constants.headers;
return rp({
headers: self.headers,
jar: self.jar,
method: 'GET',
url: constants.urls.ceiba_web_home
}).then(function() {
return rp({
headers: self.headers,
jar: self.jar,
method: 'GET',
url: constants.urls.ceiba_api_entry
});
}).then(function() {
self.headers.referer = constants.urls.ceiba_sso_callback_endpoint;
return rp({
headers: self.headers,
jar: self.jar,
method: 'GET',
url: constants.urls.ntu_sso_entry
});
}).then(function() {
self.headers.referer = constants.urls.ntu_sso_entry;
return rp({
headers: self.headers,
jar: self.jar,
method: 'POST',
resolveWithFullResponse: true,
simple: false, // statusCode 302 is expected
url: constants.urls.ntu_sso_login,
form: {
user: username,
pass: password
}
});
}).then(function(response){
if(response.statusCode == 302 && response.headers.location.indexOf(constants.urls.ceiba_sso_callback_prefix) === 0){
// success
self.headers.referer = constants.urls.ntu_sso_login;
return rp({
headers: self.headers,
jar: self.jar,
method: 'GET',
url: response.headers.location
});
}else{
throw new Error('bad auth');
}
}).then(function() {
return rp({
headers: self.headers,
jar: self.jar,
method: 'GET',
url: constants.urls.ceiba_sso_callback_endpoint
});
});
};
Ceiba.prototype.get_semester = function(param) {
var self = this;
return new Promise(function(resolve, reject) {
var index = param;
if(Math.abs(param) != param) {
index = _.findIndex(self.semester, { name: param });
}
if(self.semester[index].courses.length != 0) {
return resolve(self.semester[index]);
}
self.semester[index].fetch(resolve);
});
};
Ceiba.prototype.get = function(params) {
var self = this;
return rp({
headers: self.headers,
jar: self.jar,
json: true,
method: 'GET',
url: urlbuilder.api(params),
});
};
module.exports = Ceiba;