@@ -15,42 +15,42 @@ var session = require('../session');
15
15
const cmd = {
16
16
command : 'submission [keyword]' ,
17
17
aliases : [ 'pull' ] ,
18
- desc : 'Download submission code' ,
19
- builder : function ( yargs ) {
18
+ desc : 'Download submission code' ,
19
+ builder : function ( yargs ) {
20
20
return yargs
21
21
. option ( 'a' , {
22
- alias : 'all' ,
23
- type : 'boolean' ,
24
- default : false ,
22
+ alias : 'all' ,
23
+ type : 'boolean' ,
24
+ default : false ,
25
25
describe : 'Download all questions'
26
26
} )
27
27
. option ( 'l' , {
28
- alias : 'lang' ,
29
- type : 'string' ,
30
- default : 'all' ,
28
+ alias : 'lang' ,
29
+ type : 'string' ,
30
+ default : 'all' ,
31
31
describe : 'Filter by programming language'
32
32
} )
33
33
. option ( 'o' , {
34
- alias : 'outdir' ,
35
- type : 'string' ,
34
+ alias : 'outdir' ,
35
+ type : 'string' ,
36
36
describe : 'Where to save submission code' ,
37
- default : '.'
37
+ default : '.'
38
38
} )
39
39
. option ( 'x' , {
40
- alias : 'extra' ,
41
- type : 'boolean' ,
42
- default : false ,
40
+ alias : 'extra' ,
41
+ type : 'boolean' ,
42
+ default : false ,
43
43
describe : 'Show extra question details in submission code'
44
44
} )
45
45
. option ( 'T' , {
46
- alias : 'dontTranslate' ,
47
- type : 'boolean' ,
48
- default : false ,
46
+ alias : 'dontTranslate' ,
47
+ type : 'boolean' ,
48
+ default : false ,
49
49
describe : 'Set to true to disable endpoint\'s translation' ,
50
50
} )
51
51
. positional ( 'keyword' , {
52
- type : 'string' ,
53
- default : '' ,
52
+ type : 'string' ,
53
+ default : '' ,
54
54
describe : 'Download specific question by id'
55
55
} )
56
56
. example ( chalk . yellow ( 'leetcode submission -a -o mydir' ) , 'Download all to folder mydir' )
@@ -69,13 +69,13 @@ function doTask(problem, queue, cb) {
69
69
// - yellow: not ac-ed, fresh download
70
70
// - white: existed already, skip download
71
71
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 ) ) ;
73
73
if ( cb ) cb ( e ) ;
74
74
}
75
75
76
76
if ( argv . extra ) {
77
77
// 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 ) {
79
79
if ( e ) return cb ( e ) ;
80
80
exportSubmission ( problem , argv , onTaskDone ) ;
81
81
} ) ;
@@ -85,52 +85,49 @@ function doTask(problem, queue, cb) {
85
85
}
86
86
87
87
function exportSubmission ( problem , argv , cb ) {
88
- core . getSubmissions ( problem , function ( e , submissions ) {
88
+ core . getSubmissions ( problem , function ( e , submissions ) {
89
89
if ( e ) return cb ( e ) ;
90
90
if ( submissions . length === 0 )
91
91
return cb ( 'No submissions?' ) ;
92
92
93
93
// 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
+ } )
125
122
} ) ;
126
123
}
127
124
128
- cmd . handler = function ( argv ) {
125
+ cmd . handler = function ( argv ) {
129
126
session . argv = argv ;
130
- const q = new Queue ( null , { argv : argv } , doTask ) ;
127
+ const q = new Queue ( null , { argv : argv } , doTask ) ;
131
128
132
129
if ( argv . all ) {
133
- core . getProblems ( function ( e , problems ) {
130
+ core . getProblems ( function ( e , problems ) {
134
131
if ( e ) return log . fail ( e ) ;
135
132
problems = problems . filter ( x => x . state === 'ac' || x . state === 'notac' ) ;
136
133
q . addTasks ( problems ) . run ( ) ;
@@ -141,7 +138,7 @@ cmd.handler = function(argv) {
141
138
if ( ! argv . keyword )
142
139
return log . fail ( 'missing keyword?' ) ;
143
140
144
- core . getProblem ( argv . keyword , ! argv . dontTranslate , function ( e , problem ) {
141
+ core . getProblem ( argv . keyword , ! argv . dontTranslate , function ( e , problem ) {
145
142
if ( e ) return log . fail ( e ) ;
146
143
q . addTask ( problem ) . run ( ) ;
147
144
} ) ;
0 commit comments