Skip to content

Commit

Permalink
add: top-by-fans endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
azharimm committed Sep 5, 2021
1 parent c19e1b6 commit c71303e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/controllers/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ exports.index = (req, res) => {
endpoint: '/v2/top-by-interest',
example: fullUrl+'/top-by-interest'
},
top_by_fans: {
endpoint: '/v2/top-by-fans',
example: fullUrl+'/top-by-fans'
},
});
}
45 changes: 44 additions & 1 deletion src/controllers/v2/miscController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,50 @@ exports.topInterest = async (req, res) => {
phones.push({
phone_name,
slug,
hits,
hits: parseInt(hits.replace(/,/g, '')),
detail:
req.protocol +
"://" +
req.get("host") +
"/v2/" +
slug,
});
}
});

return json(res, {
title,
phones,
});
} catch (error) {
return errorJson(res, error);
}
};

exports.topFans = 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("fans")')
.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 favorites = $(el).find("td").eq(1).text();
phones.push({
phone_name,
slug,
favorites: parseInt(favorites.replace(/,/g, '')),
detail:
req.protocol +
"://" +
Expand Down
1 change: 1 addition & 0 deletions src/routes/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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("/top-by-fans", miscController.topFans);
router.get("/:slug", specController.index);

module.exports = router;

0 comments on commit c71303e

Please sign in to comment.