Conditions | 2 |
Paths | 2 |
Total Lines | 85 |
Lines | 85 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* TODO |
||
75 | View Code Duplication | access.login(req.query.id, req.query.pwd, res, function (headers) { |
|
76 | if (fullLog) { |
||
77 | console.log(`${timeStamp()} Successfully logged in.`.green); |
||
78 | } |
||
79 | |||
80 | // 实际上xnxq01id为空的时候和GET这个URL的效果是一样的,都是查询所有学期 |
||
81 | superagent |
||
82 | .post('http://csujwc.its.csu.edu.cn/jsxsd/kscj/yscjcx_list') |
||
83 | .set(headers) |
||
84 | .type('form') |
||
85 | .send({ |
||
86 | xnxq01id: req.query.sem |
||
87 | }) |
||
88 | .end(function (err, iires) { |
||
89 | if (err) { |
||
90 | console.log(`${timeStamp()} Failed to get grades page\n${err.stack}`.red); |
||
91 | res.status(404).send({ error: '无法进入成绩页面' }); |
||
92 | return next(err); |
||
93 | } |
||
94 | if (fullLog) { |
||
95 | console.log(`${timeStamp()} Successfully entered grades page.`.green); |
||
96 | } |
||
97 | |||
98 | let $ = cheerio.load(iires.text); |
||
99 | |||
100 | let result = { |
||
101 | name: escaper.unescape($('#Top1_divLoginName').text().match(/\s.+\(/)[0].replace(/\s|\(/g, '')), |
||
102 | id: escaper.unescape($('#Top1_divLoginName').text().match(/\(.+\)/)[0].replace(/\(|\)/g, '')), |
||
103 | grades: {}, |
||
104 | 'subject-count': 0, |
||
105 | failed: {}, |
||
106 | 'failed-count': 0, |
||
107 | }; |
||
108 | |||
109 | // 获取成绩列表 |
||
110 | $('#dataList tr').each(function (index) { |
||
111 | if (index === 0) { |
||
112 | return; |
||
113 | } |
||
114 | let element = $(this).find('td'); |
||
115 | let title = escaper.unescape(element.eq(3).text().match(/].+$/)[0].substring(1)); |
||
116 | |||
117 | let item = { |
||
118 | sem: escaper.unescape(element.eq(2).text()), |
||
119 | reg: escaper.unescape(element.eq(4).text()), |
||
120 | exam: escaper.unescape(element.eq(5).text()), |
||
121 | overall: escaper.unescape(element.eq(6).text()) |
||
122 | }; |
||
123 | if (req.query.details) { |
||
124 | item.id = escaper.unescape(element.eq(3).text().match(/\[.+\]/)[0].replace(/\[|\]/g, '')); |
||
125 | item.attr = escaper.unescape(element.eq(8).text()); |
||
126 | item.genre = escaper.unescape(element.eq(9).text()); |
||
127 | item.credit = escaper.unescape(element.eq(7).text()); |
||
128 | } |
||
129 | |||
130 | // 如果有补考记录,则以最高分的为准 |
||
131 | if (title in result.grades) { |
||
132 | // 暂不考虑NaN |
||
133 | if (item.overall < result.grades[title].overall) { |
||
134 | return; |
||
135 | } |
||
136 | if (!element.eq(6).css('color')) { |
||
137 | delete result.failed[title]; |
||
138 | } |
||
139 | } else if (element.eq(6).css('color')) { |
||
140 | result.failed[title] = item; |
||
141 | } |
||
142 | |||
143 | result.grades[title] = item; |
||
144 | }); |
||
145 | |||
146 | result['subject-count'] = Object.keys(result.grades).length; |
||
147 | result['failed-count'] = Object.keys(result.failed).length; |
||
148 | |||
149 | access.logout(headers, res, function() { |
||
150 | // 返回JSON |
||
151 | res.send(JSON.stringify(result)); |
||
152 | if (fullLog) { |
||
153 | console.log(`${timeStamp()} Successfully logged out: `.green + |
||
154 | req.query.id.yellow + |
||
155 | ` (processed in ${new Date() - start} ms)`.green); |
||
156 | } |
||
157 | }); |
||
158 | }); |
||
159 | }); |
||
160 | }); |
||
236 | console.log(`${timeStamp()} The API is now running on port ${port}. Full logging is ${fullLog ? 'enabled' : 'disabled'}`.green); |