@@ -15,42 +15,42 @@ var session = require('../session');
1515const cmd = {
1616 command : 'submission [keyword]' ,
1717 aliases : [ 'pull' ] ,
18- desc : 'Download submission code' ,
19- builder : function ( yargs ) {
18+ desc : 'Download submission code' ,
19+ builder : function ( yargs ) {
2020 return yargs
2121 . option ( 'a' , {
22- alias : 'all' ,
23- type : 'boolean' ,
24- default : false ,
22+ alias : 'all' ,
23+ type : 'boolean' ,
24+ default : false ,
2525 describe : 'Download all questions'
2626 } )
2727 . option ( 'l' , {
28- alias : 'lang' ,
29- type : 'string' ,
30- default : 'all' ,
28+ alias : 'lang' ,
29+ type : 'string' ,
30+ default : 'all' ,
3131 describe : 'Filter by programming language'
3232 } )
3333 . option ( 'o' , {
34- alias : 'outdir' ,
35- type : 'string' ,
34+ alias : 'outdir' ,
35+ type : 'string' ,
3636 describe : 'Where to save submission code' ,
37- default : '.'
37+ default : '.'
3838 } )
3939 . option ( 'x' , {
40- alias : 'extra' ,
41- type : 'boolean' ,
42- default : false ,
40+ alias : 'extra' ,
41+ type : 'boolean' ,
42+ default : false ,
4343 describe : 'Show extra question details in submission code'
4444 } )
4545 . option ( 'T' , {
46- alias : 'dontTranslate' ,
47- type : 'boolean' ,
48- default : false ,
46+ alias : 'dontTranslate' ,
47+ type : 'boolean' ,
48+ default : false ,
4949 describe : 'Set to true to disable endpoint\'s translation' ,
5050 } )
5151 . positional ( 'keyword' , {
52- type : 'string' ,
53- default : '' ,
52+ type : 'string' ,
53+ default : '' ,
5454 describe : 'Download specific question by id'
5555 } )
5656 . example ( chalk . yellow ( 'leetcode submission -a -o mydir' ) , 'Download all to folder mydir' )
@@ -69,13 +69,13 @@ function doTask(problem, queue, cb) {
6969 // - yellow: not ac-ed, fresh download
7070 // - white: existed already, skip download
7171 log . printf ( '[%=4s] %-60s %s' , problem . fid , problem . name ,
72- ( e ? chalk . red ( 'ERROR: ' + ( e . msg || e ) ) : msg ) ) ;
72+ ( e ? chalk . red ( 'ERROR: ' + ( e . msg || e ) ) : msg ) ) ;
7373 if ( cb ) cb ( e ) ;
7474 }
7575
7676 if ( argv . extra ) {
7777 // have to get problem details, e.g. problem description.
78- core . getProblem ( problem . fid , ! argv . dontTranslate , function ( e , problem ) {
78+ core . getProblem ( problem . fid , ! argv . dontTranslate , function ( e , problem ) {
7979 if ( e ) return cb ( e ) ;
8080 exportSubmission ( problem , argv , onTaskDone ) ;
8181 } ) ;
@@ -85,52 +85,49 @@ function doTask(problem, queue, cb) {
8585}
8686
8787function exportSubmission ( problem , argv , cb ) {
88- core . getSubmissions ( problem , function ( e , submissions ) {
88+ core . getSubmissions ( problem , function ( e , submissions ) {
8989 if ( e ) return cb ( e ) ;
9090 if ( submissions . length === 0 )
9191 return cb ( 'No submissions?' ) ;
9292
9393 // get obj list contain required filetype
94- submissions = submissions . filter ( x => argv . lang === 'all' || argv . lang === x . lang ) ;
95- if ( submissions . length === 0 )
96- return cb ( 'No submissions in required language.' ) ;
97-
98- // if no accepted, use the latest non-accepted one
99- const submission = submissions . find ( x => x . status_display === 'Accepted' ) || submissions [ 0 ] ;
100- submission . ac = ( submission . status_display === 'Accepted' ) ;
101-
102- const data = _ . extend ( { } , submission , problem ) ;
103- data . sid = submission . id ;
104- data . ac = submission . ac ? 'ac' : 'notac' ;
105- const basename = file . fmt ( config . file . submission , data ) ;
106- const f = path . join ( argv . outdir , basename + h . langToExt ( submission . lang ) ) ;
107-
108- file . mkdir ( argv . outdir ) ;
109- // skip the existing cached submissions
110- if ( file . exist ( f ) )
111- return cb ( null , chalk . underline ( f ) ) ;
112-
113- core . getSubmission ( submission , function ( e , submission ) {
114- if ( e ) return cb ( e ) ;
115-
116- const opts = {
117- lang : submission . lang == 'C++' ?'cpp' :submission . lang ,
118- code : submission . code ,
119- tpl : argv . extra ? 'detailed' : 'codeonly'
120- } ;
121- file . write ( f , core . exportProblem ( problem , opts ) ) ;
122- cb ( null , submission . ac ? chalk . green . underline ( f )
123- : chalk . yellow . underline ( f ) ) ;
124- } ) ;
94+ submissions . map ( submission => {
95+ if ( ( argv . lang === 'all' || argv . lang === submission . lang ) && submission . status_display === 'Accepted' ) {
96+ submission . ac = true ;
97+ const data = _ . extend ( { } , submission , problem ) ;
98+ // data.ac = true;
99+ data . sid = submission . id ;
100+ const basename = file . fmt ( config . file . submission , data ) ;
101+ const f = path . join ( argv . outdir , basename + h . langToExt ( submission . lang ) ) ;
102+
103+ file . mkdir ( argv . outdir ) ;
104+ // skip the existing cached submissions
105+ if ( file . exist ( f ) )
106+ return cb ( null , chalk . underline ( f ) ) ;
107+
108+ core . getSubmission ( submission , function ( e , submission ) {
109+ if ( e ) return cb ( e ) ;
110+
111+ const opts = {
112+ lang : submission . lang == 'C++' ? 'cpp' : submission . lang ,
113+ code : submission . code ,
114+ tpl : argv . extra ? 'detailed' : 'codeonly'
115+ } ;
116+ file . write ( f , core . exportProblem ( problem , opts ) ) ;
117+ cb ( null , submission . ac ? chalk . green . underline ( f )
118+ : chalk . yellow . underline ( f ) ) ;
119+ } )
120+ }
121+ } )
125122 } ) ;
126123}
127124
128- cmd . handler = function ( argv ) {
125+ cmd . handler = function ( argv ) {
129126 session . argv = argv ;
130- const q = new Queue ( null , { argv : argv } , doTask ) ;
127+ const q = new Queue ( null , { argv : argv } , doTask ) ;
131128
132129 if ( argv . all ) {
133- core . getProblems ( function ( e , problems ) {
130+ core . getProblems ( function ( e , problems ) {
134131 if ( e ) return log . fail ( e ) ;
135132 problems = problems . filter ( x => x . state === 'ac' || x . state === 'notac' ) ;
136133 q . addTasks ( problems ) . run ( ) ;
@@ -141,7 +138,7 @@ cmd.handler = function(argv) {
141138 if ( ! argv . keyword )
142139 return log . fail ( 'missing keyword?' ) ;
143140
144- core . getProblem ( argv . keyword , ! argv . dontTranslate , function ( e , problem ) {
141+ core . getProblem ( argv . keyword , ! argv . dontTranslate , function ( e , problem ) {
145142 if ( e ) return log . fail ( e ) ;
146143 q . addTask ( problem ) . run ( ) ;
147144 } ) ;
0 commit comments