forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathleetcode_client.js
108 lines (89 loc) · 3.07 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
var cheerio = require('cheerio'),
request = require('request'),
_ = require('underscore');
var config = require('./config');
function makeOpts(url) {
var opts = { url: url, headers: {} };
var core = require('./core');
if (core.isLogin()) {
var user = core.getUserSync();
opts.headers['Cookie'] = 'PHPSESSID='+user.session_id+';csrftoken='+user.session_csrf+';';
}
return opts;
}
var leetcode_client = {};
leetcode_client.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'),
percent: $(tds[3]).text(),
level: $(tds[6]).text()
};
// fixup problem attributes
problem.key = _.last(_.compact(problem.link.split('/')));
problem.link = config.BASE_URL + problem.link;
return problem;
}).get();
return cb(null, problems);
});
};
leetcode_client.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.desc = $('meta[property="og:description"]').attr('content');
problem.total_ac = $(info[0]).text();
problem.total_submit = $(info[1]).text();
return cb(null, problem);
});
};
function getCookie(resp, name) {
var cookies = resp.headers['set-cookie'];
if (!cookies) return null;
for (var i=0; i<cookies.length; ++i) {
var sections = cookies[i].split(';');
for (var j=0; j<sections.length; ++j) {
var kv = sections[j].trim().split('=');
if (kv[0] == name) return kv[1];
}
}
}
leetcode_client.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.csrf = getCookie(resp, 'csrftoken');
var opts = {
url: config.LOGIN_URL,
headers: {
Origin: config.BASE_URL,
Referer: config.LOGIN_URL,
Cookie: 'csrftoken='+user.csrf+';'
},
form: {
csrfmiddlewaretoken: user.csrf,
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.session_csrf = getCookie(resp, 'csrftoken');
user.session_id = getCookie(resp, 'PHPSESSID');
user.name = getCookie(resp, 'messages').match('Successfully signed in as ([^.]*)')[1];
return cb(null, user);
});
});
};
module.exports = leetcode_client;