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

Commit 12dca72

Browse files
committed
[solution] fixes TypeError in some problems.
refs skygragon#2 Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 820c6e2 commit 12dca72

File tree

1 file changed

+56
-32
lines changed

1 file changed

+56
-32
lines changed

plugins/solution.discuss.js

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var _ = require('underscore');
2+
var cheerio = require('cheerio');
23
var request = require('request');
34

45
var log = require('../log');
@@ -16,49 +17,72 @@ var plugin = new Plugin(200, 'solution.discuss', '2017.07.29',
1617

1718
var URL_DISCUSSES = 'https://discuss.leetcode.com/api/category/';
1819
var URL_DISCUSS_TOPIC = 'https://discuss.leetcode.com/topic/';
20+
var URL_DISCUSS_TOPIC_API = 'https://discuss.leetcode.com/api/topic/';
1921

20-
plugin.getProblem = function(problem, cb) {
21-
plugin.next.getProblem(problem, function(e, problem) {
22-
if (e || !session.argv.solution) return cb(e, problem);
22+
function getSolutionDetail(solution, cb) {
23+
if (!solution) return cb();
2324

24-
var opts = {
25-
url: URL_DISCUSSES + problem.discuss
26-
};
27-
request(opts, function(e, resp, body) {
28-
if (e) return cb(e);
29-
if (resp.statusCode !== 200)
30-
return cb({msg: 'http error', statusCode: resp.statusCode});
25+
request(URL_DISCUSS_TOPIC_API + solution.slug, function(e, resp, body) {
26+
if (e) return cb(e);
27+
if (resp.statusCode !== 200)
28+
return cb({msg: 'http error', statusCode: resp.statusCode});
3129

32-
var lang = session.argv.lang;
33-
var langs = [lang];
34-
// try to find more compatible langs
35-
if (lang === 'cpp') langs.push('c++');
36-
if (lang === 'csharp') langs.push('c#');
37-
if (lang === 'golang') langs.push('go');
38-
if (lang === 'javascript') langs.push('js');
39-
if (lang === 'python3') langs.push('python');
30+
var data = JSON.parse(body);
31+
solution.title = data.titleRaw;
32+
var $ = cheerio.load(data.posts[0].content);
33+
solution.content = $.root().text();
34+
return cb(null, solution);
35+
});
36+
}
4037

41-
var data = JSON.parse(body);
42-
var solution = _.find(data.topics, function(x) {
43-
var keys = x.title.toLowerCase().split(' ');
44-
for (var i = 0; i < keys.length; ++i) {
45-
if (langs.indexOf(keys[i]) >= 0) return true;
46-
}
47-
return false;
48-
});
38+
function getSolution(problem, lang, cb) {
39+
if (!problem) return cb();
4940

50-
if (!solution) {
51-
return log.error('Solution not found for ' + lang);
41+
request(URL_DISCUSSES + problem.discuss, function(e, resp, body) {
42+
if (e) return cb(e);
43+
if (resp.statusCode !== 200)
44+
return cb({msg: 'http error', statusCode: resp.statusCode});
45+
46+
var langs = [lang];
47+
// try to find more compatible langs
48+
if (lang === 'cpp') langs.push('c++');
49+
if (lang === 'csharp') langs.push('c#');
50+
if (lang === 'golang') langs.push('go');
51+
if (lang === 'javascript') langs.push('js');
52+
if (lang === 'python3') langs.push('python');
53+
54+
var data = JSON.parse(body);
55+
var solution = _.find(data.topics, function(x) {
56+
var keys = x.title.toLowerCase().split(' ');
57+
for (var i = 0; i < keys.length; ++i) {
58+
if (langs.indexOf(keys[i]) >= 0) return true;
5259
}
60+
return false;
61+
});
5362

54-
log.info(solution._imported_title);
63+
return getSolutionDetail(solution, cb);
64+
});
65+
}
66+
67+
plugin.getProblem = function(problem, cb) {
68+
plugin.next.getProblem(problem, function(e, problem) {
69+
if (e || !session.argv.solution) return cb(e, problem);
70+
71+
var lang = session.argv.lang;
72+
getSolution(problem, lang, function(e, solution) {
73+
if (e) return cb(e);
74+
if (!solution) return log.error('Solution not found for ' + lang);
75+
76+
log.info();
77+
log.info(solution.title);
5578
log.info();
5679
log.info(chalk.underline(URL_DISCUSS_TOPIC + solution.slug));
5780
log.info();
58-
log.info('* Lang: ' + lang);
59-
log.info('* Votes: ' + solution.votes);
81+
log.info('* Lang: ' + lang);
82+
log.info('* Author: ' + solution.user.username);
83+
log.info('* Votes: ' + solution.votes);
6084
log.info();
61-
log.info(chalk.yellow(solution._imported_content));
85+
log.info(solution.content);
6286
});
6387
});
6488
};

0 commit comments

Comments
 (0)