-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 1.56 KB
/
index.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
// Command Line Args
const commandLineArgs = require('command-line-args')
const optionDefinitions = [
{ name: 'keyword', alias: 'k', type: String, defaultOption: true },
]
const options = commandLineArgs(optionDefinitions)
if (!options.hasOwnProperty("keyword")) {
console.error('Keyword not provided')
return;
}
//
// File System Utils
const fs = require('fs');
// AWS-SDK S3 setup
var AWS = require('aws-sdk')
var config = {
s3ForcePathStyle: true,
accessKeyId: 'ACCESS_KEY_ID',
secretAccessKey: 'SECRET_ACCESS_KEY',
endpoint: new AWS.Endpoint('http://s3:4569')
}
var s3Client = new AWS.S3(config)
//
// Nightmare
var Nightmare = require('nightmare');
var nightmare = Nightmare({
show: true,
openDevTools: {
mode: 'detach'
},
dock: true,
});
nightmare
.goto('https://duckduckgo.com')
.type('#search_form_input_homepage', options.keyword)
.click('#search_button_homepage')
.wait('.content-wrap')
.then(function () {
return nightmare.evaluate(function() {
return document.querySelector('body').innerHTML;
})
})
.then(function(page) {
fs.writeFileSync(options.keyword+'.html', page);
writeFileToS3(options.keyword+'.html');
})
.then(function (result) {
return nightmare.end();
})
.catch(function (error) {
console.error('Search failed:', error);
});
//
function writeFileToS3(fileName) {
var params = {
Key: fileName,
Bucket: 'duckduckgo_htmls',
Body: fs.createReadStream(fileName)
}
s3Client.upload(params, function uploadCallback (err, data) {
console.log(err, data);
})
}