-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathserver.js
76 lines (67 loc) · 2.42 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require('dotenv').load({
path: __dirname + '/.env'
});
var request = require('request');
var cheerio = require('cheerio');
var loginDetails = {
email: process.env.PACKT_EMAIL,
password: process.env.PACKT_PASSWORD,
op: "Login",
form_id: "packt_user_login_form",
form_build_id: ""
};
var url = 'https://www.packtpub.com/packt/offers/free-learning';
var loginError = 'Sorry, you entered an invalid email address and password combination.';
var getBookUrl;
var bookTitle;
//we need cookies for that, therefore let's turn JAR on
request = request.defaults({
jar: true
});
console.log('----------- Packt Grab Started -----------');
request(url, function(err, res, body) {
if (err) {
console.error('Request failed');
console.log('----------- Packt Grab Done --------------');
return;
}
var $ = cheerio.load(body);
getBookUrl = $("a.twelve-days-claim").attr("href");
bookTitle = $(".dotd-title").text().trim();
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) {
console.error('Login failed');
console.log('----------- Packt Grab Done --------------');
return;
};
var $ = cheerio.load(body);
var loginFailed = $("div.error:contains('"+loginError+"')");
if (loginFailed.length) {
console.error('Login failed, please check your email address and password');
console.log('Login failed, please check your email address and password');
console.log('----------- Packt Grab Done --------------');
return;
}
request('https://www.packtpub.com' + getBookUrl, function(err, res, body) {
if (err) {
console.error('Request Error');
console.log('----------- Packt Grab Done --------------');
return;
}
var $ = cheerio.load(body);
console.log('Book Title: ' + bookTitle);
console.log('Claim URL: https://www.packtpub.com' + getBookUrl);
console.log('----------- Packt Grab Done --------------');
});
});
});