sending notification when new items arrive with send grid

This commit is contained in:
egradanin
2019-01-19 19:20:21 +01:00
parent 18032c6ca6
commit ba2fcf0b8c
8 changed files with 540 additions and 44 deletions

View File

@@ -1,68 +1,52 @@
let fetch = require("node-fetch");
let cheerio = require("cheerio");
let express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const MarketAlert = require("./MarketAlert");
const sendNotification = require("./utils/sendnotification");
const scrapTheItems = require("./utils/scraptheitems");
const sequelize = require("./db.js");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const port = process.env.PORT || 5000;
app.get("/api/sendnotifications", async function(req, res) {
let marketAlerts = await MarketAlert.findAll();
let lastDateUpdate = await Promise.all(
marketAlerts
.map(marketAlert => {
const { id, email, olx_url, last_date } = marketAlert.dataValues;
return { id, email, olx_url, last_date };
})
.map(sendNotification)
);
if (lastDateUpdate.some(dateUpdate => !dateUpdate)) return;
lastDateUpdate.length &&
lastDateUpdate.forEach(dateUpdate =>
MarketAlert.update(
{ last_date: dateUpdate.date },
{ where: { id: dateUpdate.id } }
)
);
});
app.get("/api/:url", async (req, res) => {
let url =
"https://www.olx.ba/pretraga?" +
req.params.url +
"&sort_order=desc&sort_po=datum";
let appts = [];
response = await fetch(url);
const body = await response.text();
const $ = cheerio.load(body);
$("#rezultatipretrage")
.find(".listitem")
.each(async (index, elem) => {
if (index == 0) {
lastItemDate = $(elem)
.find(".cijena > .datum > div")
.first()
.attr("data-cijelidatum");
}
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");
appts.push({ url: id, price: cijena, image });
});
const [dan, mjesec, godina] = lastItemDate
.split(". u ")[0]
.split(".")
.map(el => Number(el));
const [sati, minute] = lastItemDate
.split(". u ")[1]
.split(":")
.map(el => Number(el));
last_date = String(new Date(godina, mjesec, dan, sati, minute));
let appts = scrapTheItems(url);
res.json({
last_date,
last_date: appts[0] && appts[0].date,
items: appts
});
});
app.post("/api/marketalerts", function(req, res) {
const { email, last_date, olx_url } = req.body;
sequelize.sync().then(() =>
MarketAlert.create({
olx_url,