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

fix ["Successfully login as undefined"] when private_favorites is empty #197

Open
wants to merge 4 commits 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
127 changes: 127 additions & 0 deletions lib/commands/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
'use strict';
var path = require('path');

var _ = require('underscore');

var h = require('../helper');
var file = require('../file');
var chalk = require('../chalk');
var config = require('../config');
var log = require('../log');
var Queue = require('../queue');
var core = require('../core');
var session = require('../session');
var md5 = require('md5');

const cmd = {
command: 'export [keyword]',
aliases: ['pulls'],
desc: 'Download submission code',
builder: function(yargs) {
return yargs
.option('o', {
alias: 'outdir',
type: 'string',
describe: 'Where to save submission code',
default: '.'
})
.positional('keyword', {
type: 'string',
default: '',
describe: 'Download specific question by id'
})
.example(chalk.yellow('leetcode export -o mydir'), 'Download all to folder mydir')
.example(chalk.yellow('leetcode export 1'), 'Download cpp submission of question 1');
}
};

function doTask(problem, queue, cb) {
const argv = queue.ctx.argv;

function onTaskDone(e, msg) {
// NOTE: msg color means different purpose:
// - red: error
// - green: accepted, fresh download
// - white: existed already, skip download
log.printf('[%=4s] %-60s %s', problem.fid, problem.name, (e ? chalk.red('ERROR: ' + (e.msg || e)) : msg));
if (cb) cb(e);
}

core.getProblem(problem.fid, function(e, problem) {
if (e) return cb(e);
exportSubmissions(problem, argv, onTaskDone);
});
}

function exportSubmissions(problem, argv, cb) {
core.getSubmissions(problem, function(e, submissions) {
if (e) return cb(e);
if (submissions.length === 0) {
return cb('No submissions?');
}

// get obj list contain required filetype
submissions = submissions.filter(x => x.status_display === 'Accepted');
if (submissions.length === 0) {
return cb(null, "No accepted code");
}


const basename = file.fmt(config.file.export, problem);
const f = path.join(argv.outdir, basename);
file.mkdir(argv.outdir);

const data = _.pick(problem,
"fid",
"name",
"slug",
"link",
"locked",
"percent",
"level",
"category",
"companies",
"tags",
"desc"
);
data.submissions = {};

for (let i = 0; i < submissions.length; i++) {
let submission = submissions[i];
var md5sum = md5(submission.code);
data.submissions[md5sum] = {
"timestamp": h.timeformat(submission.timestamp),
"code": submission.code
};
}

file.write(f, JSON.stringify(data));
cb(null, chalk.green.underline(f));
});
}

cmd.handler = function(argv) {
session.argv = argv;
const q = new Queue(null, {
argv: argv
}, doTask);

if (argv.keyword) {
core.getProblem(argv.keyword, function(e, problem) {
if (e) {
return log.fail(e);
}
if (problem.state === 'ac') {
q.addTask(problem).run();
}
});
return;
}
core.getProblems(function(e, problems) {
if (e) return log.fail(e);
problems = problems.filter(x => x.state === 'ac');
q.addTasks(problems).run();
});
};

module.exports = cmd;
2 changes: 1 addition & 1 deletion lib/commands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ cmd.handler = function(argv) {
problem.fid,
problem.name,
h.prettyLevel(problem.level),
problem.percent.toFixed(2));
problem.percent && problem.percent.toFixed(2));

