forked from draconar/grab_packt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (40 loc) · 1.62 KB
/
server.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
var request = require('request');
var cheerio = require('cheerio');
//we need cookies for that, therefore let's turn JAR on
request = request.defaults({
jar: true
});
var loginDetails = { email: "[email protected]", password: "xxxxxxxx", op: "Login", form_id: "packt_user_login_form", form_build_id: "" };
var url = 'https://www.packtpub.com/packt/offers/free-learning';
var getBookUrl;
request(url, function(err, res, body) {
if(err) {
callback.call(null, new Error('Request failed'));
return;
}
var $ = cheerio.load(body);
getBookUrl = $("a.twelve-days-claim").attr("href");
var newFormId = $("input[type='hidden'][id^=form][value^=form]").val();
if (newFormId) {
loginDetails.form_build_id = newFormId;
}
request.post({
uri: url,
headers: { 'content-type': 'application/x-www-form-urlencoded' },
body: require('querystring').stringify(loginDetails)
}, function(err, res, body){
if(err) {
callback.call(null, new Error('Login failed'));
return;
};
request('https://www.packtpub.com'+getBookUrl, function(err, res, body) {
if(err) {
callback.call(null, new Error('Request Error'));
return;
}
var $ = cheerio.load(body);
console.log('https://www.packtpub.com'+getBookUrl);
console.log(Math.random(10))
});
});
});