8000 expose more code to plugins. · zhangzhishan/leetcode-cli@bd80b3a · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd80b3a

Browse files
committed
expose more code to plugins.
* leetcode.cn/lintcode could reuse this. Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent b5a5cfa commit bd80b3a

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

lib/plugins/leetcode.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ const plugin = new Plugin(10, 'leetcode', '',
2020
var spin;
2121

2222
// update options with user credentials
23-
function signOpts(opts, user) {
23+
plugin.signOpts = function(opts, user) {
2424
opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId +
2525
';csrftoken=' + user.sessionCSRF + ';';
2626
opts.headers['X-CSRFToken'] = user.sessionCSRF;
2727
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
28-
}
28+
};
2929

30-
function makeOpts(url) {
30+
plugin.makeOpts = function(url) {
3131
const opts = {};
3232
opts.url = url;
3333
opts.headers = {};
3434

3535
if (session.isLogin())
36-
signOpts(opts, session.getUser());
36+
plugin.signOpts(opts, session.getUser());
3737
return opts;
38-
}
38+
};
3939

40-
function checkError(e, resp, expectedStatus) {
40+
plugin.checkError = function(e, resp, expectedStatus) {
4141
if (!e && resp && resp.statusCode !== expectedStatus) {
4242
const code = resp.statusCode;
4343
log.debug('http error: ' + code);
@@ -49,7 +49,7 @@ function checkError(e, resp, expectedStatus) {
4949
}
5050
}
5151
return e;
52-
}
52+
};
5353

5454
plugin.init = function() {
5555
config.app = 'leetcode';
@@ -80,11 +80,11 @@ plugin.getProblems = function(cb) {
8080

8181
plugin.getCategoryProblems = function(category, cb) {
8282
log.debug('running leetcode.getCategoryProblems: ' + category);
83-
const opts = makeOpts(config.sys.urls.problems.replace('$category', category));
83+
const opts = plugin.makeOpts(config.sys.urls.problems.replace('$category', category));
8484

8585
spin.text = 'Downloading category ' + category;
8686
request(opts, function(e, resp, body) {
87-
e = checkError(e, resp, 200);
87+
e = plugin.checkError(e, resp, 200);
8888
if (e) return cb(e);
8989

9090
const json = JSON.parse(body);
@@ -123,7 +123,7 @@ plugin.getProblem = function(problem, cb) {
123123
const user = session.getUser();
124124
if (problem.locked && !user.paid) return cb('failed to load locked problem!');
125125

126-
const opts = makeOpts(config.sys.urls.graphql);
126+
const opts = plugin.makeOpts(config.sys.urls.graphql);
127127
opts.headers.Origin = config.sys.urls.base;
128128
opts.headers.Referer = problem.link;
129129

@@ -149,7 +149,7 @@ plugin.getProblem = function(problem, cb) {
149149
const spin = h.spin('Downloading ' + problem.slug);
150150
request.post(opts, function(e, resp, body) {
151151
spin.stop();
152-
e = checkError(e, resp, 200);
152+
e = plugin.checkError(e, resp, 200);
153153
if (e) return cb(e);
154154

155155
const q = body.data.question;
@@ -191,7 +191,7 @@ function runCode(opts, problem, cb) {
191191
const spin = h.spin('Sending code to judge');
192192
request(opts, function(e, resp, body) {
193193
spin.stop();
194-
e = checkError(e, resp, 200);
194+
e = plugin.checkError(e, resp, 200);
195195
if (e) return cb(e);
196196

197197
if (body.error) {
@@ -224,7 +224,7 @@ function verifyResult(task, queue, cb) {
224224
const spin = h.spin('Waiting for judge result');
225225
request(opts, function(e, resp, body) {
226226
spin.stop();
227-
e = checkError(e, resp, 200);
227+
e = plugin.checkError(e, resp, 200);
228228
if (e) return cb(e);
229229

230230
let result = JSON.parse(body);
@@ -273,7 +273,7 @@ function formatResult(result) {
273273

274274
plugin.testProblem = function(problem, cb) {
275275
log.debug('running leetcode.testProblem');
276-
const opts = makeOpts(config.sys.urls.test.replace('$slug', problem.slug));
276+
const opts = plugin.makeOpts(config.sys.urls.test.replace('$slug', problem.slug));
277277
opts.body = {data_input: problem.testcase};
278278

279279
runCode(opts, problem, function(e, task) {
@@ -292,7 +292,7 @@ plugin.testProblem = function(problem, cb) {
292292

293293
plugin.submitProblem = function(problem, cb) {
294294
log.debug('running leetcode.submitProblem');
295-
const opts = makeOpts(config.sys.urls.submit.replace('$slug', problem.slug));
295+
const opts = plugin.makeOpts(config.sys.urls.submit.replace('$slug', problem.slug));
296296
opts.body = {judge_type: 'large'};
297297

298298
runCode(opts, problem, function(e, task) {
@@ -308,11 +308,11 @@ plugin.submitProblem = function(problem, cb) {
308308

309309
plugin.getSubmissions = function(problem, cb) {
310310
log.debug('running leetcode.getSubmissions');
311-
const opts = makeOpts(config.sys.urls.submissions.replace('$slug', problem.slug));
311+
const opts = plugin.makeOpts(config.sys.urls.submissions.replace('$slug', problem.slug));
312312
opts.headers.Referer = config.sys.urls.problem.replace('$slug', problem.slug);
313313

314314
request(opts, function(e, resp, body) {
315-
e = checkError(e, resp, 200);
315+
e = plugin.checkError(e, resp, 200);
316316
if (e) return cb(e);
317317

318318
// FIXME: this only return the 1st 20 submissions, we should get next if necessary.
@@ -326,10 +326,10 @@ plugin.getSubmissions = function(problem, cb) {
326326

327327
plugin.getSubmission = function(submission, cb) {
328328
log.debug('running leetcode.getSubmission');
329-
const opts = makeOpts(config.sys.urls.submission.replace('$id', submission.id));
329+
const opts = plugin.makeOpts(config.sys.urls.submission.replace('$id', submission.id));
330330

331331
request(opts, function(e, resp, body) {
332-
e = checkError(e, resp, 200);
332+
e = plugin.checkError(e, resp, 200);
333333
if (e) return cb(e);
334334

335335
let re = body.match(/submissionCode:\s('[^']*')/);
@@ -343,7 +343,7 @@ plugin.getSubmission = function(submission, cb) {
343343

344344
plugin.starProblem = function(problem, starred, cb) {
345345
log.debug('running leetcode.starProblem');
346-
const opts = makeOpts();
346+
const opts = plugin.makeOpts();
347347
opts.headers.Origin = config.sys.urls.base;
348348
opts.headers.Referer = problem.link;
349349

@@ -364,7 +364,7 @@ plugin.starProblem = function(problem, starred, cb) {
364364
}
365365

366366
request(opts, function(e, resp, body) {
367-
e = checkError(e, resp, 204);
367+
e = plugin.checkError(e, resp, 204);
368368
if (e) return cb(e);
369369

370370
cb(null, starred);
@@ -373,12 +373,12 @@ plugin.starProblem = function(problem, starred, cb) {
373373

374374
plugin.getFavorites = function(cb) {
375375
log.debug('running leetcode.getFavorites');
376-
const opts = makeOpts(config.sys.urls.favorites);
376+
const opts = plugin.makeOpts(config.sys.urls.favorites);
377377

378378
const spin = h.spin('Retrieving user favorites');
379379
request(opts, function(e, resp, body) {
380380
spin.stop();
381-
e = checkError(e, resp, 200);
381+
e = plugin.checkError(e, resp, 200);
382382
if (e) return cb(e);
383383

384384
const favorites = JSON.parse(body);
@@ -388,7 +388,7 @@ plugin.getFavorites = function(cb) {
388388

389389
plugin.getUserInfo = function(cb) {
390390
log.debug('running leetcode.getUserInfo');
391-
const opts = makeOpts(config.sys.urls.graphql);
391+
const opts = plugin.makeOpts(config.sys.urls.graphql);
392392
opts.headers.Origin = config.sys.urls.base;
393393
opts.headers.Referer = config.sys.urls.base;
394394
opts.json = true;
@@ -407,7 +407,7 @@ plugin.getUserInfo = function(cb) {
407407
const spin = h.spin('Retrieving user profile');
408408
request.post(opts, function(e, resp, body) {
409409
spin.stop();
410-
e = checkError(e, resp, 200);
410+
e = plugin.checkError(e, resp, 200);
411411
if (e) return cb(e);
412412

413413
const user = body.data.user;
@@ -416,15 +416,15 @@ plugin.getUserInfo = function(cb) {
416416
};
417417

418418
function runSession(method, data, cb) {
419-
const opts = makeOpts(config.sys.urls.session);
419+
const opts = plugin.makeOpts(config.sys.urls.session);
420420
opts.json = true;
421421
opts.method = method;
422422
opts.body = data;
423423

424424
const spin = h.spin('Waiting session result');
425425
request(opts, function(e, resp, body) {
426426
spin.stop();
427-
e = checkError(e, resp, 200);
427+
e = plugin.checkError(e, resp, 200);
428428
if (e && e.statusCode === 302) e = session.errors.EXPIRED;
429429

430430
return e ? cb(e) : cb(null, body.sessions);
@@ -459,7 +459,7 @@ plugin.signin = function(user, cb) {
459459
const spin = h.spin('Signing in leetcode.com');
460460
request(config.sys.urls.login, function(e, resp, body) {
461461
spin.stop();
462-
e = checkError(e, resp, 200);
462+
e = plugin.checkError(e, resp, 200);
463463
if (e) return cb(e);
464464

465465
user.loginCSRF = h.getSetCookieValue(resp, 'csrftoken');

0 commit comments

Comments
 (0)
0