@@ -12,33 +12,45 @@ var session = require('../session');
1212//
1313// https://github.com/skygragon/leetcode-cli-plugins/blob/master/docs/solution.discuss.md
1414//
15- var plugin = new Plugin ( 200 , 'solution.discuss' , '2017.12.21 ' ,
15+ var plugin = new Plugin ( 200 , 'solution.discuss' , '2018.04.14 ' ,
1616 'Plugin to fetch most voted solution in discussions.' ) ;
1717
18- var URL_DISCUSSES = 'https://discuss.leetcode.com/api/category/' ;
19- var URL_DISCUSS_TOPIC = 'https://discuss.leetcode.com/topic/' ;
20- var URL_DISCUSS_TOPIC_API = 'https://discuss.leetcode.com/api/topic/' ;
21-
22- function getSolutionDetail ( solution , cb ) {
23- if ( ! solution ) return cb ( ) ;
24-
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 } ) ;
29-
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- }
18+ var URL_DISCUSSES = 'https://leetcode.com/graphql' ;
19+ var URL_DISCUSS = 'https://leetcode.com/problems/$slug/discuss/$id' ;
3720
3821function getSolution ( problem , lang , cb ) {
3922 if ( ! problem ) return cb ( ) ;
4023
41- request ( URL_DISCUSSES + problem . discuss , function ( e , resp , body ) {
24+ var opts = {
25+ url : URL_DISCUSSES ,
26+ json : true ,
27+ qs : {
28+ query : [
29+ 'query fetchTopics($questionId: Int!, $pageNo: Int!, $orderBy: String!) {' ,
30+ ' questionTopics(questionId: $questionId, pageNo: $pageNo, orderBy: $orderBy) {' ,
31+ ' data {' ,
32+ ' id' ,
33+ ' title' ,
34+ ' post {' ,
35+ ' content' ,
36+ ' voteCount' ,
37+ ' author {' ,
38+ ' username' ,
39+ ' }' ,
40+ ' }' ,
41+ ' }' ,
42+ ' }' ,
43+ '}'
44+ ] . join ( '\n' ) ,
45+ operationName : 'fetchTopics' ,
46+ variables : JSON . stringify ( {
47+ pageNo : 1 ,
48+ orderBy : 'most_votes' ,
49+ questionId : problem . id
50+ } )
51+ }
52+ } ;
53+ request ( opts , function ( e , resp , body ) {
4254 if ( e ) return cb ( e ) ;
4355 if ( resp . statusCode !== 200 )
4456 return cb ( { msg : 'http error' , statusCode : resp . statusCode } ) ;
@@ -51,16 +63,16 @@ function getSolution(problem, lang, cb) {
5163 if ( lang === 'javascript' ) langs . push ( 'js' ) ;
5264 if ( lang === 'python3' ) langs . push ( 'python' ) ;
5365
54- var data = JSON . parse ( body ) ;
55- var solution = _ . find ( data . topics , function ( x ) {
56- var keys = x . title . toLowerCase ( ) . split ( ' ' ) ;
66+ var solutions = body . data . questionTopics . data ;
67+ var solution = _ . find ( solutions , function ( x ) {
68+ var keys = x . title . toLowerCase ( ) . split ( / [ ^ \w + ] / ) ;
5769 for ( var i = 0 ; i < keys . length ; ++ i ) {
5870 if ( langs . indexOf ( keys [ i ] ) >= 0 ) return true ;
5971 }
6072 return false ;
6173 } ) ;
6274
63- return getSolutionDetail ( solution , cb ) ;
75+ return cb ( null , solution ) ;
6476 } ) ;
6577}
6678
@@ -73,16 +85,19 @@ plugin.getProblem = function(problem, cb) {
7385 if ( e ) return cb ( e ) ;
7486 if ( ! solution ) return log . error ( 'Solution not found for ' + lang ) ;
7587
88+ var link = URL_DISCUSS . replace ( '$slug' , problem . slug ) . replace ( '$id' , solution . id ) ;
89+ var content = solution . post . content . replace ( / \\ n / g, '\n' ) . replace ( / \\ t / g, '\t' ) ;
90+
7691 log . info ( ) ;
7792 log . info ( solution . title ) ;
7893 log . info ( ) ;
79- log . info ( chalk . underline ( URL_DISCUSS_TOPIC + solution . slug ) ) ;
94+ log . info ( chalk . underline ( link ) ) ;
8095 log . info ( ) ;
8196 log . info ( '* Lang: ' + lang ) ;
82- log . info ( '* Author: ' + solution . user . username ) ;
83- log . info ( '* Votes: ' + solution . votes ) ;
97+ log . info ( '* Author: ' + solution . post . author . username ) ;
98+ log . info ( '* Votes: ' + solution . post . voteCount ) ;
8499 log . info ( ) ;
85- log . info ( solution . content ) ;
100+ log . info ( content ) ;
86101 } ) ;
87102 } ) ;
88103} ;
0 commit comments