diff --git a/index.js b/index.js index 7695bd3..e26ff5b 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ function sendMessage(message) { }); } +let isFirstRun = true; async function compareAndSave(productsPrice) { if (!fs.existsSync("productsPrice.json")) { fs.writeFileSync("productsPrice.json", "[]"); @@ -40,12 +41,15 @@ async function compareAndSave(productsPrice) { for (const product of diffProducts) { sendMessage( - `Zmiana ceny **${product.name}**:\nCena: ${product.oldPrice} -> ${product.newPrice}\nLink: ${product.link}` + `Zmiana ceny **${product.name}**: ${product.oldPrice} -> ${product.newPrice}\nLink: ${product.link}` ); } - if (diffProducts.length === 0) { - sendMessage("Brak zmian w cenach"); + if (isFirstRun) { + for (const product of productsPrice) { + sendMessage(`Początkowa cena **${product.name}**: ${product.price}`); + } + isFirstRun = false; } fs.writeFileSync( @@ -97,9 +101,14 @@ async function init() { console.log("Sprawdzone! Aktualne ceny zapisane w productsPrice.json"); } -sendMessage("Zaczynam monitoring cen"); -const task = cron.schedule("0 6,9,12,14,16,18,21 * * *", init, { +sendMessage("Startuję monitoring cen"); +const task = cron.schedule("*/15 7-23 * * *", init, { timezone: "Europe/Warsaw", }); +cron.schedule("0 7 * * *", () => { + const date = new Date().toLocaleDateString("pl-PL"); + sendMessage(`Zaczynamy monitoring ${date}`); +}); + task.execute();