if (argv.extra) {
let badges = [problem.category];
Expand Down
49 changes: 48 additions & 1 deletion lib/commands/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var log = require('../log');
var config = require('../config');
var core = require('../core');
var session = require('../session');
var cheerio = require('cheerio');
var he = require('he');
var open = require('open')

const cmd = {
command: 'show [keyword]',
Expand All @@ -30,12 +33,36 @@ const cmd = {
type: 'string',
describe: 'Open source code in editor'
})
.option('d', {
alias: 'decode',
type: 'boolean',
default: false,
describe: 'Decode desc to text'
})
.option('z', {
alias: 'translate',
type: 'boolean',
default: false,
describe: 'Translate desc'
})
.option('g', {
alias: 'gen',
type: 'boolean',
default: false,
describe: 'Generate source code'
})
.option('v', {
alias: 'view',
type: 'boolean',
default: false,
describe: 'view content in browser'
})
.option('m', {
alias: 'markdown',
type: 'boolean',
default: false,
describe: 'show url as markdown'
})
.option('l', {
alias: 'lang',
type: 'string',
Expand Down Expand Up @@ -137,6 +164,15 @@ function showProblem(problem, argv) {
}
}

if (argv.view) {
open(problem.link);
}

if (argv.markdown) {
log.printf('- [ ] %s.%s %s', problem.fid, problem.translatedName, problem.link);
return;
}

log.printf('[%s] %s %s', problem.fid, problem.name,
(problem.starred ? chalk.yellow(icon.like) : icon.empty));
log.info();
Expand All @@ -162,7 +198,18 @@ function showProblem(problem, argv) {
log.printf('* Testcase Example: %s', chalk.yellow(util.inspect(problem.testcase)));

log.info();
log.info(problem.desc);
if (argv.decode) {
problem.desc = problem.desc.replace(/<\/sup>/gm, '').replace(/<sup>/gm, '^');
problem.desc = he.decode(cheerio.load(problem.desc).root().text());

problem.translatedDesc = problem.translatedDesc.replace(/<\/sup>/gm, '').replace(/<sup>/gm, '^');
problem.translatedDesc = he.decode(cheerio.load(problem.translatedDesc).root().text());
}
if (config.translate.enable || argv.translate) {
log.info(problem.translatedDesc);
} else {
log.info(problem.desc);
}
}

cmd.handler = function(argv) {
Expand Down
22 changes: 3 additions & 19 deletions lib/commands/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,13 @@ cmd.handler = function(argv) {
const result = results[0];

printResult(result, 'state');
printLine(result, '%d/%d cases passed (%s)',
result.passed, result.total, result.runtime);
printLine(result, '%d/%d cases passed (%s)', result.passed, result.total, result.runtime);

if (result.ok) {
session.updateStat('ac', 1);
session.updateStat('ac.set', problem.fid);
core.getSubmission({id: result.id}, function(e, submission) {
if (e || !submission || !submission.distributionChart)
return log.warn('Failed to get submission beat ratio.');

const lang = submission.distributionChart.lang;
const scores = submission.distributionChart.distribution;
const myRuntime = parseFloat(result.runtime);

let ratio = 0.0;
for (let score of scores) {
if (parseFloat(score[0]) >= myRuntime)
ratio += parseFloat(score[1]);
}

printLine(result, 'Your runtime beats %d %% of %s submissions',
ratio.toFixed(2), lang);
});
printLine(result, 'Your cpu runtime (%s) beats %d %% of %s submissions', result.runtime, result.runtime_percentile.toFixed(2), result.lang);
printLine(result, 'Your memory runtime (%s) beats %d %% of %s submissions', result.memory, result.memory_percentile.toFixed(2), result.lang);
} else {
printResult(result, 'error');
printResult(result, 'testcase');
Expand Down
3 changes: 2 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const DEFAULT_CONFIG = {
},
file: {
show: '${fid}.${slug}',
submission: '${fid}.${slug}.${sid}.${ac}'
submission: '${fid}.${slug}.${sid}.${ac}',
export: '${fid}.${slug}.json'
},
color: {
enable: true,
Expand Down
3 changes: 2 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ core.getProblem = function(keyword, cb) {

keyword = Number(keyword) || keyword;
const problem = problems.find(function(x) {
return x.fid === keyword || x.name === keyword || x.slug === keyword;
return Number(x.fid) === keyword || x.fid === keyword || x.name === keyword || x.slug === keyword;
});
if (!problem) return cb('Problem not found!');
core.next.getProblem(problem, cb);
Expand All @@ -116,6 +116,7 @@ core.exportProblem = function(problem, opts) {
data.comment = h.langToCommentStyle(data.lang);
data.percent = data.percent.toFixed(2);
data.testcase = util.inspect(data.testcase || '');
data.currentTime = h.timeformat();

if (opts.tpl === 'detailed') {
// NOTE: wordwrap internally uses '\n' as EOL, so here we have to
Expand Down
32 changes: 20 additions & 12 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var _ = require('underscore');
var ora = require('ora');
var moment = require('moment');

var file = require('./file');

Expand Down Expand Up @@ -31,21 +32,21 @@ function getUnit(units, v) {

const LANGS = [
{lang: 'bash', ext: '.sh', style: '#'},
{lang: 'c', ext: '.c', style: 'c'},
{lang: 'cpp', ext: '.cpp', style: 'c'},
{lang: 'csharp', ext: '.cs', style: 'c'},
{lang: 'golang', ext: '.go', style: 'c'},
{lang: 'java', ext: '.java', style: 'c'},
{lang: 'javascript', ext: '.js', style: 'c'},
{lang: 'kotlin', ext: '.kt', style: 'c'},
{lang: 'c', ext: '.c', style: '//'},
{lang: 'cpp', ext: '.cpp', style: '//'},
{lang: 'csharp', ext: '.cs', style: '//'},
{lang: 'golang', ext: '.go', style: '//'},
{lang: 'java', ext: '.java', style: '//'},
{lang: 'javascript', ext: '.js', style: '//'},
{lang: 'kotlin', ext: '.kt', style: '//'},
{lang: 'mysql', ext: '.sql', style: '--'},
{lang: 'php', ext: '.php', style: 'c'},
{lang: 'php', ext: '.php', style: '//'},
{lang: 'python', ext: '.py', style: '#'},
{lang: 'python3', ext: '.py', style: '#'},
{lang: 'ruby', ext: '.rb', style: '#'},
{lang: 'rust', ext: '.rs', style: 'c'},
{lang: 'scala', ext: '.scala', style: 'c'},
{lang: 'swift', ext: '.swift', style: 'c'}
{lang: 'rust', ext: '.rs', style: '//'},
{lang: 'scala', ext: '.scala', style: '//'},
{lang: 'swift', ext: '.swift', style: '//'}
];

const h = {};
Expand All @@ -55,7 +56,7 @@ h.KEYS = {
stat: '../stat',
plugins: '../../plugins',
problems: 'problems',
problem: p => p.fid + '.' + p.slug + '.' + p.category
problem: p => p.fid + '.' + p.slug
};

h.prettyState = function(state) {
Expand Down Expand Up @@ -186,6 +187,13 @@ h.spin = function(s) {
return ora(require('./chalk').gray(s)).start();
};

h.timeformat = function(date = 0) {
if (date == 0) {
return moment().format('YYYY-MM-DD HH:mm:ss');
}
return moment.unix(date).format('YYYY-MM-DD HH:mm:ss');
};

const COLORS = {
blue: {fg: 'white', bg: 'bgBlue'},
cyan: {fg: 'white', bg: 'bgCyan'},
Expand Down
Loading