-
Notifications
You must be signed in to change notification settings - Fork 53
/
aws-lambda.js
73 lines (63 loc) · 2.06 KB
/
aws-lambda.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
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
});
exports.handler = function(event, context, callback) {
request(url, function(err, res, body) {
if (err) {
callback('Request failed');
return;
}
var $ = cheerio.load(body);
getBookUrl = 'https://www.packtpub.com' + $("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) {
callback('Login failed');
return;
};
var $ = cheerio.load(body);
var loginFailed = $("div.error:contains('"+loginError+"')");
if (loginFailed.length) {
callback('Login failed, please check your email address and password');
return;
}
request(getBookUrl, function(err, res, body) {
if (err) {
callback('Request Error');
return;
}
console.log({
title: bookTitle,
url: getBookUrl,
});
});
});
});
}