Add initial price notifications for products and adjust cron schedule to run every 15 minutes. Implement first run logic to send initial prices.
This commit is contained in:
19
index.js
19
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();
|
||||
|
||||
Reference in New Issue
Block a user