/
daily-stock-email.js
97 lines (90 loc) · 2.39 KB
/
daily-stock-email.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
require('dotenv');
const iex = require('iexcloud_api_wrapper'); // gets auth from .env automatically
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const getMoverData = async() => {
try {
const gainers = await iex.list('gainers');
const losers = await iex.list('losers');
return { gainers, losers };
}
catch(error) {
console.error(`Could not get data: ${error}`);
process.exit(-1); // nonzero exit code indicates failure
}
}
const generateTable = (stockData) => {
stockData = stockData.sort((a, b) =>
Math.abs(a.changePercent) < Math.abs(b.changePercent));
const rows = stockData.map(data =>
`<tr>
<td>${Math.round(data.changePercent * 10000) / 100}</td>
<td>${data.symbol}</td>
<td>${data.companyName}</td>
<td>${data.latestPrice}</td>
<td>${data.previousClose}</td>
<td>${Math.round(data.ytdChange * 100) / 100}</td>
</tr>`
).join('\n');
return `
<table>
<thead>
<tr>
<th>% Change</th>
<th>Symbol</th>
<th>Company</th>
<th>Close</th>
<th>Previous Close</th>
<th>YTD Change</th>
</tr>
</thead>
<tbody>
${rows}
</tbody>
</table>
`;
}
const generateHtml = (gainers, losers) => {
const gainerTable = generateTable(gainers);
const loserTable = generateTable(losers);
return `<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 3px;
}
</style>
</head>
<body>
<h1>Today's Biggest Stock Movers</h1>
<h2>Gainers</h2>
<div>${gainerTable}</div>
<h2>Losers</h2>
<div>${loserTable}</div>
</body>
</html>`;
}
const sendEmail = async (htmlEmailContents) => {
const msg = {
to: process.env.EMAIL,
from: process.env.EMAIL,
subject: 'Today\'s biggest stock market movers',
html: htmlEmailContents,
};
try {
await sgMail.send(msg);
}
catch (error) {
console.error(`Could not send message: ${error}`);
}
}
const main = async () => {
const { gainers, losers } = await getMoverData();
const htmlEmailContents = generateHtml(gainers, losers);
await sendEmail(htmlEmailContents);
}
main()
.then(() => console.log(`Sent stock mover email to ${process.env.EMAIL}!`))
.catch(error => console.error(error));