Completed
Push — master ( 43b78b...e6437d )
by Equim
01:01
created
1
/* TODO
2
 * 完善异常处理
3
 * 建立用户名-Cookie数据库
4
 */
5
6
'use strict';
7
8
var express    = require('express'),
9
    superagent = require('superagent'),
10
    cheerio    = require('cheerio'),
11
    escaper    = require('true-html-escape'),
12
    colors     = require('colors'),
13
    program    = require('commander'),
14
    Date       = require('./lib/Date.js'),
0 ignored issues
show
Comprehensibility introduced by
You are shadowing the built-in type Date. This makes code hard to read, consider using a different name.
Loading history...
15
    access     = require('./lib/access.js');
16
17
program
18
    .option('-h, --help')
19
    .option('-v, --version')
20
    .option('-p, --port [n]', parseInt)
0 ignored issues
show
The variable parseInt seems to be never declared. If this is a global, consider adding a /** global: parseInt */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
21
    .option('-f, --fullLog')
22
    .parse(process.argv);
23
24
// 别问我为什么这里逻辑这么奇怪……测试的结果确实是这样的啊hhh
25
if (!program.help || !program.version) {
26
    console.log(('CSUEMS API v1.0.1').rainbow);
1 ignored issue
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
27
    console.log(('by The Liberators').rainbow);
28
    if (!program.help) {
29
        console.log('Preparation:');
30
        console.log('  \\\\This section is WIP\\\\');
31
        console.log('\nUsage:');
32
        console.log('  npm start [-- <options...>]');
33
        console.log('\nOptions:');
34
        console.log('  -h, --help          print this message and exit.');
35
        console.log('  -v, --version       print the version and exit.');
36
        console.log('  -f, --fullLog       enable full log, by default only errors are logged.');
37
        console.log('  -p, --port [value]  specify a port to listen, 2333 by default.');
38
        console.log('\nExamples:');
39
        console.log('  $ npm start -p 43715                  # listening to 43715');
40
        console.log('  $ forever start app.js                # deploy with forever as daemon (root access recommended)');
41
        console.log('  $ pm2 start -i 0 -n "csuapi" app.js   # deploy with pm2 as daemon  (root access recommended)');
42
    }
43
    process.exit(0);
1 ignored issue
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
44
}
45
46
const timeStamp = () => new Date().format('[MM-dd hh:mm:ss] '),
47
      // 以系统时间获取当前学期
48
      getSem = () => {
49
          let now = new Date();
50
          let month = now.getMonth();
51
          let year = now.getFullYear();
52
          if (month === 0) {
53
              return (year - 1) + '-' + year + '-1';
54
          } else if (month <= 6) {
55
              return (year - 1) + '-' + year + '-2';
56
          } else {
57
              return year + '-' + (year + 1) + '-1';
58
          }
59
      },
60
      port = program.port || 2333,
61
      fullLog = program.fullLog;
