const fetch = require('isomorphic-fetch')
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const tableify = require('tableify');
const juice = require('juice');
const animals = ['🦓', '🦒', '🦔', '🦕', '🦖', '🦝', '🦙', '🦛', '🦘', '🦡', '🦚', '🦢', '🦜', '🦞', '🕊', '🐀', '🐁', '🐂', '🐃', '🐄', '🐅', '🐆', '🐇', '🐈', '🐉', '🐊', '🐋', '🐌', '🐍', '🐎', '🐏', '🐐', '🐑', '🐒', '🐝', '🐠', '🐢', '🐥', '🐫', '🐩', '🐡', '🦈', '🦀', '🦎', '🦏', '🦑', '🦄'];
const prices = [];
const pricesToHighlight = [];
function createAnchor(url, text) {
return `<a href="${url}">${text}</a>`
}
function emailTrimRemover() {
const uniqueContent = `<span style="opacity: 0"> ${Date.now()} </span>`;
return `<div> <p></p> ${uniqueContent} </div>`
}
async function setPrice(url, goalPrice) {
const response = await fetch(url);
const text = await response.text();
console.log(text)
const dom = await new JSDOM(text);
let title = dom.window.document.querySelector("h1").textContent.trim();
const titleWords = title.split(' ');
if (titleWords.length > 5) {
title = titleWords.splice(0, 5).join(' ') + '...';
}
let price;
try {
price = dom.window.document.querySelector(".kib-product-price").textContent;
price = parseFloat(price.trim().slice(1).substring(0, price.indexOf("Chewy")));
} catch(e) {
console.log(e);
price = "?";
}
const row = {product: createAnchor(url, title), current: `$${price}`, goal: `$${goalPrice}`};
prices.push(row);
if (price < goalPrice) {
pricesToHighlight.push(row)
}
}
async function setPrices() {
await setPrice('https://www.chewy.com/tiki-cat-aloha-friends-variety-pack/dp/130430', 10.00);
await setPrice('https://www.chewy.com/tiki-cat-born-carnivore-herring/dp/266430', 15.00);
await setPrice('https://www.chewy.com/tiki-cat-born-carnivore-chicken-egg/dp/266429', 40.00);
await setPrice('https://www.chewy.com/tiki-cat-born-carnivore-chicken/dp/266435', 40.00);
await setPrice('https://www.chewy.com/fresh-news-unscented-non-clumping/dp/46608', 15.00);
}
await setPrices();
const highlightedPricesTableHTML = pricesToHighlight.length ? tableify(pricesToHighlight) : `<p> No current 🌶 deals 😿 </p>`;
const pricesTableHTML = tableify(prices);
const css = '<style> body { font-family: "Roboto", helvetica, arial, sans-serif; font-size: 16px; font-weight: 400; text-rendering: optimizeLegibility; } h3 { color: #1b1e24; font-size: 30px; font-weight: 400; text-transform: uppercase; text-align: center; } p {color: #1b1e24; text-align: center; } table { border: 4px solid #1b1e24; border-spacing: 0px; border-radius: 4px; margin: auto; width: 100%; max-width: 800px; } th { color: white; background: #1b1e24; border-right: 1px solid #343a45; font-size: 18px; padding: 12px; } th:last-child { border-right: none; } tr:hover td { background: #4E5066; color: #FFFFFF; } tr:nth-child(odd) td { background: #EBEBEB; } tr:nth-child(odd):hover td { background: #4E5066; } td { background: #FFFFFF; padding: 12px; border-right: 1px solid #C1C3D1; } td:last-child { border-right: 0px; } a { color: #108CED; } </style>';
const html = juice(`<html lang="en"> <body> <h3>Hottest Deals 🔥</h3> ${highlightedPricesTableHTML} <h3>All Prices 💰</h3> ${pricesTableHTML} ${emailTrimRemover()} <p> Sent by your favorite Chwishlist cat 😸 </p> </body> ${css} </html>`);
const date = new Date();
const animal = animals[Math.floor(Math.random() * animals.length)];
this.subject = `Your Chewy Prices for ${date.getMonth() + 1}/${date.getDate()} ${animal}`;
this.html = html;
this.text = `Hottest Prices:\n${JSON.stringify(pricesToHighlight)}\nNormal Prices:\n${JSON.stringify(prices)}`;