Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Select problem of today. #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/commands/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ const cmd = {
default: false,
describe: 'Set to true to disable endpoint\'s translation',
})
.option('d', {
alias: 'daily',
type: 'boolean',
default: false,
describe: 'Show question of the day.'
})
.positional('keyword', {
type: 'string',
default: '',
Expand Down Expand Up @@ -179,6 +185,16 @@ function showProblem(problem, argv) {

cmd.handler = function(argv) {
session.argv = argv;

if (argv.daily) {
// Show problem of the day.
core.getProblemOfToday(!argv.dontTranslate, function(e, problem) {
if (e) return log.fail(e);
showProblem(problem, argv);
})
return;
}

if (argv.keyword.length > 0) {
// show specific one
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
Expand Down
15 changes: 15 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ core.getProblem = function(keyword, needTranslation, cb) {
});
};

core.getProblemOfToday = function(needTranslation, cb) {
core.next.getProblemOfToday(needTranslation, function (e, problemSlug) {
if (e) return cb(e);

core.getProblems(needTranslation, function(e, problems) {
if (e) return cb(e);
const problem = problems.find(function(x) {
return x.slug === problemSlug;
})
if (!problem) return cb('Problem not found!');
core.next.getProblem(problem, needTranslation, cb);
});
});
}

core.starProblem = function(problem, starred, cb) {
if (problem.starred === starred) {
log.debug('problem is already ' + (starred ? 'starred' : 'unstarred'));
Expand Down
28 changes: 28 additions & 0 deletions lib/plugins/leetcode.cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ function checkError(e, resp, expectedStatus) {
return e;
}


// Daily Challenge for leetcode-cn.com
plugin.getProblemOfToday = function (needTranslation, cb) {
log.debug('running leetcode.getProblemOfToday...');
const opts = plugin.makeOpts(config.sys.urls.graphql);
opts.headers.Origin = config.sys.urls.base;
opts.headers.Referer = config.sys.urls.base;

opts.json = true;
opts.body = {
query: 'query questionOfToday { todayRecord { question { questionId questionTitleSlug }}}',
variables: {},
operationName: 'questionOfToday'
};

const spin = h.spin('Getting problem of today...');
request.post(opts, function (e, resp, body) {
spin.stop();
e = plugin.checkError(e, resp, 200);
if (e) return cb(e);

const slug = body.data.todayRecord[0].question.questionTitleSlug;
log.debug('Daily problem:', slug);
return cb(null, slug);
});
}


// overloading getProblems here to make sure everything related
// to listing out problems can have a chance to be translated.
// NOTE: Details of the problem is translated inside leetcode.js
Expand Down
26 changes: 26 additions & 0 deletions lib/plugins/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ plugin.getProblems = function (needTranslation, cb) {
});
};

// Daily challenge for leetcode.com
plugin.getProblemOfToday = function(needTranslation, cb) {
log.debug('running leetcode.getProblemOfToday...');
const opts = plugin.makeOpts(config.sys.urls.graphql);
opts.headers.Origin = config.sys.urls.base;
opts.headers.Referer = config.sys.urls.base;

opts.json = true;
opts.body = {
query: 'query questionOfToday { currentDailyCodingChallenge { questionOfToday { question { titleSlug } } } }',
variables: {},
operationName: 'questionOfToday'
};

const spin = h.spin('Getting problem of today...');
request.post(opts, function (e, resp, body) {
spin.stop();
e = plugin.checkError(e, resp, 200);
if (e) return cb(e);

const slug = body.data.currentDailyCodingChallenge.questionOfToday.question.titleSlug;
log.debug('Daily problem:', slug);
return cb(null, slug);
});
}

plugin.getCategoryProblems = function(category, cb) {
log.debug('running leetcode.getCategoryProblems: ' + category);
const opts = plugin.makeOpts(config.sys.urls.problems.replace('$category', category));
Expand Down
10 changes: 10 additions & 0 deletions test/test_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,15 @@ describe('core', function() {
done();
});
});

it('should get problem of today ok', function(done) {
next.getProblemOfToday = (needT, cb) => {
cb(null, 'slug0');
}
core.getProblemOfToday(false, function(e, problem) {
assert.notExists(e);
done();
})
})
}); // #getProblem
});