Code Duplication    Length = 71-92 lines in 2 locations

app.js 2 locations

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