diff --git a/lib/commands/show.js b/lib/commands/show.js index 93f643e..b71c53a 100644 --- a/lib/commands/show.js +++ b/lib/commands/show.js @@ -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: '', @@ -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) { diff --git a/lib/core.js b/lib/core.js index c9df632..38352a6 100644 --- a/lib/core.js +++ b/lib/core.js @@ -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')); diff --git a/lib/plugins/leetcode.cn.js b/lib/plugins/leetcode.cn.js index 77639c7..ecc98a7 100644 --- a/lib/plugins/leetcode.cn.js +++ b/lib/plugins/leetcode.cn.js @@ -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 diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 9e81014..ebefafe 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -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)); diff --git a/test/test_core.js b/test/test_core.js index 1c689f5..58c31a6 100644 --- a/test/test_core.js +++ b/test/test_core.js @@ -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 });
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: