@@ -11,7 +11,7 @@ var session = require('../session');
1111//
1212// https://github.com/skygragon/leetcode-cli-plugins/blob/master/docs/solution.discuss.md
1313//
14- var plugin = new Plugin ( 200 , 'solution.discuss' , '2018.04.14 ' ,
14+ var plugin = new Plugin ( 200 , 'solution.discuss' , '2019.02.03 ' ,
1515 'Plugin to fetch most voted solution in discussions.' ) ;
1616
1717var URL_DISCUSSES = 'https://leetcode.com/graphql' ;
@@ -20,14 +20,22 @@ var URL_DISCUSS = 'https://leetcode.com/problems/$slug/discuss/$id';
2020function getSolution ( problem , lang , cb ) {
2121 if ( ! problem ) return cb ( ) ;
2222
23+ if ( lang === 'python3' ) lang = 'python' ;
24+
2325 var opts = {
2426 url : URL_DISCUSSES ,
2527 json : true ,
26- qs : {
28+ body : {
2729 query : [
28- 'query fetchTopics($questionId: Int!, $pageNo: Int!, $orderBy: String!) {' ,
29- ' questionTopics(questionId: $questionId, pageNo: $pageNo, orderBy: $orderBy) {' ,
30- ' data {' ,
30+ 'query questionTopicsList($questionId: String!, $orderBy: TopicSortingOption, $skip: Int, $query: String, $first: Int!, $tags: [String!]) {' ,
31+ ' questionTopicsList(questionId: $questionId, orderBy: $orderBy, skip: $skip, query: $query, first: $first, tags: $tags) {' ,
32+ ' ...TopicsList' ,
33+ ' }' ,
34+ '}' ,
35+ 'fragment TopicsList on TopicConnection {' ,
36+ ' totalNum' ,
37+ ' edges {' ,
38+ ' node {' ,
3139 ' id' ,
3240 ' title' ,
3341 ' post {' ,
@@ -42,11 +50,14 @@ function getSolution(problem, lang, cb) {
4250 '}'
4351 ] . join ( '\n' ) ,
4452
45- operationName : 'fetchTopics ' ,
53+ operationName : 'questionTopicsList ' ,
4654 variables : JSON . stringify ( {
47- pageNo : 1 ,
55+ query : '' ,
56+ first : 1 ,
57+ skip : 0 ,
4858 orderBy : 'most_votes' ,
49- questionId : problem . id
59+ questionId : '' + problem . id ,
60+ tags : [ lang ]
5061 } )
5162 }
5263 } ;
@@ -55,23 +66,8 @@ function getSolution(problem, lang, cb) {
5566 if ( resp . statusCode !== 200 )
5667 return cb ( { msg : 'http error' , statusCode : resp . statusCode } ) ;
5768
58- var langs = [ lang ] ;
59- // try to find more compatible langs
60- if ( lang === 'cpp' ) langs . push ( 'c++' ) ;
61- if ( lang === 'csharp' ) langs . push ( 'c#' ) ;
62- if ( lang === 'golang' ) langs . push ( 'go' ) ;
63- if ( lang === 'javascript' ) langs . push ( 'js' ) ;
64- if ( lang === 'python3' ) langs . push ( 'python' ) ;
65-
66- var solutions = body . data . questionTopics . data ;
67- var solution = _ . find ( solutions , function ( x ) {
68- var keys = x . title . toLowerCase ( ) . split ( / [ ^ \w + ] / ) ;
69- for ( var i = 0 ; i < keys . length ; ++ i ) {
70- if ( langs . indexOf ( keys [ i ] ) >= 0 ) return true ;
71- }
72- return false ;
73- } ) ;
74-
69+ const solutions = body . data . questionTopicsList . edges ;
70+ const solution = solutions . length > 0 ? solutions [ 0 ] . node : null ;
7571 return cb ( null , solution ) ;
7672 } ) ;
7773}
0 commit comments