connecting to remote mysql db, saving market alerts
This commit is contained in:
@@ -3,13 +3,19 @@ let cheerio = require("cheerio");
|
||||
let express = require("express");
|
||||
const path = require("path");
|
||||
const bodyParser = require("body-parser");
|
||||
let fs = require("fs");
|
||||
const MarketAlert = require("./MarketAlert");
|
||||
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/:url", async (req, res) => {
|
||||
let url = "https://www.olx.ba/pretraga?" + req.params.url;
|
||||
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();
|
||||
@@ -18,7 +24,13 @@ app.get("/api/:url", async (req, res) => {
|
||||
|
||||
$("#rezultatipretrage")
|
||||
.find(".listitem")
|
||||
.each(async (i, elem) => {
|
||||
.each(async (index, elem) => {
|
||||
if (index == 0) {
|
||||
lastItemDate = $(elem)
|
||||
.find(".cijena > .datum > div")
|
||||
.first()
|
||||
.attr("data-cijelidatum");
|
||||
}
|
||||
const id = $(elem)
|
||||
.find("a")
|
||||
.first()
|
||||
@@ -33,7 +45,32 @@ app.get("/api/:url", async (req, res) => {
|
||||
.attr("src");
|
||||
appts.push({ url: id, price: cijena, image });
|
||||
});
|
||||
res.json(appts);
|
||||
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));
|
||||
res.json({
|
||||
last_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,
|
||||
last_date,
|
||||
email
|
||||
})
|
||||
);
|
||||
res.json({ message: "Market Alert Created!" });
|
||||
});
|
||||
|
||||
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|
||||
|
||||
Reference in New Issue
Block a user