-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjwxt.js
187 lines (172 loc) · 7.57 KB
/
jwxt.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
var superagent = require('superagent')
var cheerio = require('cheerio')
var webVPN_ID = ''
var webVPN_Pwd = ''
var jwID = webVPN_ID;
var jwPwd = ''
var webVpnToken = '';
async function getWebVPNToken_(myagent,error) {
vpnLoginPostData = {
auth_type: 'local',
username: webVPN_ID, // '这里填写学号
sms_code: '',
password: webVPN_Pwd, // 这里填写密码
}
headers = {
'Origin': 'http://webvpn.bupt.edu.cn',
'Referer': 'http://webvpn.bupt.edu.cn/login',
'Host': 'webvpn.bupt.edu.cn',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15'
}
return await new Promise(function (resolve) {
myagent.post('http://webvpn.bupt.edu.cn/do-login?local_login=true').set(headers).send(vpnLoginPostData).then(
function (res) {
if (res.text.indexOf('用户名密码错误') !== -1) throw new Error('WebVPN用户名密码错误')
// console.log(res.text)
resolve(res.text)
}
).catch(err=>{error(err)})
}).catch(err=>{error(err)})
}
exports.getAgent = function () {
return superagent.agent();
}
exports.getAgentWithVpn = async function (webid, webpwd,error) {
webVPN_ID = webid;
webVPN_Pwd = webpwd;
var agent = superagent.agent();
await getWebVPNToken(agent,error);
return agent;
}
exports.loginJwxt = function (sid, jwpwd, agent, todo,error) {
jwPwd = jwpwd;
jwID = sid;
loginJwxt(agent, todo,error)
}
async function getWebVPNToken(myagent,error, tryNum = 0) {
var result = await getWebVPNToken_(myagent,error);
if(result.indexOf('快速跳转')!=-1){
return;
}
}
function loginJwxt(myagent, todo,error) {
var jwxt = "http://webvpn.bupt.edu.cn/https/77726476706e69737468656265737421fae0469069327d406a468ca88d1b203b/jsxsd/xk/LoginToXk"
// Buffer.from('Hello').toString('base64')
jwLoginPostData = {
'userAccount': jwID, // '这里填写学号
'userPassword': '', // 这里填写密码
'encoded': Buffer.from(jwID).toString('base64') + '%%%'
+ Buffer.from(jwPwd).toString('base64')
}
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15',
}
myagent.set(headers).post(jwxt).send(jwLoginPostData).then(function (res) {
console.log(res.text)
if (res.text.indexOf('密码提示为') !== -1) {
throw new Error('教务系统密码错误');
} else {
dosth(myagent, todo);
}
}).catch(err => {error(err);})
}
function dosth(myagent, todo) {
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15',
}
if (todo.type == 'cj') {
var url = 'https://webvpn.bupt.edu.cn/https/77726476706e69737468656265737421fae0469069327d406a468ca88d1b203b/jsxsd/kscj/cjcx_list'
var lastCid = '';
myagent.get(url).set(headers).then(function (res) {
var $ = cheerio.load(res.text);
var result = [];
$('table tr').each(function (i, element) {
var grade = {};
const tds = $(element).find('td')
if ($(tds[2]).text() === lastCid) return;
lastCid = $(tds[2]).text()
grade.xueqi = $(tds[1]).text();
grade.cname = $(tds[3]).text() + $(tds[4]).text();
grade.score = $(tds[5]).text().trim()
grade.desc = $(tds[6]).text()
grade.xuefen = $(tds[7]).text()
grade.jidian = $(tds[9]).text()
result.push(grade);
})
todo.callback(result);
})
} else if (todo.type == 'kcb') {
var url = 'https://webvpn.bupt.edu.cn/https/77726476706e69737468656265737421fae0469069327d406a468ca88d1b203b/jsxsd/framework/main_index_loadkb.jsp'
myagent.post(url).set(headers).send({ rq: todo.date }).then(function (res) {
var $ = cheerio.load(res.text);
var kcb = [];
$('table tbody tr').each((i, e) => {
var row = []
$(e).find('td').each((ii, ee) => {
var text = $(ee).html();
if (text.indexOf('课程名称') !== -1) {
try {
row.push('<span class="classname">' + text.match(/课程名称:(\S*)<br/)[1] + '</span><br>' + text.match(/上课地点:(\S*)<br/)[1]);
} catch (e) {
row.push('<span class="classname">' + text.match(/课程名称:(\S*)<br/)[1] + '</span><br>' + text.match(/上课地点:(\S*)"/)[1]);
}
return;
}
row.push(text.replaceAll('\t', '').replaceAll('\n', ''))
})
kcb.push(row)
})
// 处理第一列
for (let i = 0; i < kcb.length; i++) {
kcb[i][0] = '<span class="largeNum">' + kcb[i][0].split('<br>')[0] + '</span> ' + kcb[i][0].split('<br>')[2]
}
// 处理连堂
console.log(kcb);
if (kcb.length > 0)
for (let i = 1; i < kcb[0].length; i++) {
var j = 1;
var target = 0;
while (j < kcb.length)
if (kcb[j][i] == kcb[target][i] && kcb[j][i] != '') {
delete kcb[j][i];
j++;
if(j==kcb.length){
if ((j - target) > 1) kcb[target][i] = '<td class="multiclass" rowspan="' + (j - target) + '">' + kcb[target][i] + '</td>';
target = j;
j++;
}
} else {
if ((j - target) > 1) kcb[target][i] = '<td class="multiclass" rowspan="' + (j - target) + '">' + kcb[target][i] + '</td>';
target = j;
j++;
}
}
//console.log(kcb)
// 将数组格式化为html代码
var result = '<thead>' + $('table thead').html() + '</thead><tbody>'
for (let i = 0; i < kcb.length; i++) {
result += '<tr>'
for (let j = 0; j < kcb[i].length; j++) {
if (kcb[i][j] === undefined) continue;
else if (kcb[i][j].indexOf('<td') == 0) result += kcb[i][j]
else result += '<td>' + kcb[i][j] + '</td>'
}
result += '</tr>'
}
result += '</tbody>'
todo.callback(result);
})
}
}