chore: Add Icons

This commit is contained in:
Rajarshee Chatterjee
2021-09-11 11:12:10 +05:30
parent cb6f9642f1
commit c89460f430
5 changed files with 221 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
const express = require("express");
const WNMTLScraper = require("./WNMTLScraper");
const router = express.Router();
router.get("/novels/", WNMTLScraper.novelsScraper);
router.get("/novel/:novelUrl", WNMTLScraper.novelScraper);
router.get("/novel/:novelUrl/:chapterUrl", WNMTLScraper.chapterScraper);
router.get("/search/", WNMTLScraper.searchScraper);
module.exports = router;
+206
View File
@@ -0,0 +1,206 @@
const cheerio = require("cheerio");
const fetch = require("node-fetch");
const { htmlToText } = require("html-to-text");
const { parseHtml } = require("../../helper");
const baseUrl = "https://noveldeglace.com/";
const sourceName = "Novel De Glace";
const sourceId = 76;
const novelsScraper = async (req, res) => {
let url = baseUrl + "roman";
console.log(url);
const result = await fetch(url);
const body = await result.text();
$ = cheerio.load(body);
let novels = [];
$("article").each(function () {
const novelName = $(this).find("h2").text().trim();
const novelCover = $(this).find("img").attr("src");
const novelUrl = $(this).find("h2 > a").attr("href");
const novel = {
sourceId,
novelName,
novelCover,
novelUrl,
};
novels.push(novel);
});
res.json(novels);
};
const novelScraper = async (req, res) => {
const novelUrl = req.params.novelUrl;
const url = baseUrl + "novel/" + novelUrl;
const result = await fetch(url);
const body = await result.text();
$ = cheerio.load(body);
let novel = { sourceId, sourceName, url, novelUrl };
function getNovelName(y) {
return y
.replace("-", " ")
.replace(/(?:^|\s)\S/g, (a) => a.toUpperCase());
}
novelName = getNovelName(novelUrl);
novel.novelName = novelName;
novel.novelCover = $("#thumbnail > img").attr("src");
let novelSummary = $("div#Description").html();
novel.novelSummary = htmlToText(novelSummary);
novel["Genre(s)"] = $("div#Genre")
.text()
.replace(/Genre:|\s/g, "");
novel.Status = $("div#Status")
.text()
.replace(/Status:|\s/g, "");
novel["Author(s)"] = $("div#Publisher")
.text()
.replace(/Publisher:|\s/g, "");
// novel['Genre(s)'] = $("div#Genre").
let novelChapters = [];
let chapterUrlPrefix = $("tbody > tr")
.first()
.next()
.find("td > a")
.text()
.split(" ");
chapterUrlPrefix.pop();
chapterUrlPrefix = chapterUrlPrefix.join("-").toLowerCase();
const latestChapter = $("tbody > tr")
.first()
.next()
.find("td > a")
.text()
.replace(/^\D+/g, "");
for (let i = 1; i <= latestChapter; i++) {
const chapterName = "Chapter " + i;
const releaseDate = null;
const chapterUrl = chapterUrlPrefix + "-" + i + "/";
const chapter = {
chapterName,
releaseDate,
chapterUrl,
};
novelChapters.push(chapter);
}
novel.novelChapters = novelChapters;
res.json(novel);
};
const chapterScraper = async (req, res) => {
let novelUrl = req.params.novelUrl;
let chapterUrl = req.params.chapterUrl;
const url = `${baseUrl}mtl/${novelUrl}/${chapterUrl}`;
const result = await fetch(url);
const body = await result.text();
$ = cheerio.load(body);
let chapterName = $(".entry-title").text();
let chapterText = $(".entry-content").html();
chapterText = parseHtml(chapterText);
let nextChapter = null;
if ($(".nav-next").find("a").length) {
nextChapter = $(".nav-next")
.find("a")
.attr("href")
.replace(baseUrl + "mtl/" + novelUrl + "/", "");
}
let prevChapter = null;
if ($(".nav-previous").find("a").length) {
prevChapter = $(".nav-previous")
.find("a")
.attr("href")
.replace(baseUrl + "mtl/" + novelUrl + "/", "");
}
novelUrl = novelUrl + "/";
chapterUrl = chapterUrl + "/";
const chapter = {
sourceId,
novelUrl,
chapterUrl,
chapterName,
chapterText,
nextChapter,
prevChapter,
};
res.json(chapter);
};
const searchScraper = async (req, res) => {
const searchTerm = req.query.s;
const url = baseUrl + "?s=" + searchTerm + "&post_type=novel";
const result = await fetch(url);
const body = await result.text();
$ = cheerio.load(body);
let novels = [];
$("article.blog-article").each(function (result) {
const novelName = $(this).find("h5 > a").text();
const novelCover = $(this).find("img").attr("src");
let novelUrl = $(this).find("h5 > a").attr("href");
novelUrl = novelUrl.replace(baseUrl + "novel/", "");
const novel = {
sourceId,
novelName,
novelCover,
novelUrl,
};
novels.push(novel);
});
res.json(novels);
};
module.exports = WNMTLScraper = {
novelsScraper,
novelScraper,
chapterScraper,
searchScraper,
};