Code Duplication    Length = 71-85 lines in 2 locations

app.js 2 locations

@@ 75-159 (lines=85) @@
72
    res.sendFile(__dirname + '/doc/API.html');
73
});
74
75
// 查成绩API,通过GET传入用户名和密码
76
app.get(/^\/g(?:|rades)$/, function (req, res, next) {
77
    if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
78
        res.status(404).send({ error: "参数不正确" });
79
        return;
80
    }
81
    if (fullLog) {
82
        var start = new Date();
83
        console.log(`${timeStamp()} Started to query the grades: `.cyan + req.query.id.yellow);
84
    }
85
    access.login(req.query.id, req.query.pwd, res, function (headers) {
86
        if (fullLog) {
87
            console.log(`${timeStamp()} Successfully logged in.`.green);
88
        }
89
90
        // 实际上xnxq01id为空的时候和GET这个URL的效果是一样的,都是查询所有学期
91
        superagent
92
            .post('http://csujwc.its.csu.edu.cn/jsxsd/kscj/yscjcx_list')
93
            .set(headers)
94
            .type('form')
95
            .send({
96
                xnxq01id: req.query.sem
97
            })
98
            .end(function (err, iires) {
99
                if (err) {
100
                    console.log(`${timeStamp()} Failed to get grades page\n${err.stack}`.red);
101
                    res.status(404).send({ error: '无法进入成绩页面' });
102
                    return next(err);
103
                }
104
                if (fullLog) {
105
                    console.log(`${timeStamp()} Successfully entered grades page.`.green);
106
                }
107
108
                let $ = cheerio.load(iires.text);
109
110
                let result = {
111
                    name: escaper.unescape($('#Top1_divLoginName').text().match(/\s.+\(/)[0].replace(/\s|\(/g, '')),
112
                    id: escaper.unescape($('#Top1_divLoginName').text().match(/\(.+\)/)[0].replace(/\(|\)/g, '')),
113
                    grades: {},
114
                    'subject-count': 0,
115
                    failed: {},
116
                    'failed-count': 0,
117
                };
118
119
                // 获取成绩列表
120
                $('#dataList tr').each(function (index) {
121
                    if (index === 0) {
122
                        return;
123
                    }
124
                    let element = $(this).find('td');
125
126
                    let title = escaper.unescape(element.eq(3).text().match(/].+$/)[0].substring(1));
127
128
                    let item = {
129
                        sem: escaper.unescape(element.eq(2).text()),
130
                        reg: escaper.unescape(element.eq(4).text()),
131
                        exam: escaper.unescape(element.eq(5).text()),
132
                        overall: escaper.unescape(element.eq(6).text())
133
                    };
134
                    if (req.query.details) {
135
                        item.id = escaper.unescape(element.eq(3).text().match(/\[.+\]/)[0].replace(/\[|\]/g, ''));
136
                        item.attr = escaper.unescape(element.eq(8).text());
137
                        item.genre = escaper.unescape(element.eq(9).text());
138
                        item.credit = escaper.unescape(element.eq(7).text());
139
                    }
140
141
                    // 如果有补考记录,则以最高分的为准
142
                    // 这段代码是拿可读性换了时间复杂度……
143
                    if (title in result.grades) {
144
                        // 暂不考虑NaN
145
                        if (item.overall < result.grades[title].overall) {
146
                            return;
147
                        }
148
                        if (!element.eq(6).css('color')) {
149
                            delete result.failed[title];
150
                        }
151
                    } else if (element.eq(6).css('color')) {
152
                        result.failed[title] = item;
153
                    }
154
155
                    result.grades[title] = item;
156
                });
157
158
                result['subject-count'] = Object.keys(result.grades).length;
159
                result['failed-count'] = Object.keys(result.failed).length;
160
161
                access.logout(headers, res, function() {
162
                    // 返回JSON
@@ 163-233 (lines=71) @@
160
161
                access.logout(headers, res, function() {
162
                    // 返回JSON
163
                    res.send(JSON.stringify(result));
164
                    if (fullLog) {
165
                        console.log(`${timeStamp()} Successfully logged out: `.green +
166
                            req.query.id.yellow +
167
                            ` (processed in ${new Date() - start} ms)`.green);
168
                    }
169
                });
170
            });
171
    });
172
});
173
174
// 查考试API,通过GET传入用户名和密码
175
app.get(/^\/e(?:|xams)$/, function (req, res, next) {
176
    if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
177
        res.status(404).send({ error: "参数不正确" });
178
        return;
179
    }
180
    if (fullLog) {
181
        var start = new Date();
182
        console.log(`${timeStamp()} Started to query the exams: `.cyan + req.query.id.yellow);
183
    }
184
    access.login(req.query.id, req.query.pwd, res, function (headers) {
185
        if (fullLog) {
186
            console.log(`${timeStamp()} Successfully logged in.`.green);
187
        }
188
        superagent
189
            .post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list')
190
            .set(headers)
191
            .type('form')
192
            .send({
193
                xqlbmc: '',
194
                xnxqid: req.query.sem || getSem(),
195
                xqlb: ''
196
            })
197
            .end(function (err, iires) {
198
                if (err) {
199
                    console.log(`${timeStamp()} Failed to reach exams page\n${err.stack}`.red);
200
                    res.status(404).send({ error: '获取成绩失败' });
201
                    return next(err);
202
                }
203
                if (fullLog) {
204
                    console.log(`${timeStamp()} Successfully entered exams page.`.green);
205
                }
206
207
                let $ = cheerio.load(iires.text);
208
209
                let result = {
210
                    name: escaper.unescape($('#Top1_divLoginName').text().match(/\s.+\(/)[0].replace(/\s|\(/g, '')),
211
                    id: escaper.unescape($('#Top1_divLoginName').text().match(/\(.+\)/)[0].replace(/\(|\)/g, '')),
212
                    sem: req.query.sem || getSem(),
213
                    exams: {},
214
                    'exams-count': 0,
215
                };
216
217
                $('#dataList tr').each(function (index) {
218
                    if (index === 0) {
219
                        return;
220
                    }
221
                    let element = $(this).find('td');
222
                    let title = escaper.unescape(element.eq(3).text());
223
224
                    let item = {
225
                        time: escaper.unescape(element.eq(4).text()),
226
                        location: escaper.unescape(element.eq(5).text()),
227
                        seat: escaper.unescape(element.eq(6).text())
228
                    };
229
230
                    result.exams[title] = item;
231
                    result['exams-count']++;
232
                });
233
234
                access.logout(headers, res, function() {
235
                    res.send(JSON.stringify(result));
236
                    if (fullLog) {