-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
83 lines (83 loc) · 2.59 KB
/
test.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
var request = require("request");
var config = require("./config");
config.app = "leetcode";
config.init()
let user = {login: "ayang879@gmail.com", password: "github901103"};
test(user, () => {
console.log("it callback");
});
function test(user, cb) {
const urls = config.sys.urls;
const leetcodeUrl = urls.github_login;
const _request = request.defaults({jar: true});
_request(urls.github_login_request, function (e, resp, body) {
const authenticityToken = body.match(
/name="authenticity_token" value="(.*?)"/
);
if (authenticityToken === null) {
return cb("Get GitHub token failed");
}
const options = {
url: urls.github_session_request,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
followAllRedirects: true,
form: {
login: user.login,
password: user.pass,
authenticity_token: authenticityToken[1],
utf8: encodeURIComponent("✓"),
commit: encodeURIComponent("Sign in"),
},
};
_request(options, function (e, resp, body) {
if (resp.statusCode !== 200) {
return cb("GitHub login failed");
}
if (resp.request.uri.href !== urls.github_tf_redirect) {
return requestLeetcodeAndSave(_request, leetcodeUrl, user, cb);
}
prompt.colors = false;
prompt.message = "";
prompt.start();
prompt.get(
[
{
name: "twoFactorCode",
required: true,
},
],
function (e, result) {
if (e) return log.fail(e);
const authenticityTokenTwoFactor = body.match(
/name="authenticity_token" value="(.*?)"/
);
if (authenticityTokenTwoFactor === null) {
return cb("Get GitHub two-factor token failed");
}
const optionsTwoFactor = {
url: urls.github_tf_session_request,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
followAllRedirects: true,
form: {
otp: result.twoFactorCode,
authenticity_token: authenticityTokenTwoFactor[1],
utf8: encodeURIComponent("✓"),
},
};
_request(optionsTwoFactor, function (e, resp, body) {
if (resp.request.uri.href === urls.github_tf_session_request) {
return cb("Invalid two-factor code please check");
}
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb);
});
}
);
});
});
}