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

Commit f4fa713

Browse files
committed
Add plugin to fetch most voted solution.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent c9ac6ee commit f4fa713

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

solution.discuss.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var _ = require('underscore');
2+
var request = require('request');
3+
4+
var log = require('../log');
5+
var chalk = require('../chalk');
6+
var Plugin = require('../plugin');
7+
var session = require('../session');
8+
9+
var plugin = new Plugin(200, 'solution.discuss', '2017.07.29',
10+
'Plugin to fetch most voted solution in discussions.');
11+
12+
var URL_DISCUSSES = 'https://discuss.leetcode.com/api/category/';
13+
var URL_DISCUSS_TOPIC = 'https://discuss.leetcode.com/topic/';
14+
15+
plugin.getProblem = function(problem, cb) {
16+
plugin.next.getProblem(problem, function(e, problem) {
17+
if (e || !session.argv.solution) return cb(e, problem);
18+
19+
var opts = {
20+
url: URL_DISCUSSES + problem.discuss
21+
};
22+
request(opts, function(e, resp, body) {
23+
if (e) return cb(e);
24+
if (resp.statusCode !== 200)
25+
return cb({msg: 'http error', statusCode: resp.statusCode});
26+
27+
var lang = session.argv.lang;
28+
var langs = [lang];
29+
// try to find more compatible langs
30+
if (lang === 'cpp') langs.push('c++');
31+
if (lang === 'csharp') langs.push('c#');
32+
if (lang === 'golang') langs.push('go');
33+
if (lang === 'javascript') langs.push('js');
34+
if (lang === 'python3') langs.push('python');
35+
36+
var data = JSON.parse(body);
37+
var solution = _.find(data.topics, function(x) {
38+
var keys = x.title.toLowerCase().split(' ');
39+
for (var i = 0; i < keys.length; ++i) {
40+
if (langs.indexOf(keys[i]) >= 0) return true;
41+
}
42+
return false;
43+
});
44+
45+
if (!solution) {
46+
return log.error('Solution not found for ' + lang);
47+
}
48+
49+
log.info(solution._imported_title);
50+
log.info();
51+
log.info(chalk.underline(URL_DISCUSS_TOPIC + solution.slug));
52+
log.info();
53+
log.info('* Lang: ' + lang);
54+
log.info('* Votes: ' + solution.votes);
55+
log.info();
56+
log.info(chalk.yellow(solution._imported_content));
57+
});
58+
});
59+
};
60+
61+
module.exports = plugin;

0 commit comments

Comments
 (0)