1
1
var _ = require ( 'underscore' ) ;
2
+ var cheerio = require ( 'cheerio' ) ;
2
3
var request = require ( 'request' ) ;
3
4
4
5
var log = require ( '../log' ) ;
@@ -16,49 +17,72 @@ var plugin = new Plugin(200, 'solution.discuss', '2017.07.29',
16
17
17
18
var URL_DISCUSSES = 'https://discuss.leetcode.com/api/category/' ;
18
19
var URL_DISCUSS_TOPIC = 'https://discuss.leetcode.com/topic/' ;
20
+ var URL_DISCUSS_TOPIC_API = 'https://discuss.leetcode.com/api/topic/' ;
19
21
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 ( ) ;
23
24
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 } ) ;
31
29
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
+ }
40
37
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 ( ) ;
49
40
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 ;
52
59
}
60
+ return false ;
61
+ } ) ;
53
62
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 ) ;
55
78
log . info ( ) ;
56
79
log . info ( chalk . underline ( URL_DISCUSS_TOPIC + solution . slug ) ) ;
57
80
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 ) ;
60
84
log . info ( ) ;
61
- log . info ( chalk . yellow ( solution . _imported_content ) ) ;
85
+ log . info ( solution . content ) ;
62
86
} ) ;
63
87
} ) ;
64
88
} ;
0 commit comments