From c19e1b6f3ff3de37a389ba1cec1cf236553f80d4 Mon Sep 17 00:00:00 2001 From: azharimm Date: Sun, 5 Sep 2021 07:42:58 +0700 Subject: [PATCH] add: top-by-interest endpoint --- src/controllers/v2/index.js | 4 +++ src/controllers/v2/miscController.js | 43 ++++++++++++++++++++++++++++ src/routes/v2/index.js | 1 + 3 files changed, 48 insertions(+) diff --git a/src/controllers/v2/index.js b/src/controllers/v2/index.js index 54d846d..04dad9d 100644 --- a/src/controllers/v2/index.js +++ b/src/controllers/v2/index.js @@ -26,5 +26,9 @@ exports.index = (req, res) => { endpoint: '/v2/latest', example: fullUrl+'/latest' }, + top_by_interest: { + endpoint: '/v2/top-by-interest', + example: fullUrl+'/top-by-interest' + }, }); } diff --git a/src/controllers/v2/miscController.js b/src/controllers/v2/miscController.js index db15718..884740e 100644 --- a/src/controllers/v2/miscController.js +++ b/src/controllers/v2/miscController.js @@ -32,3 +32,46 @@ exports.index = async (req, res) => { return errorJson(res, error); } }; + +exports.topInterest = async (req, res) => { + try { + const url = `${process.env.BASE_URL}`; + const htmlResult = await request.get(url); + const $ = await cheerio.load(htmlResult); + const title = "Top By Daily Interest"; + const phones = []; + $('h4:contains("interest")') + .next() + .find("tbody") + .find("tr") + .each((index, el) => { + const phone_name = $(el).find("th").text(); + if (phone_name) { + const slug = $(el) + .find("th") + .find("a") + .attr("href") + .replace(".php", ""); + const hits = $(el).find("td").eq(1).text(); + phones.push({ + phone_name, + slug, + hits, + detail: + req.protocol + + "://" + + req.get("host") + + "/v2/" + + slug, + }); + } + }); + + return json(res, { + title, + phones, + }); + } catch (error) { + return errorJson(res, error); + } +}; diff --git a/src/routes/v2/index.js b/src/routes/v2/index.js index f8f46f9..6785ee2 100644 --- a/src/routes/v2/index.js +++ b/src/routes/v2/index.js @@ -13,6 +13,7 @@ router.get("/brands", brandController.index); router.get("/brands/:slug", brandController.show); router.get("/search", searchController.index); router.get("/latest", miscController.index); +router.get("/top-by-interest", miscController.topInterest); router.get("/:slug", specController.index); module.exports = router;