Skip to content

Commit eb965d0

Browse files
committed
[Cache] use 'slug' to avoid ambiguous.
* NOTE: not compatible with existing cache! Signed-off-by: Eric Wang <[email protected]>
1 parent dfa8759 commit eb965d0

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

lib/config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ var DEFAULT_CONFIG = {
77
URL_BASE: 'https://leetcode.com',
88
URL_LOGIN: 'https://leetcode.com/accounts/login/',
99
URL_PROBLEMS: 'https://leetcode.com/api/problems/algorithms/',
10-
URL_PROBLEM: 'https://leetcode.com/problems/$id',
11-
URL_TEST: 'https://leetcode.com/problems/$key/interpret_solution/',
12-
URL_SUBMIT: 'https://leetcode.com/problems/$key/submit/',
13-
URL_SUBMISSIONS: 'https://leetcode.com/api/submissions/$key',
10+
URL_PROBLEM: 'https://leetcode.com/problems/$slug',
11+
URL_TEST: 'https://leetcode.com/problems/$slug/interpret_solution/',
12+
URL_SUBMIT: 'https://leetcode.com/problems/$slug/submit/',
13+
URL_SUBMISSIONS: 'https://leetcode.com/api/submissions/$slug',
1414
URL_SUBMISSION: 'https://leetcode.com/submissions/detail/$id/',
1515
URL_VERIFY: 'https://leetcode.com/submissions/detail/$id/check/',
1616
URL_FAVORITES: 'https://leetcode.com/list/api/questions',

lib/core.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function saveProblem(problem) {
1515
// independent, thus try to reuse existing cache as much as possible
1616
// after changing user.
1717
var p = _.omit(problem, ['locked', 'state', 'starred']);
18-
return cache.set(p.key, p);
18+
return cache.set(p.slug, p);
1919
}
2020

2121
function saveUser(user) {
@@ -54,14 +54,14 @@ core.getProblem = function(keyword, cb) {
5454
var problem = _.find(problems, function(x) {
5555
return x.id === keyword ||
5656
x.name === keyword ||
57-
x.key === keyword;
57+
x.slug === keyword;
5858
});
5959
if (!problem)
6060
return cb('Problem not found!');
6161

62-
var problemDetail = cache.get(problem.key);
62+
var problemDetail = cache.get(problem.slug);
6363
if (problemDetail) {
64-
log.debug('loading from ' + problem.key + '.json');
64+
log.debug('loading from ' + problem.slug + '.json');
6565
_.extendOwn(problem, problemDetail);
6666
return cb(null, problem);
6767
}

lib/leetcode_client.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ leetcodeClient.getProblems = function(user, cb) {
126126
state: p.status || 'None',
127127
id: p.stat.question_id,
128128
name: p.stat.question__title,
129-
key: p.stat.question__title_slug,
130-
link: config.URL_PROBLEM.replace('$id', p.stat.question__title_slug),
129+
slug: p.stat.question__title_slug,
130+
link: config.URL_PROBLEM.replace('$slug', p.stat.question__title_slug),
131131
locked: p.paid_only,
132132
percent: p.stat.total_acs * 100 / p.stat.total_submitted,
133133
level: h.levelToName(p.difficulty.level),
@@ -182,8 +182,8 @@ leetcodeClient.getProblem = function(user, problem, cb) {
182182

183183
leetcodeClient.getSubmissions = function(problem, cb) {
184184
var opts = makeOpts();
185-
opts.url = config.URL_SUBMISSIONS.replace('$key', problem.key);
186-
opts.headers.Referer = config.URL_PROBLEM.replace('$id', problem.key);
185+
opts.url = config.URL_SUBMISSIONS.replace('$slug', problem.slug);
186+
opts.headers.Referer = config.URL_PROBLEM.replace('$slug', problem.slug);
187187

188188
requestWithReLogin(opts, function(e, resp, body) {
189189
if (e) return cb(e);
@@ -328,7 +328,7 @@ function runCode(opts, problem, cb) {
328328

329329
leetcodeClient.testProblem = function(problem, cb) {
330330
var opts = makeOpts();
331-
opts.url = config.URL_TEST.replace('$key', problem.key);
331+
opts.url = config.URL_TEST.replace('$slug', problem.slug);
332332
opts.body = {'data_input': problem.testcase};
333333

334334
runCode(opts, problem, function(e, task) {
@@ -344,7 +344,7 @@ leetcodeClient.testProblem = function(problem, cb) {
344344

345345
leetcodeClient.submitProblem = function(problem, cb) {
346346
var opts = makeOpts();
347-
opts.url = config.URL_SUBMIT.replace('$key', problem.key);
347+
opts.url = config.URL_SUBMIT.replace('$slug', problem.slug);
348348
opts.body = {'judge_type': 'large'};
349349

350350
runCode(opts, problem, function(e, task) {

test/test_core.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ describe('core', function() {
111111

112112
describe('#problems', function() {
113113
var PROBLEMS = [
114-
{id: 0, name: 'name0', key: 'key0', starred: false},
115-
{id: 1, name: 'name1', key: 'key1', starred: true}
114+
{id: 0, name: 'name0', slug: 'slug0', starred: false},
115+
{id: 1, name: 'name1', slug: 'slug1', starred: true}
116116
];
117117
var RESULTS = [
118118
{name: 'result0'},
@@ -161,7 +161,7 @@ describe('core', function() {
161161
describe('#getProblem', function() {
162162
it('should getProblem by id w/ cache ok', function(done) {
163163
cache.set('all', PROBLEMS);
164-
cache.set('key0', PROBLEMS[0]);
164+
cache.set('slug0', PROBLEMS[0]);
165165

166166
core.getProblem(0, function(e, problem) {
167167
assert.equal(e, null);
@@ -172,7 +172,7 @@ describe('core', function() {
172172

173173
it('should getProblem by name w/ cache ok', function(done) {
174174
cache.set('all', PROBLEMS);
175-
cache.set('key0', PROBLEMS[0]);
175+
cache.set('slug0', PROBLEMS[0]);
176176

177177
core.getProblem('name0', function(e, problem) {
178178
assert.equal(e, null);
@@ -183,9 +183,9 @@ describe('core', function() {
183183

184184
it('should getProblem by key w/ cache ok', function(done) {
185185
cache.set('all', PROBLEMS);
186-
cache.set('key0', PROBLEMS[0]);
186+
cache.set('slug0', PROBLEMS[0]);
187187

188-
core.getProblem('key0', function(e, problem) {
188+
core.getProblem('slug0', function(e, problem) {
189189
assert.equal(e, null);
190190
assert.deepEqual(problem, PROBLEMS[0]);
191191
done();
@@ -194,7 +194,7 @@ describe('core', function() {
194194

195195
it('should getProblem by id w/o cache ok', function(done) {
196196
cache.set('all', PROBLEMS);
197-
cache.del('key0');
197+
cache.del('slug0');
198198

199199
client.getProblem = function(user, problem, cb) {
200200
return cb(null, problem);
@@ -218,7 +218,7 @@ describe('core', function() {
218218

219219
it('should getProblem fail if client error', function(done) {
220220
cache.set('all', PROBLEMS);
221-
cache.del('key0');
221+
cache.del('slug0');
222222

223223
client.getProblem = function(user, problem, cb) {
224224
return cb('client getProblem error');
@@ -246,7 +246,7 @@ describe('core', function() {
246246
describe('#updateProblem', function() {
247247
it('should updateProblem ok', function(done) {
248248
cache.set('all', PROBLEMS);
249-
cache.del('key0');
249+
cache.del('slug0');
250250

251251
var kv = {value: 'value00'};
252252
var ret = core.updateProblem(PROBLEMS[0], kv);
@@ -255,7 +255,7 @@ describe('core', function() {
255255
core.getProblem(0, function(e, problem) {
256256
assert.equal(e, null);
257257
assert.deepEqual(problem,
258-
{id: 0, name: 'name0', key: 'key0', value: 'value00', starred: false});
258+
{id: 0, name: 'name0', slug: 'slug0', value: 'value00', starred: false});
259259
done();
260260
});
261261
});

test/test_leetcode_client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('leetcode_client', function() {
1111
var PROBLEM = {
1212
id: 389,
1313
name: 'Find the Difference',
14-
key: 'find-the-difference',
14+
slug: 'find-the-difference',
1515
link: 'https://leetcode.com/problems/find-the-difference',
1616
locked: false,
1717
file: '/dev/null'
@@ -409,7 +409,7 @@ describe('leetcode_client', function() {
409409
var problem = {
410410
id: 1,
411411
name: 'Two Sum',
412-
key: 'two-sum',
412+
slug: 'two-sum',
413413
link: 'https://leetcode.com/problems/two-sum',
414414
locked: false
415415
};

0 commit comments

Comments
 (0)