From 15e726fb1d1484af91f454c69d0da923a04d331e Mon Sep 17 00:00:00 2001 From: Rajarshee Chatterjee Date: Wed, 23 Dec 2020 19:16:15 +0530 Subject: [PATCH] Add search and chapter route --- api/chapter.js | 56 +++++++++++++++++++++++++++++++++++ api/latest.js | 2 -- api/novel.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ api/search.js | 62 ++++++++++++++++++++++++++++++++++++++ server.js | 3 ++ 5 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 api/chapter.js create mode 100644 api/novel.js create mode 100644 api/search.js diff --git a/api/chapter.js b/api/chapter.js new file mode 100644 index 0000000..7b5b072 --- /dev/null +++ b/api/chapter.js @@ -0,0 +1,56 @@ +const express = require("express"); +const cheerio = require("cheerio"); +const request = require("request"); + +const router = express.Router(); + +const baseUrl = "https://boxnovel.com/"; + +router.get("/novel/:novelUrl/:chapterUrl", (req, res) => { + url = + baseUrl + "novel/" + req.params.novelUrl + "/" + req.params.chapterUrl; + + request(url, (err, response, body) => { + if (err) throw err; + + $ = cheerio.load(body); + + chapterNameLower = req.params.chapterUrl.replace("-", " "); + + chapterName = + chapterNameLower.charAt(0).toUpperCase() + + chapterNameLower.slice(1); + + chapterText = $(".reading-content").text(); + + let nextChapter = null; + + if ($(".nav-next").length) { + nextChapter = $(".nav-next") + .find("a") + .attr("href") + .replace(baseUrl, ""); + } + + let prevChapter = null; + + if ($(".nav-previous").length) { + prevChapter = $(".nav-previous") + .find("a") + .attr("href") + .replace(baseUrl, ""); + } + + chapter = { + chapterUrl: url, + chapterName, + chapterText, + nextChapter, + prevChapter, + }; + + res.json(chapter); + }); +}); + +module.exports = router; diff --git a/api/latest.js b/api/latest.js index 34ce4fd..f464c50 100644 --- a/api/latest.js +++ b/api/latest.js @@ -15,8 +15,6 @@ router.get("/latest", (req, res) => { $ = cheerio.load(body); $(".page-item-detail").each(function (result) { - let novelName = null; - novelName = $(this).find("h5 > a").text(); novelCover = $(this).find("img").attr("src"); novelUrl = $(this) diff --git a/api/novel.js b/api/novel.js new file mode 100644 index 0000000..e6c4727 --- /dev/null +++ b/api/novel.js @@ -0,0 +1,80 @@ +const express = require("express"); +const cheerio = require("cheerio"); +const request = require("request"); + +const router = express.Router(); + +const baseUrl = "https://boxnovel.com/"; + +router.get("/novel/:novelUrl", (req, res) => { + url = baseUrl + "novel/" + req.params.novelUrl; + request(url, (err, response, body) => { + if (err) throw err; + + $ = cheerio.load(body); + + $(".post-title > h3 > span").remove(); + + novelName = $(".post-title > h3") + .text() + .replace(/[\t\n]/g, "") + .trim(); + + // authorName = $(".author-content") + // .text() + // .replace(/[\t\n]/g, "") + // .trim(); + + // artistName = $(".artist-content") + // .text() + // .replace(/[\t\n]/g, "") + // .trim(); + + let novelDetails = {}; + + $(".post-content_item").each(function (result) { + detailName = $(this) + .find(".summary-heading > h5") + .text() + .replace(/[\t\n]/g, "") + .trim(); + detail = $(this) + .find(".summary-content") + .text() + .replace(/[\t\n]/g, "") + .trim(); + + novelDetails[detailName] = detail; + }); + + let novelChapters = []; + + $(".wp-manga-chapter").each(function (result) { + chapterName = $(this) + .find("a") + .text() + .replace(/[\t\n]/g, "") + .trim(); + + releaseDate = $(this) + .find("span") + .text() + .replace(/[\t\n]/g, "") + .trim(); + + chapterUrl = $(this).find("a").attr("href"); + + novelChapters.push({ chapterName, releaseDate, chapterUrl }); + }); + + novel = { + novelName, + novelDetails, + novelChapters, + }; + + res.json(novel); + }); +}); + +module.exports = router; diff --git a/api/search.js b/api/search.js new file mode 100644 index 0000000..395263c --- /dev/null +++ b/api/search.js @@ -0,0 +1,62 @@ +const express = require("express"); +const cheerio = require("cheerio"); +const request = require("request"); + +const router = express.Router(); + +const baseUrl = "https://boxnovel.com/"; + +router.get("/search/", (req, res) => { + searchTerm = req.query.s; + orderBy = req.query.o; + + url = + baseUrl + + "?s=" + + searchTerm + + "&post_type=wp-manga&m_orderby=" + + orderBy; + request(url, (err, response, body) => { + if (err) throw err; + + $ = cheerio.load(body); + + let novels = []; + + $(".c-tabs-item__content").each(function (result) { + novelName = $(this).find("h4 > a").text(); + + let novelDetails = {}; + + $(this) + .find(".post-content_item") + .each(function (result) { + detailName = $(this) + .find(".summary-heading > h5") + .text() + .replace(/[\t\n]/g, "") + .trim(); + detail = $(this) + .find(".summary-content") + .text() + .replace(/[\t\n]/g, "") + .trim(); + + novelDetails[detailName] = detail; + }); + + novelCover = $(this).find("img").attr("src"); + + novelUrl = $(this) + .find("h4 > a") + .attr("href") + .replace(baseUrl + "novel/", ""); + + novels.push({ novelName, novelCover, novelUrl, novelDetails }); + }); + + res.json(novels); + }); +}); + +module.exports = router; diff --git a/server.js b/server.js index 3aaf089..d3b4231 100644 --- a/server.js +++ b/server.js @@ -17,6 +17,9 @@ app.get("/", (req, res) => ); app.use("/api/", require("./api/latest")); +app.use("/api/", require("./api/novel")); +app.use("/api/", require("./api/search")); +app.use("/api/", require("./api/chapter")); const PORT = process.env.PORT || 5000;