sending notification when new items arrive with send grid
This commit is contained in:
42
backend/utils/scraptheitems.js
Normal file
42
backend/utils/scraptheitems.js
Normal file
@@ -0,0 +1,42 @@
|
||||
let fetch = require("node-fetch");
|
||||
let cheerio = require("cheerio");
|
||||
const areThereAnyNewItems = require("./arethereanynewitems");
|
||||
|
||||
async function scrapTheItems(url, controlDate, noNewItems = false) {
|
||||
let items = [];
|
||||
let response = await fetch(url);
|
||||
const body = await response.text();
|
||||
const $ = cheerio.load(body);
|
||||
$("#rezultatipretrage")
|
||||
.find(".listitem")
|
||||
.each(async (index, elem) => {
|
||||
if (noNewItems) return;
|
||||
const itemDate = $(elem)
|
||||
.find(".cijena > .datum > div")
|
||||
.first()
|
||||
.attr("data-cijelidatum");
|
||||
|
||||
if (controlDate && !areThereAnyNewItems(itemDate, controlDate)) {
|
||||
noNewItems = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const id = $(elem)
|
||||
.find("a")
|
||||
.first()
|
||||
.attr("href");
|
||||
const cijena = $(elem)
|
||||
.find(".cijena > .datum > span")
|
||||
.first()
|
||||
.text();
|
||||
const image = $(elem)
|
||||
.find("a > .slika > img")
|
||||
.first()
|
||||
.attr("src");
|
||||
|
||||
items.push({ url: id, price: cijena, image, date: itemDate });
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
module.exports = scrapTheItems;
|
||||
Reference in New Issue
Block a user