1 | /* TODO |
||
2 | * 完善异常处理 |
||
3 | */ |
||
4 | |||
5 | var http = require('http'), |
||
6 | express = require('express'), |
||
7 | superagent = require('superagent'), |
||
8 | cheerio = require('cheerio'), |
||
9 | escaper = require('true-html-escape'), |
||
10 | colors = require('colors'), |
||
11 | program = require('commander'), |
||
12 | Date = require('./lib/Date.js'), |
||
13 | access = require('./lib/access.js'); |
||
14 | |||
15 | program |
||
16 | .option('-h, --help') |
||
17 | .option('-v, --version') |
||
18 | .option('-p, --port [n]', parseInt) |
||
19 | .option('-f, --fulllog') |
||
20 | .parse(process.argv); |
||
21 | |||
22 | // 别问我为什么这里逻辑这么奇怪……测试的结果确实是这样的啊hhh |
||
23 | if (!program.help || !program.version) { |
||
24 | console.log(('CSUEMS API v1.0.0').rainbow); |
||
25 | console.log(('by The Liberators').rainbow); |
||
26 | if (!program.help) { |
||
27 | console.log('Preparation:') |
||
28 | console.log(' \\\\This section is WIP\\\\'); |
||
29 | console.log('\nUsage:'); |
||
30 | console.log(' npm start [-- <options...>]'); |
||
31 | console.log('\nOptions:'); |
||
32 | console.log(' -h, --help print this message and exit.'); |
||
33 | console.log(' -v, --version print the version and exit.'); |
||
34 | console.log(' -f, --fulllog enable full log, by default only errors are logged.'); |
||
35 | console.log(' -p, --port [value] specify a port to listen, 2333 by default.'); |
||
36 | console.log('\nExamples:'); |
||
37 | console.log(' $ npm start -p 43715 # listening to 43715'); |
||
38 | console.log(' # forever start app.js # deploy with forever as daemon (root access recommended)'); |
||
39 | console.log(' # pm2 start -i 0 --name "csuapi" app.js # deploy with pm2 as daemon (root access recommended)'); |
||
40 | } |
||
41 | process.exit(0); |
||
42 | } |
||
43 | |||
44 | const timeStamp = () => new Date().format('[MM-dd hh:mm:ss] '), |
||
45 | port = program.port || 2333; |
||
46 | base = 'http://csujwc.its.csu.edu.cn'; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
47 | |||
48 | var app = express(); |
||
49 | |||
50 | // 查成绩API,通过GET传入用户名和密码 |
||
51 | app.get('/grades/', function (req, res, next) { |
||
52 | if (program.fulllog) { |
||
53 | var start = new Date(); |
||
54 | console.log((timeStamp() + 'Started to query the grades: ').cyan + req.query.id.yellow); |
||
55 | } |
||
56 | access.login(req.query.id, req.query.pwd, res, function (headers, iires) { |
||
57 | program.fulllog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
58 | var ret = {}; |
||
59 | var $ = cheerio.load(iires.text); |
||
60 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3); |
||
61 | ret.id = req.query.id; |
||
62 | |||
63 | // 进入成绩页面 |
||
64 | superagent.get(base + $('li[title="我的成绩"] a').attr('href')) |
||
65 | .set(headers) |
||
66 | .end(function (err, iiires) { |
||
67 | if (err) { |
||
68 | console.log((timeStamp() + 'Failed to fetch grades\n' + err.stack).red); |
||
69 | ret.error = '获取成绩失败'; |
||
70 | return next(err); |
||
71 | } |
||
72 | program.fulllog && console.log((timeStamp() + 'Successfully entered grades page.').green); |
||
73 | |||
74 | $ = cheerio.load(iiires.text); |
||
75 | |||
76 | // 获取成绩列表 |
||
77 | let grades = {}; |
||
78 | let failed = {}; |
||
79 | $('#dataList').each(function (index) { |
||
80 | // cheerio没有实现jQuery的lt |
||
81 | if (index >= 2) |
||
82 | return; |
||
83 | $(this).find('tr[class!="theadCss"]').each(function() { |
||
84 | // 这段写得真是要吐血了 |
||
85 | let subject = escaper.unescape($(this).find('td[align="left"]').eq(1).text()); |
||
86 | if (subject) { |
||
87 | let score = $(this).find('font'); |
||
88 | if (score.text()) |
||
89 | grades[subject] = score.text(); |
||
90 | if (score.css('color')) |
||
91 | failed[subject] = score.text(); |
||
92 | } |
||
93 | }); |
||
94 | }); |
||
95 | ret.grades = grades; |
||
96 | ret['subject-count'] = Object.getOwnPropertyNames(grades).length; |
||
97 | ret.failed = failed; |
||
98 | ret['failed-count'] = Object.getOwnPropertyNames(failed).length; |
||
99 | |||
100 | // 完成所有工作后,登出 |
||
101 | access.logout(headers, res, function() { |
||
102 | // 第五步:返回JSON |
||
103 | res.send(JSON.stringify(ret)); |
||
104 | program.fulllog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green); |
||
105 | }); |
||
106 | }); |
||
107 | }); |
||
108 | }); |
||
109 | |||
110 | // 查考试API,通过GET传入用户名和密码 |
||
111 | /* |
||
112 | app.get('/exams/', function (req, res, next) { |
||
113 | if (program.fulllog) { |
||
114 | var start = new Date(); |
||
115 | console.log((timeStamp() + 'Started to query the exams: ').cyan + req.query.id.yellow); |
||
116 | } |
||
117 | login(req.query.id, req.query.pwd, res, function (headers, iires) { |
||
118 | var ret = {}; |
||
119 | var $ = cheerio.load(iires.text); |
||
120 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3); |
||
121 | ret.id = req.query.id; |
||
122 | |||
123 | // TODO: POST,注意headers |
||
124 | }); |
||
125 | */ |
||
126 | |||
127 | app.listen(port); |
||
128 | console.log((timeStamp() + 'The server is now running on port ' + port + '.').green); |