Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9dbf1e5

Browse files
committed
bug-fix: submission sync to async && approve accept
1 parent fc91bbb commit 9dbf1e5

File tree

3 files changed

+65
-65
lines changed

3 files changed

+65
-65
lines changed

.vscode/launch.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@
1616
"request": "launch",
1717
"name": "mine",
1818
"program": "${workspaceFolder}/bin/leetcode",
19-
"args": ["submission", "-l", "C++", "-o", "submissions", "94"]
19+
"args": [
20+
"submission",
21+
"-c",
22+
"true",
23+
"-l",
24+
"C++",
25+
"-o",
26+
"submissions",
27+
"94"
28+
]
2029
},
2130
{
2231
"type": "node",

lib/commands/submission.js

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const cmd = {
3636
describe: 'Where to save submission code',
3737
default: '.'
3838
})
39+
.option('c', {
40+
alias: 'accept',
41+
type: 'boolean',
42+
describe: 'choose accept',
43+
default: false
44+
})
3945
.option('x', {
4046
alias: 'extra',
4147
type: 'boolean',
@@ -85,40 +91,35 @@ function doTask(problem, queue, cb) {
8591
}
8692

8793
function exportSubmission(problem, argv, cb) {
88-
core.getSubmissions(problem, function (e, submissions) {
94+
core.getSubmissions(problem, function (e, submission) {
8995
if (e) return cb(e);
90-
if (submissions.length === 0)
91-
return cb('No submissions?');
92-
93-
// get obj list contain required filetype
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-
})
96+
97+
if ((argv.lang === 'all' || argv.lang === submission.lang)) {
98+
submission.ac = submission.status_display === 'Accepted';
99+
if (argv.accept && !submission.ac) return;
100+
const data = _.extend({}, submission, problem);
101+
data.sid = submission.id;
102+
const basename = file.fmt(config.file.submission, data);
103+
const f = path.join(argv.outdir, basename + h.langToExt(submission.lang));
104+
105+
file.mkdir(argv.outdir);
106+
// skip the existing cached submissions
107+
if (file.exist(f))
108+
return cb(null, chalk.underline(f));
109+
110+
core.getSubmission(submission, function (e, submission) {
111+
if (e) return cb(e);
112+
113+
const opts = {
114+
lang: submission.lang == 'C++' ? 'cpp' : submission.lang,
115+
code: submission.code,
116+
tpl: argv.extra ? 'detailed' : 'codeonly'
117+
};
118+
file.write(f, core.exportProblem(problem, opts));
119+
cb(null, submission.ac ? chalk.green.underline(f)
120+
: chalk.yellow.underline(f));
121+
})
122+
}
122123
});
123124
}
124125

lib/plugins/leetcode.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -352,38 +352,28 @@ plugin.getSubmissions = async (problem, cb) => {
352352

353353
// FIXME: this only return the 1st 20 submissions, we should get next if necessary.
354354
const submissions = JSON.parse(body).submissions_dump;
355-
Promise.all(
356-
submissions.map(async (submission) => {
357-
submission.id = _.last(_.compact(submission.url.split("/")));
358-
const codeOpts = plugin.makeOpts(config.sys.urls.graphql);
359-
codeOpts.headers.Origin = config.sys.urls.base;
360-
codeOpts.headers.Referer = config.sys.urls.problem.replace(
361-
"$slug",
362-
problem.slug
363-
);
364-
codeOpts.json = true;
365-
codeOpts.body = {
366-
query: [
367-
"query mySubmissionDetail($id: ID!) {",
368-
"submissionDetail(submissionId: $id) { code} ",
369-
"}",
370-
].join("\n"),
371-
variables: { id: submission.id },
372-
};
373-
await new Promise((res, rej) => {
374-
request.post(codeOpts, function (e, resp, body) {
375-
submission.code = body.data.submissionDetail.code;
376-
res();
377-
});
378-
});
379-
})
380-
)
381-
.then(() => {
382-
return cb(null, submissions);
383-
})
384-
.catch((err) => {
385-
console.log(err);
355+
submissions.map(async (submission) => {
356+
submission.id = _.last(_.compact(submission.url.split("/")));
357+
const codeOpts = plugin.makeOpts(config.sys.urls.graphql);
358+
codeOpts.headers.Origin = config.sys.urls.base;
359+
codeOpts.headers.Referer = config.sys.urls.problem.replace(
360+
"$slug",
361+
problem.slug
362+
);
363+
codeOpts.json = true;
364+
codeOpts.body = {
365+
query: [
366+
"query mySubmissionDetail($id: ID!) {",
367+
"submissionDetail(submissionId: $id) { code} ",
368+
"}",
369+
].join("\n"),
370+
variables: { id: submission.id },
371+
};
372+
request.post(codeOpts, function (e, resp, body) {
373+
submission.code = body.data.submissionDetail.code;
374+
cb(null, submission)
386375
});
376+
});
387377
});
388378
};
389379

0 commit comments

Comments
 (0)