| const baths = req.body.baths; | const baths = req.body.baths; | ||||
| // query builder | // query builder | ||||
| var query = `https://www.apartments.com`; | |||||
| if (type) { | |||||
| query += `/${type}`; | |||||
| } | |||||
| if (location) { | |||||
| var locationQuery = location.replace(", ", "-").replace(" ", "-").toLowerCase(); | |||||
| query += `/${locationQuery}`; | |||||
| } | |||||
| if (beds) { | |||||
| query += `/${beds}-bedrooms`; | |||||
| } | |||||
| if (baths) { | |||||
| query += `${beds ? '-' : '/'}${baths}-bathrooms`; | |||||
| } | |||||
| if (price) { | |||||
| if (beds) { | |||||
| query += `-over-${price}`; | |||||
| } else { | |||||
| query += `/over-${price}`; | |||||
| } | |||||
| } | |||||
| if (lifestyle) { | |||||
| query += `/${lifestyle}`; | |||||
| } | |||||
| var query = buildQuery(type, location, beds, baths, price, lifestyle); | |||||
| console.log(query); | console.log(query); | ||||
| } | } | ||||
| return res.json(); | return res.json(); | ||||
| }); | }); | ||||
| app.post("/scrapes/estimate", async (req, res) => { | |||||
| const location = req.body.location; | |||||
| const description = req.body.description; | |||||
| const price = req.body.price; | |||||
| const beds = req.body.beds; | |||||
| const type = req.body.type; | |||||
| const lifestyle = req.body.lifestyle; | |||||
| const baths = req.body.baths; | |||||
| var query = buildQuery(type, location, beds, baths, price, lifestyle); | |||||
| console.log(query); | |||||
| const filterPage = await axios(query); | |||||
| const html = filterPage.data; | |||||
| const $ = cheerio.load(html); | |||||
| var $pageRange = $(".pageRange"); | |||||
| var pagesCount = 0; | |||||
| var resultCount = 0; | |||||
| if (!$pageRange.length) { | |||||
| let propertyLinks = $('#placardContainer .property-link').map(function () { | |||||
| return $(this).attr('href'); | |||||
| }).get(); | |||||
| if (!propertyLinks.length) { | |||||
| console.error("No results"); | |||||
| return res.status(404).json(); | |||||
| } | |||||
| resultCount = propertyLinks.length; | |||||
| } else { | |||||
| pagesCount = $pageRange.text().slice($pageRange.text().lastIndexOf("of ") + 3); | |||||
| resultCount = pagesCount * 25; | |||||
| } | |||||
| const dt = new Date(); | |||||
| dt.setSeconds(dt.getSeconds() + resultCount); | |||||
| return res.json({ | |||||
| count: resultCount, | |||||
| pageCount: pagesCount, | |||||
| estimate: dt, | |||||
| }); | |||||
| }); | |||||
| app.patch("/scrapes/:id/execute", async (req, res) => { | app.patch("/scrapes/:id/execute", async (req, res) => { | ||||
| const id = req.params.id; | const id = req.params.id; | ||||
| console.log(`Example app listening at http://localhost:${port}`) | console.log(`Example app listening at http://localhost:${port}`) | ||||
| }); | }); | ||||
| function buildQuery(type, location, beds, baths, price, lifestyle) { | |||||
| var query = `https://www.apartments.com`; | |||||
| if (type) { | |||||
| query += `/${type}`; | |||||
| } | |||||
| if (location) { | |||||
| var locationQuery = location.replace(", ", "-").replace(" ", "-").toLowerCase(); | |||||
| query += `/${locationQuery}`; | |||||
| } | |||||
| if (beds) { | |||||
| query += `/${beds}-bedrooms`; | |||||
| } | |||||
| if (baths) { | |||||
| query += `${beds ? '-' : '/'}${baths}-bathrooms`; | |||||
| } | |||||
| if (price) { | |||||
| if (beds) { | |||||
| query += `-over-${price}`; | |||||
| } else { | |||||
| query += `/over-${price}`; | |||||
| } | |||||
| } | |||||
| if (lifestyle) { | |||||
| query += `/${lifestyle}`; | |||||
| } | |||||
| return query; | |||||
| } | |||||
| // Handles graceful stopping of jobs | // Handles graceful stopping of jobs | ||||
| function graceful() { | function graceful() { | ||||
| agenda.stop(function () { | agenda.stop(function () { |