Skip to content

Commit 4a7612d

Browse files
committedAug 20, 2016
Implement 'submit'
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent b62eb57 commit 4a7612d

File tree

6 files changed

+110
-5
lines changed

6 files changed

+110
-5
lines changed
 

‎lib/commands/show.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cmd.handler = function(argv) {
4040
if (!template)
4141
return console.log('Failed to generate source file: unknown language', argv.lang);
4242

43-
var f = problem.key + h.fileExt(argv.lang);
43+
var f = problem.key + h.langToExt(argv.lang);
4444
fs.writeFileSync(f, template.defaultCode);
4545
msg = sprintf('(File: %s)', f);
4646
}

‎lib/commands/submit.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var util = require('util');
2+
3+
var core = require('../core'),
4+
h = require('../helper');
5+
6+
var cmd = {
7+
command: 'submit <filename>',
8+
desc: 'Submit solution to leetcode.',
9+
builder: {
10+
filename: {
11+
describe: 'Problem source file name.'
12+
}
13+
}
14+
};
15+
16+
cmd.handler = function(argv) {
17+
var keyword = h.getFilename(argv.filename);
18+
core.getProblem(keyword, function(e, problem){
19+
if (e) return console.log('ERROR:', e);
20+
21+
problem.file = argv.filename;
22+
23+
core.submitProblem(problem, function(e, results){
24+
if (e) return console.log('ERROR:', e);
25+
26+
var result = results[0];
27+
var ok = (result.total_correct == result.total_testcases) &&
28+
(result.status_code == 10);
29+
var run_ok = result.run_success;
30+
31+
if (!ok && run_ok) {
32+
console.log(util.format('input:\n%s', result['input']));
33+
}
34+
35+
console.log(util.format(' %s %s',
36+
h.prettyYesNo(ok),
37+
h.statusToName(result.status_code)));
38+
console.log(util.format(' %s %d/%d cases passed (%s)',
39+
h.prettyYesNo(ok),
40+
result.total_correct,
41+
result.total_testcases,
42+
result.status_runtime));
43+
44+
if (!ok && run_ok) {
45+
console.log(util.format(' ✘ output: %s', result['code_output']));
46+
console.log(util.format(' ✔ expected: %s', result['expected_output']));
47+
}
48+
});
49+
});
50+
}
51+
52+
module.exports = cmd;

‎lib/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
module.exports = {
2+
// usually you don't wanna change those
23
BASE_URL: 'https://leetcode.com',
34
LOGIN_URL: 'https://leetcode.com/accounts/login/',
45
PROBLEMS_URL: 'https://leetcode.com/problems/',
56
TEST_URL: 'https://leetcode.com/problems/$key/interpret_solution/',
7+
SUBMIT_URL: 'https://leetcode.com/problems/$key/submit/',
68
VERIFY_URL: 'https://leetcode.com/submissions/detail/$id/check/',
79

10+
// but you will want change these
811
LANG: 'cpp' // others: c,csharp,golang,java,javascript,python,ruby,swift
912
};

‎lib/core.js

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ core.testProblem = function(problem, cb) {
3939
client.testProblem(problem, cb);
4040
};
4141

42+
core.submitProblem = function(problem, cb) {
43+
client.submitProblem(problem, cb);
44+
};
45+
4246
core.login = function(user, cb) {
4347
client.login(user, function(e, user){
4448
if (e) return cb(e);

‎lib/helper.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@ h.prettyState = function(state) {
1111
};
1212
};
1313

14-
h.fileExt = function(lang) {
14+
h.prettyYesNo = function(x) {
15+
return x ? '✔' : '✘';
16+
};
17+
18+
h.statusToName = function(sc) {
19+
switch(sc) {
20+
case 10: return 'Accepted'; break;
21+
case 11: return 'Wrong Answer'; break;
22+
case 12: return 'Memory Limit Exceeded'; break;
23+
case 13: return 'Output Limit Exceeded'; break;
24+
case 14: return 'Time Limit Exceeded'; break;
25+
case 15: return 'Runtime Error'; break;
26+
case 16: return 'Internal Error'; break;
27+
case 20: return 'Compile Error'; break;
28+
case 21: return 'Unknown Error'; break;
29+
default: return 'Unknown'; break;
30+
}
31+
};
32+
33+
h.langToExt = function(lang) {
1534
switch(lang) {
1635
case 'c': return '.c'; break;
1736
case 'cpp': return '.cpp'; break;
@@ -26,7 +45,7 @@ h.fileExt = function(lang) {
2645
}
2746
};
2847

29-
h.fileLang = function(fullpath) {
48+
h.extToLang = function(fullpath) {
3049
var ext = path.extname(fullpath);
3150
switch(ext) {
3251
case '.c': return 'c'; break;

‎lib/leetcode_client.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function makeOpts(url) {
1111
if (core.isLogin()) {
1212
var user = core.getUser();
1313
opts.headers['Cookie'] = 'PHPSESSID='+user.session_id+';csrftoken='+user.session_csrf+';';
14-
//opts.headers['X-CSRFToken'] = user.session_csrf;
14+
opts.headers['X-CSRFToken'] = user.session_csrf;
1515
}
1616
return opts;
1717
}
@@ -132,7 +132,7 @@ leetcode_client.testProblem = function(problem, cb) {
132132
opts.json = true;
133133
opts.body = {
134134
data_input: problem.testcase,
135-
lang: h.fileLang(problem.file),
135+
lang: h.extToLang(problem.file),
136136
question_id: parseInt(problem.id),
137137
test_mode: false,
138138
typed_code: h.fileData(problem.file)
@@ -153,4 +153,31 @@ leetcode_client.testProblem = function(problem, cb) {
153153
});
154154
};
155155

156+
leetcode_client.submitProblem = function(problem, cb) {
157+
var opts = makeOpts();
158+
opts.url = config.SUBMIT_URL.replace('$key', problem.key);
159+
opts.headers['Origin'] = config.BASE_URL;
160+
opts.headers['Referer'] = problem.link;
161+
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
162+
opts.json = true;
163+
opts.body = {
164+
judge_type: 'large',
165+
lang: h.extToLang(problem.file),
166+
question_id: parseInt(problem.id),
167+
test_mode: false,
168+
typed_code: h.fileData(problem.file)
169+
};
170+
171+
request.post(opts, function(e, resp, body){
172+
if (e) return cb(e);
173+
if (resp.statusCode != 200) return cb('HTTP failed:' + resp.statusCode);
174+
175+
opts.json = false;
176+
opts.body = null;
177+
178+
var jobs = [ { name: 'Your', id: body.submission_id } ];
179+
verify_test(opts, jobs, [], cb);
180+
});
181+
};
182+
156183
module.exports = leetcode_client;

0 commit comments

Comments
 (0)
Please sign in to comment.