forked from sawyerh/lambda-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (113 loc) · 3.32 KB
/
Copy pathindex.js
File metadata and controls
131 lines (113 loc) · 3.32 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var async = require('async');
var aws = require('aws-sdk');
var config = require('./config');
var request = require('request');
var pages = require('./pages');
var parseHTML = require('./parsers/html');
var parseJSON = require('./parsers/json');
var debug = false;
var savedState;
var awsConfig = new aws.Config({
accessKeyId: config.aws_key,
secretAccessKey: config.aws_secret,
region: config.aws_region
});
var s3 = new aws.S3(awsConfig);
exports.handler = function() {
var params = {
Bucket: config.aws_bucket,
Key: config.filename
};
s3.getObject(params, function(err, data) {
if (err) console.log(err.code, config.filename);
savedState = (err && err.code === "NoSuchKey") ? [] : JSON.parse(data.Body);
var results = [];
async.eachSeries(pages, (page, cb) => {
request(page.url, (error, response, body) => {
var pageResults = page.json ? parseJSON(page, body, savedState) : parseHTML(page, body, savedState);
if (debug)
console.log(`Found results for ${page.url}`, pageResults);
results = results.concat(pageResults);
cb();
});
}, () => {
if (results.length) {
sendResults(results);
} else {
console.log("No matching results");
}
updateState(results);
});
});
};
/**
* Email the found results through SES to the email specified in the config
* @param {array} results - The new results we've found
*/
var sendResults = function(results) {
var body = getResultsEmailHTML(results);
if (debug)
return console.log("Email", body);
var ses = new aws.SES(awsConfig);
var params = {
Destination: {
ToAddresses: [config.email_to]
},
Message: {
Body: {
Html: {
Data: body
},
Text: {
Data: JSON.stringify(results)
}
},
Subject: {
Data: config.email_subject
}
},
Source: config.email_from
};
ses.sendEmail(params, function(err) {
if (err) console.log(err, err.stack);
else console.log(`Emailed ${results.length} results`);
});
};
/**
* Creates the email HTML for the given results
* @param {array} results - The new results we've found
* @return {string} The email's HTML
*/
var getResultsEmailHTML = function(results) {
return results.map(result => {
return Object.getOwnPropertyNames(result).map(key => {
return `<strong>${key}</strong>: ${getHTMLValue(key, result[key])}`;
}).join('<br />');
}).join('<br /><br />');
};
var getHTMLValue = function(key, value) {
if (key === 'image' || key === 'thumbnail') {
return `<br /><img style="max-width: 100%;" src="${value}" alt="${value}" />`;
}
return value;
};
/**
* Update S3 state file with the new URL's scraped
* @param {array} results - The new results we've found
*/
var updateState = function(results) {
if (!results.length) return;
var pendingState = results
.map(r => r.url)
.filter(url => !!url);
if (debug)
return console.log(`Add ${pendingState.length} new items to the saved state`);
s3.putObject({
Bucket: config.aws_bucket,
Key: config.filename,
Body: JSON.stringify(savedState.concat(pendingState))
}, function(err) {
if (err) console.log(err, err.stack);
else console.log(`Added ${pendingState.length} items to saved state`);
});
};