8000 Retrieve premium user info when login. · junlelu/leetcode-cli@fc7a09b · GitHub
[go: up one dir, main page]

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

Appearance settings

Commit fc7a09b

Browse files
committed
Retrieve premium user info when login.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent b807163 commit fc7a09b

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

lib/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const DEFAULT_CONFIG = {
3030
],
3131
urls: {
3232
base: 'https://leetcode.com',
33+
graphql: 'https://leetcode.com/graphql',
3334
login: 'https://leetcode.com/accounts/login/',
3435
problems: 'https://leetcode.com/api/problems/$category/',
3536
problem: 'https://leetcode.com/problems/$slug/description/',
36-
problem_detail: 'https://leetcode.com/graphql',
3737
test: 'https://leetcode.com/problems/$slug/interpret_solution/',
3838
session: 'https://leetcode.com/session/',
3939
submit: 'https://leetcode.com/problems/$slug/submit/',

lib/plugins/leetcode.js

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ plugin.getCategoryProblems = function(category, cb) {
9191
return cb(session.errors.EXPIRED);
9292
}
9393

94-
const user = session.getUser();
95-
user.paid = json.is_paid;
96-
session.saveUser(user);
97-
9894
const problems = json.stat_status_pairs
9995
.filter(p => !p.stat.question__hide)
10096
.map(function(p) {
@@ -120,9 +116,9 @@ plugin.getCategoryProblems = function(category, cb) {
120116
plugin.getProblem = function(problem, cb) {
121117
log.debug('running leetcode.getProblem');
122118
const user = session.getUser();
123-
//if (problem.locked && !user.paid) return cb('failed to load locked problem!');
119+
if (problem.locked && !user.paid) return cb('failed to load locked problem!');
124120

125-
const opts = makeOpts(config.sys.urls.problem_detail);
121+
const opts = makeOpts(config.sys.urls.graphql);
126122
opts.headers.Origin = config.sys.urls.base;
127123
opts.headers.Referer = problem.link;
128124

@@ -373,7 +369,9 @@ plugin.getFavorites = function(cb) {
373369
log.debug('running leetcode.getFavorites');
374370
const opts = makeOpts(config.sys.urls.favorites);
375371

372+
const spin = h.spin('Retrieving user favorites');
376373
request(opts, function(e, resp, body) {
374+
spin.stop();
377375
e = checkError(e, resp, 200);
378376
if (e) return cb(e);
379377

@@ -382,6 +380,34 @@ plugin.getFavorites = function(cb) {
382380
});
383381
};
384382

383+
plugin.getUser = function(cb) {
384+
log.debug('running leetcode.getUser');
385+
const opts = makeOpts(config.sys.urls.graphql);
386+
opts.headers.Origin = config.sys.urls.base;
387+
opts.headers.Referer = config.sys.urls.base;
388+
opts.json = true;
389+
opts.body = {
390+
query: [
391+
'{',
392+
' user {',
393+
' isCurrentUserPremium',
394+
' }',
395+
'}'
396+
].join('\n'),
397+
variables: {}
398+
};
399+
400+
const spin = h.spin('Retrieving user profile');
401+
request.post(opts, function(e, resp, body) {
402+
spin.stop();
403+
e = checkError(e, resp, 200);
404+
if (e) return cb(e);
405+
406+
const user = body.data.user;
407+
return cb(null, user);
408+
});
409+
};
410+
385411
function runSession(method, data, cb) {
386412
const opts = makeOpts(config.sys.urls.session);
387413
opts.json = true;
@@ -456,25 +482,30 @@ plugin.signin = function(user, cb) {
456482
});
457483
};
458484

459-
plugin.getUser = function(user, cb) {
485+
plugin.initUser = function(user, cb) {
460486
plugin.getFavorites(function(e, favorites) {
461-
if (e) return cb(e);
462-
463-
const favorite = favorites.favorites.private_favorites.find(function(f) {
464-
return f.name === 'Favorite';
487+
if (!e) {
488+
const favorite = favorites.favorites.private_favorites.find(function(f) {
489+
return f.name === 'Favorite';
490+
});
491+
user.hash = favorite.id_hash;
492+
user.name = favorites.user_name;
493+
}
494+
plugin.getUser(function(e, _user) {
495+
if (!e) {
496+
user.paid = _user.isCurrentUserPremium;
497+
}
498+
session.saveUser(user);
499+
return cb(null, user);
465500
});
466-
user.hash = favorite.id_hash;
467-
user.name = favorites.user_name;
468-
session.saveUser(user);
469-
return cb(null, user);
470501
});
471502
};
472503

473504
plugin.login = function(user, cb) {
474505
log.debug('running leetcode.login');
475506
plugin.signin(user, function(e, user) {
476507
if (e) return cb(e);
477-
plugin.getUser(user, cb);
508+
plugin.initUser(user, cb);
478509
});
479510
};
480511

0 commit comments

Comments
 (0)
0