@@ -60,9 +60,9 @@ plugin.getProblems = function(cb) {
6060 const getCategory = function ( category , queue , cb ) {
6161 plugin . getCategoryProblems ( category , function ( e , _problems ) {
6262 if ( e ) {
63- log . debug ( category + ': failed to getProblems : ' + e . msg ) ;
63+ log . debug ( category + ': failed to getCategory : ' + e . msg ) ;
6464 } else {
65- log . debug ( category + ': getProblems got ' + _problems . length + ' problems' ) ;
65+ log . debug ( category + ': getCategory got ' + _problems . length + ' problems' ) ;
6666 problems = problems . concat ( _problems ) ;
6767 }
6868 return cb ( e ) ;
@@ -73,10 +73,68 @@ plugin.getProblems = function(cb) {
7373 const q = new Queue ( config . sys . categories , { } , getCategory ) ;
7474 q . run ( null , function ( e ) {
7575 spin . stop ( ) ;
76- return cb ( e , problems ) ;
76+ if ( e ) return cb ( e ) ;
77+ plugin . attachTags ( problems , cb ) ;
7778 } ) ;
7879} ;
7980
81+ plugin . attachTags = function ( problems , cb ) {
82+ problems = new Map ( problems . map ( p => [ p . id , p ] ) ) ;
83+ const getTag = function ( tag , queue , cb ) {
84+ plugin . getTagProblems ( tag , function ( e , _problems ) {
85+ if ( e ) {
86+ log . debug ( tag + ': failed to getTag: ' + e . msg ) ;
87+ } else if ( ! _problems ) {
88+ log . warn ( tag + ': retrieve empty tag' ) ;
89+ } else {
90+ log . debug ( tag + ': getTag got ' + _problems . length + ' problems' ) ;
91+ _problems . forEach ( function ( p ) {
92+ let id = parseInt ( p . questionId ) ;
93+ if ( problems . has ( id ) ) {
94+ problems . get ( id ) . tags . push ( tag ) ;
95+ }
96+ } )
97+ }
98+ return cb ( e ) ;
99+ } ) ;
100+ } ;
101+ spin = h . spin ( "Downloading tags" ) ;
102+ const q = new Queue ( config . sys . tags , { } , getTag ) ;
103+ q . run ( null , function ( e ) {
104+ spin . stop ( ) ;
105+ problems = Array . from ( problems . values ( ) ) ;
106+ return cb ( e , problems ) ;
107+ } ) ;
108+ }
109+
110+ plugin . getTagProblems = function ( tag , cb ) {
111+ log . debug ( 'running leetcode.getTagProblems: ' + tag ) ;
112+ const opts = plugin . makeOpts ( config . sys . urls . graphql ) ;
113+ opts . headers . Origin = config . sys . urls . base ;
114+ opts . headers . Referer = config . sys . urls . tag . replace ( '$tag' , tag ) ;
115+ opts . json = true ;
116+ opts . body = {
117+ query : [
118+ 'query getTopicTag($slug: String!) {' ,
119+ ' topicTag(slug: $slug) {' ,
120+ ' slug' ,
121+ ' questions {' ,
122+ ' questionId' ,
123+ ' }' ,
124+ ' }' ,
125+ '}'
126+ ] . join ( '\n' ) ,
127+ operationName : 'getTopicTag' ,
128+ variables : { slug : tag } ,
129+ } ;
130+ spin . text = 'Downloading tag ' + tag ;
131+ request ( opts , function ( e , resp , body ) {
132+ e = plugin . checkError ( e , resp , 200 ) ;
133+ if ( e ) return cb ( e ) ;
134+ return cb ( null , body . data . topicTag . questions ) ;
135+ } ) ;
136+ }
137+
80138plugin . getCategoryProblems = function ( category , cb ) {
81139 log . debug ( 'running leetcode.getCategoryProblems: ' + category ) ;
82140 const opts = plugin . makeOpts ( config . sys . urls . problems . replace ( '$category' , category ) ) ;
@@ -109,7 +167,8 @@ plugin.getCategoryProblems = function(category, cb) {
109167 percent : p . stat . total_acs * 100 / p . stat . total_submitted ,
110168 level : h . levelToName ( p . difficulty . level ) ,
111169 starred : p . is_favor ,
112- category : json . category_slug
170+ category : json . category_slug ,
171+ tags : [ ]
113172 } ;
114173 } ) ;
115174
0 commit comments