Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

feat: Select problem of today.#60

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jtchen2k wants to merge1 commit intoleetcode-tools:master
base:master
Choose a base branch
Loading
fromjtchen2k:master
Open
Show file tree
Hide file tree
Changes fromall 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 deletionslib/commands/show.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionslib/core.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslib/plugins/leetcode.cn.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslib/plugins/leetcode.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstest/test_core.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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
});

[8]ページ先頭

©2009-2025 Movatter.jp