-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (73 loc) · 2.42 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
const nodeMailer = require('nodemailer');
require('dotenv').config();
const puppeteer = require('puppeteer');
let giphList = [];
const giph = async () => {
const browser = await puppeteer.launch({
executablePath: process.env.NODE_ENV === 'production'
? process.env.PUPPETEER_EXECUTABLE_PATH
: puppeteer.executablePath(),
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
await page.goto(`https://giphy.com/search/${process.env.SEARCH_TERM}}`);
await page.waitForSelector('div.giphy-grid div:first-of-type a img.giphy-gif-img');
const scrollDown = async () => {
await page.evaluate(() => {
window.scrollBy(0, window.innerHeight);
});
};
for (let i = 0; i < 2; i++) {
await scrollDown();
await page.waitForTimeout(1000);
}
await page.waitForSelector('img.giphy-gif-img.giphy-img-loaded')
const giph = await page.evaluate(() => {
const images = Array.from(document.querySelectorAll('img.giphy-gif-img.giphy-img-loaded')).map((image) => image.src);
return images.slice((images.length/2 + 1), images.length);
});
giphList.push(giph);
await browser.close();
};
const transporter = nodeMailer.createTransport({
service: 'hotmail',
auth: {
user: process.env.MY_EMAIL,
pass: process.env.MY_PASSWORD,
},
});
const gifLink = async () => {
await giph()
const randomIndex = Math.floor(Math.random() * giphList[0].length)
return giphList[0][randomIndex];
};
const getEmailContent = async () => {
const gifSrc = await gifLink();
return `
<h1>${process.env.MESSAGE_HEADING}</h1>
<h2>${process.env.MESSAGE_BODY}</h2>
<img src="${gifSrc}" alt="Cool GIF">
`;
};
const sendEmail = async () => {
try {
const emailContent = await getEmailContent();
const mailOptions = {
from: process.env.MY_EMAIL,
to: process.env.EMAIL_TO,
subject: 'Sending gif',
html: emailContent
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent:' + info.response);
}
});
} catch (error) {
console.error("Failed to send email:", error);
}
};
sendEmail();