Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9f122ad

Browse files
committed
Change to new problem list API
1 parent 93398fd commit 9f122ad

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

lib/commands/show.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var cmd = {
2727
};
2828

2929
cmd.handler = function(argv) {
30-
core.getProblem(argv.keyword, function(e, problem) {
30+
core.getProblem(argv.keyword.toString(), function(e, problem) {
3131
if (e) return console.log('ERROR:', e);
3232

3333
var msg = '';

lib/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var defaultConfig = {
77
// usually you don't wanna change those
88
BASE_URL: 'https://leetcode.com',
99
LOGIN_URL: 'https://leetcode.com/accounts/login/',
10-
PROBLEMS_URL: 'https://leetcode.com/problems/',
10+
PROBLEMS_URL: 'https://leetcode.com/api/problems/algorithms/',
11+
PROBLEM_URL: 'https://leetcode.com/problems/',
1112
TEST_URL: 'https://leetcode.com/problems/$key/interpret_solution/',
1213
SUBMIT_URL: 'https://leetcode.com/problems/$key/submit/',
1314
VERIFY_URL: 'https://leetcode.com/submissions/detail/$id/check/',

lib/leetcode_client.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,36 @@ leetcodeClient.getProblems = function(cb) {
2424
if (e) return cb(e);
2525
if (resp.statusCode !== 200) return cb('HTTP failed:' + resp.statusCode);
2626

27-
var $ = cheerio.load(body);
28-
var problems = $('#problemList tbody tr').map(function() {
29-
var tds = $(this).children();
27+
var problemsListJson = JSON.parse(body);
28+
var problems = [];
29+
problemsListJson.stat_status_pairs.forEach(function(problemData) {
3030
var problem = {
31-
state: $(tds[0]).children('span').attr('class'),
32-
id: $(tds[1]).text(),
33-
name: $(tds[2]).children('a').text(),
34-
link: $(tds[2]).children('a').attr('href'),
35-
locked: ($(tds[2]).children('i[class="fa fa-lock"]').length === 1),
36-
percent: $(tds[3]).text(),
37-
level: $(tds[6]).text()
31+
state: problemData['status'],
32+
id: problemData['stat']['question_id'].toString(),
33+
name: problemData['stat']['question__title'],
34+
key: problemData['stat']['question__title_slug'],
35+
link: config.PROBLEM_URL + problemData['stat']['question__title_slug'],
36+
locked: problemData['paid_only'],
37+
level: parseDifficulty(problemData['difficulty']['level'])
3838
};
39-
40-
// fixup problem attributes
41-
problem.id = parseInt(problem.id, 10);
42-
problem.key = _.last(_.compact(problem.link.split('/')));
43-
problem.link = config.BASE_URL + problem.link;
44-
45-
return problem;
46-
}).get();
39+
var percent = parseInt(problemData['stat']['total_acs'], 10) / parseInt(problemData['stat']['total_submitted'], 10) * 100;
40+
problem.percent = percent.toFixed(1).toString() + '%';
41+
problems.push(problem);
42+
});
4743

4844
return cb(null, problems);
4945
});
5046
};
5147

48+
function parseDifficulty(level) {
49+
switch (level) {
50+
case 1 : return 'Easy';
51+
case 2 : return 'Medium';
52+
case 3 : return 'Hard';
53+
default : return 'Unknown Difficulty';
54+
}
55+
}
56+
5257
// hacking ;P
5358
var aceCtrl = {
5459
init: function() {

0 commit comments

Comments
 (0)