Skip to content

Commit

Permalink
chore: add date to api results res
Browse files Browse the repository at this point in the history
  • Loading branch information
TriptoAfsin committed Aug 18, 2024
1 parent 06fb7f1 commit 4530798
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/controllers/appController/appController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require("dotenv").config();
// let Feed = require('rss-to-json');

const axios = require('axios');
const scrapeResults = require('../../utils/scrapping/scrapeResults')


// analytics
Expand Down Expand Up @@ -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) => {

Expand Down Expand Up @@ -5901,6 +5928,9 @@ let labs = (req, res) => {

module.exports = {
intro: appIntro,

//results
results: appResults,

labs: labs,

Expand Down
2 changes: 2 additions & 0 deletions src/routes/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/utils/scrapping/scrapeResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});
}

Expand Down

0 comments on commit 4530798

Please sign in to comment.