62
63
var app = express();
64
65
// 查成绩API,通过GET传入用户名和密码
66 View Code Duplication
app.get('/grades', function (req, res, next) {
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
67
    if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
68
        res.send({ error: "参数不正确" });
69
        return;
70
    }
71
    if (fullLog) {
72
        var start = new Date();
73
        console.log((timeStamp() + 'Started to query the grades: ').cyan + req.query.id.yellow);
1 ignored issue
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
74
    }
75
    access.login(req.query.id, req.query.pwd, res, function (headers, ires) {
76
        fullLog && console.log((timeStamp() + 'Successfully logged in.').green);
1 ignored issue
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
77
78
        var ret = {};
79
        var $ = cheerio.load(ires.text);
80
81
        ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].substring(3).replace(/</, '');
82
        ret.id = req.query.id;
83
84
        // 实际上xnxq01id为空的时候和GET这个URL的效果是一样的,都是查询所有学期
85
        superagent
86
            .post('http://csujwc.its.csu.edu.cn/jsxsd/kscj/yscjcx_list')
87
            .set(headers)
88
            .type('form')
89
            .send({
90
                xnxq01id: req.query.sem
91
            })
92
            .end(function (err, iires) {
93
                if (err) {
94
                    console.log((timeStamp() + 'Failed to get grades page\n' + err.stack).red);
1 ignored issue
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
95
                    res.send({ error: '无法进入成绩页面' });
96
                    return next(err);
97
                }
98
                fullLog && console.log((timeStamp() + 'Successfully entered grades page.').green);
99
100
                $ = cheerio.load(iires.text);
101
                
102
                ret.grades = {};
103
                ret['subject-count'] = 0;
104
                ret.failed = {};
105
                ret['failed-count'] = 0;
106
107
                // 获取成绩列表,如果有补考记录,则以最高分的为准
108
                $('#dataList tr').each(function (index) {
109
                    if (index === 0) {
110
                        return;
111
                    }
112
                    let element = $(this).find('td');
113
                    let title = escaper.unescape(element.eq(3).text().match(/].+$/)[0].substring(1));
114
115
                    let item = {};
116
                    item.sem = escaper.unescape(element.eq(2).text());
117
                    item.regular = escaper.unescape(element.eq(4).text());
118
                    item.exam = escaper.unescape(element.eq(5).text());
119
                    item.overall = escaper.unescape(element.eq(6).text());
120
121
                    if (title in ret.grades) {
122
                        if (item.overall < ret.grades[title].overall) {
123
                            return;
124
                        }
125
                    }
126
127
                    ret.grades[title] = item;
128
                    ret['subject-count']++;
129
130
                    // 暂不考虑NaN,(字符串 < 60)为false
131
                    if (parseInt(item.overall) < 60) {
132
                        ret.failed[title] = item;
133
                        ret['failed-count']++;
134
                    }
135
                });
136
137
                access.logout(headers, res, function() {
138
                    // 返回JSON
139
                    res.send(JSON.stringify(ret));
140
                    fullLog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green);
1 ignored issue
show
The variable start does not seem to be initialized in case fullLog on line 71 is false. Are you sure this can never be the case?
Loading history...
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
141
                });
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
142
            });
143
144
145
    });
146
});
147
148
// 查考试API,通过GET传入用户名和密码
149 View Code Duplication
app.get('/exams', function (req, res, next) {
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
150
    if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
151
        res.send({ error: "参数不正确" });
152
        return;
153
    }
154
    if (fullLog) {
155
        var start = new Date();
156
        console.log((timeStamp() + 'Started to query the exams: ').cyan + req.query.id.yellow);
1 ignored issue
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
157
    }
158
    access.login(req.query.id, req.query.pwd, res, function (headers, ires) {
159
        fullLog && console.log((timeStamp() + 'Successfully logged in.').green);
1 ignored issue
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
160
161
        var ret = {};
162
        var $ = cheerio.load(ires.text);
163
164
        ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3);
165
        ret.id = req.query.id;
166
        ret.sem = req.query.sem || getSem();
167
168
        superagent
169
            .post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list')
170
            .set(headers)
171
            .type('form')
172
            .send({
173
                xqlbmc: '',
174
                xnxqid: ret.sem,
175
                xqlb: ''
176
            })
177
            .end(function (err, iires) {
178
                if (err) {
179
                    console.log((timeStamp() + 'Failed to reach exams page\n' + err.stack).red);
1 ignored issue
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
180
                    res.send({ error: '获取成绩失败' });
181
                    return next(err);
182
                }
183
                fullLog && console.log((timeStamp() + 'Successfully entered exams page.').green);
184
185
                $ = cheerio.load(iires.text);
186
187
                ret.exams = {};
188
                ret['exams-count'] = 0;
189
190
                $('#dataList tr').each(function (index) {
191
                    if (index === 0) {
192
                        return;
193
                    }
194
                    let element = $(this).find('td');
195
                    let title = escaper.unescape(element.eq(3).text());
196
197
                    let item = {};
198
                    item.time = escaper.unescape(element.eq(4).text());
199
                    item.location = escaper.unescape(element.eq(5).text());
200
                    item.seat = escaper.unescape(element.eq(6).text());
201
202
                    ret.exams[title] = item;
203
                    ret['exams-count']++;
204
                });
205
206
                access.logout(headers, res, function() {
207
                    res.send(JSON.stringify(ret));
208
                    fullLog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green);
1 ignored issue
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
The variable start does not seem to be initialized in case fullLog on line 154 is false. Are you sure this can never be the case?
Loading history...
209
                });
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
210
            });
211
    });
212
});
213
214
app.listen(port);
215
console.log((timeStamp() + 'The server is now running on port ' + port + '.').green);
216