Skip to content

Commit 12e2183

Browse files
committed
Add 'submission -a'
Signed-off-by: Eric Wang <[email protected]>
1 parent 008260f commit 12e2183

File tree

4 files changed

+95
-26
lines changed

4 files changed

+95
-26
lines changed

lib/commands/submission.js

+56-22
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,78 @@ var sprintf = require('sprintf-js').sprintf;
66

77
var core = require('../core');
88
var h = require('../helper');
9+
var queue = require('../queue');
910

1011
var cmd = {
11-
command: 'submission <keyword>',
12+
command: 'submission [keyword]',
1213
desc: 'Retrieve earlier submission by name or index',
1314
builder: {
14-
// TODO: retrieve all?? That would be very time costing...
15+
all: {
16+
alias: 'a',
17+
type: 'boolean',
18+
describe: 'Retrieve for all problems'
19+
}
1520
}
1621
};
1722

18-
cmd.handler = function(argv) {
19-
core.getProblem(argv.keyword, function(e, problem) {
20-
if (e) return console.log('ERROR:', e);
23+
function getSubmissionDone(e, msg, problem, cb) {
24+
console.log(sprintf('[%3d] %-60s %s',
25+
problem.id,
26+
problem.name,
27+
(e ? chalk.red('ERROR: ' + e) : chalk.yellow.underline(msg))
28+
));
29+
if (cb) cb(e);
30+
}
2131

22-
core.getSubmissions(problem, function(e, submissions) {
23-
if (e) return console.log('ERROR:', e);
32+
function getSubmission(problem, cb) {
33+
var done = _.partial(getSubmissionDone, _, _, problem, cb);
2434

25-
console.log('Total: %s submissions', chalk.yellow(submissions.length));
35+
core.getSubmissions(problem, function(e, submissions) {
36+
if (e) return done(e);
37+
if (submissions.length === 0) return done('no submissions?');
2638

27-
// Find the latest accepted one
28-
var submission = _.find(submissions, function(x) {
29-
// TODO: select by lang? or always select the latest one?
30-
return x.state === 'Accepted';
31-
});
39+
// find the latest accepted one
40+
var submission = _.find(submissions, function(x) {
41+
// TODO: select by lang?
42+
return x.state === 'Accepted';
43+
});
3244

33-
if (!submission) {
34-
console.log('No Accepted found?');
35-
return;
36-
}
45+
// if no accepted, use the latest non-accepted one
46+
submission = submission || submissions[0];
3747

38-
core.getSubmission(submission, function(e, submission) {
39-
if (e) return console.log('ERROR:', e);
48+
core.getSubmission(submission, function(e, submission) {
49+
if (e) return done(e);
4050

41-
var f = problem.key + '.' + submission.id + h.langToExt(submission.lang);
42-
fs.writeFileSync(f, submission.code);
51+
var f = sprintf('%s.%s.%s%s', problem.key, submission.id,
52+
problem.state, h.langToExt(submission.lang));
53+
fs.writeFileSync(f, submission.code);
4354

44-
console.log(sprintf('Saved: %s', chalk.yellow.underline(f)));
55+
done(null, f);
56+
});
57+
});
58+
}
59+
60+
cmd.handler = function(argv) {
61+
if (argv.all) {
62+
core.getProblems(function(e, problems) {
63+
if (e) return console.log('ERROR:', e);
64+
65+
problems = problems.filter(function(q) {
66+
return q.state === 'ac';
4567
});
68+
69+
queue.run(problems, getSubmission);
4670
});
71+
return;
72+
}
73+
74+
if (!argv.keyword)
75+
return console.log('ERROR: missing keyword?');
76+
77+
core.getProblem(argv.keyword, function(e, problem) {
78+
if (e) return console.log('ERROR:', e);
79+
80+
queue.run([problem], getSubmission);
4781
});
4882
};
4983

lib/config.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ var DEFAULT_CONFIG = {
1515
URL_VERIFY: 'https://leetcode.com/submissions/detail/$id/check/',
1616

1717
// but you will want change these
18-
LANG: 'cpp', // avail: [c,cpp,csharp,golang,java,javascript,python,ruby,swift]
19-
USE_COLOR: true,
20-
AUTO_LOGIN: false
18+
LANG: 'cpp', // avail: [c,cpp,csharp,golang,java,javascript,python,ruby,swift]
19+
USE_COLOR: true,
20+
AUTO_LOGIN: false,
21+
MAX_WORKERS: 10
2122
};
2223

2324
function Config() {}

lib/leetcode_client.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ leetcodeClient.getSubmissions = function(problem, cb) {
158158
lang: $(tds[4]).text()
159159
};
160160

161-
submission.id = _.last(_.compact(submission.path.split('/')));
161+
if (submission.path)
162+
submission.id = _.last(_.compact(submission.path.split('/')));
162163

163164
return submission;
164165
}).get();

lib/queue.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var config = require('./config');
2+
3+
var queue = {};
4+
5+
function startWorker(ctx) {
6+
// no more tasks, quit now
7+
if (ctx.tasks.length === 0) {
8+
if (--ctx.workers === 0 && ctx.cb)
9+
ctx.cb();
10+
return;
11+
}
12+
13+
var task = ctx.tasks.shift();
14+
ctx.doTask(task, function(e) {
15+
// TODO: could retry failed task here.
16+
startWorker(ctx);
17+
});
18+
}
19+
20+
queue.run = function(tasks, doTask, cb) {
21+
var ctx = {
22+
tasks: tasks,
23+
doTask: doTask,
24+
cb: cb,
25+
workers: config.MAX_WORKERS
26+
};
27+
28+
for (var i = 0; i < ctx.workers; ++i) {
29+
startWorker(ctx);
30+
}
31+
};
32+
33+
module.exports = queue;

0 commit comments

Comments
 (0)