From 45307988ca1bf947a0b61619b644c6c0006a44f0 Mon Sep 17 00:00:00 2001 From: TriptoAfsin Date: Sun, 18 Aug 2024 16:59:47 +0600 Subject: [PATCH] chore: add date to api results res --- .../appController/appController.js | 30 +++++++++++++++++++ src/routes/web.js | 2 ++ src/utils/scrapping/scrapeResults.js | 10 +++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/controllers/appController/appController.js b/src/controllers/appController/appController.js index 71a029f..1ebd1e9 100644 --- a/src/controllers/appController/appController.js +++ b/src/controllers/appController/appController.js @@ -5,6 +5,7 @@ require("dotenv").config(); // let Feed = require('rss-to-json'); const axios = require('axios'); +const scrapeResults = require('../../utils/scrapping/scrapeResults') // analytics @@ -1141,6 +1142,32 @@ let appIntro = (req, res) => { return res.send(appIntro); }; +let appResults = (req, res) => { + console.log("🟠 App results called"); + + // Get the limit from query parameters, default to 5 if not provided + const limit = parseInt(req.query.limit) || 10; + + scrapeResults(limit).then(results => { + if (results && results.length > 0) { + const response = { + msg: `Here are the last ${results.length} results`, + data: results + }; + return res.send(response); + } else { + return res.status(404).send({ + msg: "No results found or error while getting results", + }); + } + }).catch(error => { + console.error("Error in appResults:", error); + return res.status(500).send({ + msg: "Internal server error while processing results", + }); + }); +}; + let notes = (req, res) => { @@ -5901,6 +5928,9 @@ let labs = (req, res) => { module.exports = { intro: appIntro, + + //results + results: appResults, labs: labs, diff --git a/src/routes/web.js b/src/routes/web.js index 531f136..2d223cb 100644 --- a/src/routes/web.js +++ b/src/routes/web.js @@ -29,6 +29,8 @@ let initWebRoutes = (app) => { //app routes router.get("/app", appController.intro); + router.get("/results", appController.results); + // router.get("/app/notices", appController.notice); router.get("/app/notes", appController.notes); diff --git a/src/utils/scrapping/scrapeResults.js b/src/utils/scrapping/scrapeResults.js index c7f9008..cd59edf 100644 --- a/src/utils/scrapping/scrapeResults.js +++ b/src/utils/scrapping/scrapeResults.js @@ -13,13 +13,17 @@ const scrapeResults = async (limit = 3) => { // Find the target elements and extract the data const results = []; - const elements = $('.large-9.columns h3 a'); + const elements = $('.large-9.columns h3'); for (let i = 0; i < elements.length && i < limit; i++) { const element = elements[i]; + const aTag = $(element).find('a'); + const dateElement = $(element).next('small').find('time'); + results.push({ - href: $(element).attr('href'), - content: $(element).text().trim() + href: aTag.attr('href'), + content: aTag.text().trim(), + date: dateElement.text().trim() }); }