forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathleetcode_client.js
52 lines (40 loc) · 1.5 KB
/
leetcode_client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var cheerio = require('cheerio'),
request = require('request'),
_ = require('underscore');
var config = require('./config');
var leetcode_client = {};
leetcode_client.getProblems = function(cb) {
request(config.PROBLEMS_URL, function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode != 200) return cb('Invalid HTTP response: ' + resp.statusCode);
var $ = cheerio.load(body);
var problems = $('#problemList tbody tr').map(function(){
var tds = $(this).children();
var problem = {
id: $(tds[1]).text(),
name: $(tds[2]).children('a').text(),
link: $(tds[2]).children('a').attr('href'),
percent: $(tds[3]).text(),
level: $(tds[6]).text()
};
// fixup problem attributes
problem.key = _.last(_.compact(problem.link.split('/')));
problem.link = config.BASE_URL + problem.link;
return problem;
}).get();
return cb(null, problems);
});
};
leetcode_client.getProblem = function(problem, cb) {
request(problem.link, function(e, resp, body){
if (e) return cb(e);
if (resp.statusCode != 200) return cb('Invalid HTTP response: ' + resp.statusCode);
var $ = cheerio.load(body);
var info = $('div[class="question-info text-info"] ul li strong');
problem.desc = $('meta[property="og:description"]').attr('content');
problem.total_ac = $(info[0]).text();
problem.total_submit = $(info[1]).text();
return cb(null, problem);
});
};
module.exports = leetcode_client;