forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathleetcode_client.js
195 lines (162 loc) · 5.69 KB
/
leetcode_client.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var _ = require('underscore');
var cheerio = require('cheerio');
var request = require('request');
var config = require('./config');
var h = require('./helper');
function makeOpts(url) {
var opts = {url: url, headers: {}};
var core = require('./core');
if (core.isLogin()) {
var user = core.getUser();
opts.headers.Cookie = 'PHPSESSID=' + user.sessionId +
';csrftoken=' + user.sessionCSRF + ';';
opts.headers['X-CSRFToken'] = user.sessionCSRF;
}
return opts;
}
var leetcodeClient = {};
leetcodeClient.getProblems = function(cb) {
request(makeOpts(config.PROBLEMS_URL), function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode !== 200) return cb('HTTP failed:' + resp.statusCode);
var $ = cheerio.load(body);
var problems = $('#problemList tbody tr').map(function() {
var tds = $(this).children();
var problem = {
state: $(tds[0]).children('span').attr('class'),
id: $(tds[1]).text(),
name: $(tds[2]).children('a').text(),
link: $(tds[2]).children('a').attr('href'),
locked: ($(tds[2]).children('i[class="fa fa-lock"]').length === 1),
percent: $(tds[3]).text(),
level: $(tds[6]).text()
};
// fixup problem attributes
problem.id = parseInt(problem.id, 10);
problem.key = _.last(_.compact(problem.link.split('/')));
problem.link = config.BASE_URL + problem.link;
return problem;
}).get();
return cb(null, problems);
});
};
// hacking ;P
var aceCtrl = {
init: function() {
return Array.prototype.slice.call(arguments);
}
};
leetcodeClient.getProblem = function(problem, cb) {
request(problem.link, function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode !== 200) return cb('HTTP failed:' + resp.statusCode);
var $ = cheerio.load(body);
var info = $('div[class="question-info text-info"] ul li strong');
problem.totalAC = $(info[0]).text();
problem.totalSubmit = $(info[1]).text();
problem.desc = $('meta[property="og:description"]').attr('content');
var raw = $('div[ng-controller="AceCtrl as aceCtrl"]').attr('ng-init');
if (!raw)
return cb('failed to load' + (problem.locked ? ' locked ' : ' ') +
'problem!');
raw = raw.replace(/\n/g, ''); // FIXME: might break test cases!
var args = eval(raw);
problem.templates = args[0];
return cb(null, problem);
});
};
leetcodeClient.login = function(user, cb) {
request(config.LOGIN_URL, function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode !== 200) return cb('HTTP failed:' + resp.statusCode);
user.loginCSRF = h.getSetCookieValue(resp, 'csrftoken');
var opts = {
url: config.LOGIN_URL,
headers: {
Origin: config.BASE_URL,
Referer: config.LOGIN_URL,
Cookie: 'csrftoken=' + user.loginCSRF + ';'
},
form: {
csrfmiddlewaretoken: user.loginCSRF,
login: user.login,
password: user.pass
}
};
request.post(opts, function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode !== 302) return cb('HTTP failed:' + resp.statusCode);
user.sessionCSRF = h.getSetCookieValue(resp, 'csrftoken');
user.sessionId = h.getSetCookieValue(resp, 'PHPSESSID');
user.name = h.getSetCookieValue(resp, 'messages')
.match('Successfully signed in as ([^.]*)')[1];
return cb(null, user);
});
});
};
function verifyResult(opts, jobs, results, cb) {
if (jobs.length === 0)
return cb(null, results);
opts.url = config.VERIFY_URL.replace('$id', jobs[0].id);
request.get(opts, function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode !== 200) return cb('HTTP failed:' + resp.statusCode);
var result = JSON.parse(body);
if (result.state === 'SUCCESS') {
result.name = jobs[0].name;
results.push(result);
jobs.shift();
}
setImmediate(verifyResult, opts, jobs, results, cb);
});
}
leetcodeClient.testProblem = function(problem, cb) {
var opts = makeOpts();
opts.url = config.TEST_URL.replace('$key', problem.key);
opts.headers.Origin = config.BASE_URL;
opts.headers.Referer = problem.link;
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
opts.json = true;
opts.body = {
'data_input': problem.testcase,
'lang': h.extToLang(problem.file),
'question_id': parseInt(problem.id, 10),
'test_mode': false,
'typed_code': h.getFileData(problem.file)
};
request.post(opts, function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode !== 200) return cb('HTTP failed:' + resp.statusCode);
opts.json = false;
opts.body = null;
var jobs = [
{name: 'Your', id: body.interpret_id},
{name: 'Expected', id: body.interpret_expected_id}
];
verifyResult(opts, jobs, [], cb);
});
};
leetcodeClient.submitProblem = function(problem, cb) {
var opts = makeOpts();
opts.url = config.SUBMIT_URL.replace('$key', problem.key);
opts.headers.Origin = config.BASE_URL;
opts.headers.Referer = problem.link;
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
opts.json = true;
opts.body = {
'judge_type': 'large',
'lang': h.extToLang(problem.file),
'question_id': parseInt(problem.id, 10),
'test_mode': false,
'typed_code': h.getFileData(problem.file)
};
request.post(opts, function(e, resp, body) {
if (e) return cb(e);
if (resp.statusCode !== 200) return cb('HTTP failed:' + resp.statusCode);
opts.json = false;
opts.body = null;
var jobs = [{name: 'Your', id: body.submission_id}];
verifyResult(opts, jobs, [], cb);
});
};
module.exports = leetcodeClient;