-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
96 lines (87 loc) · 2.68 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const AWS = require('aws-sdk');
const ses = new AWS.SES();
const https = require('https');
const gis = require('g-i-s');
const {
emailaddress,
} = process.env;
const url = 'https://api.icndb.com/jokes/random/';
const emailaddressArray = emailaddress.split(',');
console.log(emailaddressArray); // eslint-disable-line
exports.handler = (event) => {
console.log('Receveived', event); // eslint-disable-line
const opts = {
searchTerm: ['Chuck Norris Movie', 'Chuck Norris Jeans 70s', 'Chuck Norris action'],
queryStringAddition: '&tbs=ic:trans',
filterOutDomains: [
'pinterest.com',
'deviantart.com',
'giphy.com',
'images-na.ssl-images-amazon.com',
'i.dlpng.com',
'www.scoopnest.com',
'whowouldwinafight.com',
'brainstaple.com',
'66.media.tumblr.com',
'jeremysaid.com',
'cdn.shopify.com',
'png2.kisspng.com',
'pbs.twimg.com',
'static1.squarespace.com',
'www.millcreekent.com'
],
};
gis(opts, logResults);
function logResults(resulterror, results) {
if (resulterror) {
console.log(resulterror); // eslint-disable-line
} else {
const arrayRandom = Math.floor(Math.random() * results.length);
const randomImage = results[arrayRandom].url;
console.log(randomImage); // eslint-disable-line
https.get(url, (res) => {
res.on('data', (joke) => {
const parsejoke = JSON.parse(joke);
const sesparams = {
Destination: {
ToAddresses: emailaddressArray,
},
Message: {
Body: {
Html: {
Charset: 'UTF-8',
Data: `
<html>
<body>
<font size="+2"> <b> ${parsejoke.value.joke} <b> </font>
<br>
<br>
<br>
<img src=${randomImage} />
</body>
</html>`,
},
Text: {
Charset: 'UTF-8',
Data: parsejoke.value.joke,
},
},
Subject: {
Charset: 'UTF-8',
Data: 'Daily Random Chuck Norris Jokes',
},
},
Source: '[email protected]',
};
console.log(parsejoke.value.joke);
ses.sendEmail(sesparams, (err, data) => {
if (err) console.log(err, err.stack); // eslint-disable-line
else console.log(data); // eslint-disable-line
});
});
}).on('error', (error) => {
console.error(error); // eslint-disable-line
});
}
}
};