forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathsolution.discuss.js
122 lines (104 loc) · 3.7 KB
/
solution.discuss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var request = require('request');
var log = require('../log');
var chalk = require('../chalk');
var Plugin = require('../plugin');
var session = require('../session');
//
// [Usage]
//
// https://github.com/skygragon/leetcode-cli-plugins/blob/master/docs/solution.discuss.md
//
var plugin = new Plugin(200, 'solution.discuss', '2019.02.03',
'Plugin to fetch most voted solution in discussions.');
var URL_DISCUSSES = 'https://leetcode.com/graphql';
var URL_DISCUSS = 'https://leetcode.com/problems/$slug/discuss/$id';
function setupHeaders(opts, user) {
opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId +
';csrftoken=' + user.sessionCSRF + ';';
opts.headers['X-CSRFToken'] = user.sessionCSRF;
opts.headers['x-csrftoken'] = user.sessionCSRF;
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
opts.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0';
opts.headers['Referer'] = 'https://leetcode.com';
opts.headers['Origin'] = 'https://leetcode.com/';
opts.headers['Host'] = 'leetcode.com';
opts.headers['Content-Type'] = 'application/json';
opts.headers['Accept'] = 'application/json';
}
function getSolution(problem, lang, cb) {
if (!problem) return cb();
if (lang === 'python3') lang = 'python';
var opts = {
url: URL_DISCUSSES,
json: true,
body: {
query: [
'query questionTopicsList($questionId: String!, $orderBy: TopicSortingOption, $skip: Int, $query: String, $first: Int!, $tags: [String!]) {',
' questionTopicsList(questionId: $questionId, orderBy: $orderBy, skip: $skip, query: $query, first: $first, tags: $tags) {',
' ...TopicsList',
' }',
'}',
'fragment TopicsList on TopicConnection {',
' totalNum',
' edges {',
' node {',
' id',
' title',
' post {',
' content',
' voteCount',
' author {',
' username',
' }',
' }',
' }',
' }',
'}'
].join('\n'),
operationName: 'questionTopicsList',
variables: JSON.stringify({
query: '',
first: 1,
skip: 0,
orderBy: 'most_votes',
questionId: '' + problem.id,
tags: [lang]
})
}
};
opts.headers = {};
setupHeaders(opts, session.getUser());
request(opts, function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode !== 200)
return cb({msg: 'http error', statusCode: resp.statusCode});
const solutions = body.data.questionTopicsList.edges;
const solution = solutions.length > 0 ? solutions[0].node : null;
return cb(null, solution);
});
}
plugin.getProblem = function(problem, needTranslation, cb) {
plugin.next.getProblem(problem, needTranslation, function(e, problem) {
if (e || !session.argv.solution) return cb(e, problem);
var lang = session.argv.lang;
getSolution(problem, lang, function(e, solution) {
if (e) return cb(e);
if (!solution) return log.error('Solution not found for ' + lang);
var link = URL_DISCUSS.replace('$slug', problem.slug).replace('$id', solution.id);
var content = solution.post.content.replace(/\\n/g, '\n').replace(/\\t/g, '\t');
log.info();
log.info(problem.name);
log.info();
log.info(solution.title);
log.info();
log.info(chalk.underline(link));
log.info();
log.info('* Lang: ' + lang);
log.info('* Author: ' + solution.post.author.username);
log.info('* Votes: ' + solution.post.voteCount);
log.info();
log.info(content);
});
});
};
module.exports = plugin;