Refactor (#4)

* refactor BoxNovel

* refactor BoxNovel

* refactor BoxNovel

* refactor FastNovel

* remove comments

* try puppeteer for FastNovel

* refactor MTLNovel

* comment out MTLNovel

* refactor MTLNovel

* revert FastNovel and MTLNovel

* possible fix for vercel application error

* clear git cache

* clear git cache

* rename boxnovel in index.js

* refactor NovelFull

* refactor Novelhall

* refactor rest of the sources

* fix WuxiaWorld
This commit is contained in:
Rajarshee Chatterjee
2021-05-03 12:31:40 +05:30
committed by GitHub
parent 70e23ea459
commit 4b33834cfb
27 changed files with 1699 additions and 1656 deletions
+14
View File
@@ -0,0 +1,14 @@
const express = require("express");
const wuxiaWorldScraper = require("./WuxiaWorldScraper");
const router = express.Router();
router.get("/novels/", wuxiaWorldScraper.novelsScraper);
router.get("/novel/:novelUrl", wuxiaWorldScraper.novelScraper);
router.get("/novel/:novelUrl/:chapterUrl", wuxiaWorldScraper.chapterScraper);
router.get("/search/", wuxiaWorldScraper.searchScraper);
module.exports = router;
+180
View File
@@ -0,0 +1,180 @@
const cheerio = require("cheerio");
const fetch = require("node-fetch");
const baseUrl = "https://www.wuxiaworld.com/";
const novelsScraper = async (req, res) => {
const url = `${baseUrl}api/novels`;
const result = await fetch(url);
const data = await result.json();
let novels = [];
data.items.map((novel) => {
let novelName = novel.name;
let novelCover = novel.coverUrl;
let novelUrl = novel.slug;
novelUrl = novelUrl + "/";
novels.push({
extensionId: 7,
novelUrl,
novelName,
novelCover,
});
});
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 = {};
novel.extensionId = 7;
novel.sourceName = "WuxiaWorld";
novel.sourceUrl = url;
novel.novelUrl = `${novelUrl}/`;
novel.novelName = $("h2").text();
novel.novelCover = $("img.img-thumbnail").attr("src");
novel.novelSummary = $(".fr-view > p").text();
novel["Author(s)"] = $("div > dt")
.filter(function () {
return $(this).text().trim() === "Author:";
})
.next()
.text();
let genres = [];
$(".genres")
.find("div")
.each(function (res) {
genres.push($(this).find("a").text());
});
novel["Genre(s)"] = genres.join(",");
novel["Artist(s)"] = null;
novel.Status = null;
let novelChapters = [];
$(".chapter-item").each(function (result) {
let chapterName = $(this).text();
chapterName = chapterName.replace(/[\t\n]/g, "");
const releaseDate = null;
let chapterUrl = $(this).find("a").attr("href");
chapterUrl = chapterUrl.replace(`/novel/${novelUrl}/`, "");
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;
let url = `${baseUrl}novel/${novelUrl}/${chapterUrl}`;
const result = await fetch(url);
const body = await result.text();
$ = cheerio.load(body);
let chapterName = $("#sidebar-toggler-container").next().text();
chapterName = chapterName.replace(/[\t\n]/g, "");
const chapterText = $("#chapter-content").text();
let nextChapter = null;
if ($("li.next > a").attr("href") !== "#") {
nextChapter = $("li.next > a")
.attr("href")
.replace(`/novel/${novelUrl}`, "");
}
let prevChapter = null;
if ($("li.prev > a").attr("href") !== "#") {
prevChapter = $("li.prev > a")
.attr("href")
.replace(`/novel/${novelUrl}`, "");
}
const chapter = {
extensionId: 7,
novelUrl,
chapterUrl,
chapterName,
chapterText,
nextChapter,
prevChapter,
};
res.json(chapter);
};
const searchScraper = async (req, res) => {
const searchTerm = req.query.s;
const searchUrl = `https://www.wuxiaworld.com/api/novels/search?query=`;
const url = `${searchUrl}${searchTerm}`;
const result = await fetch(url);
const data = await result.json();
let novels = [];
data.items.map((novel) => {
let novelName = novel.name;
let novelCover = novel.coverUrl;
let novelUrl = novel.slug;
novelUrl = novelUrl + "/";
novels.push({
extensionId: 7,
novelUrl,
novelName,
novelCover,
});
});
res.json(novels);
};
module.exports = wuxiaWorldScraper = {
novelsScraper,
novelScraper,
chapterScraper,
searchScraper,
};
-185
View File
@@ -1,185 +0,0 @@
const express = require("express");
const cheerio = require("cheerio");
const request = require("request");
const baseUrl = "https://www.wuxiaworld.com/";
const router = express.Router();
router.get("/novels/", async (req, res) => {
let url = `https://www.wuxiaworld.com/api/novels`;
request(url, (err, response, body) => {
if (err) throw err;
let novels = [];
let data = JSON.parse(body);
data = data.items;
data.map((novel) => {
let novelName = novel.name;
let novelCover = novel.coverUrl;
let novelUrl = novel.slug;
novelUrl = novelUrl + "/";
novels.push({
extensionId: 7,
novelUrl,
novelName,
novelCover,
});
});
res.json(novels);
});
});
// Novel
router.get("/novel/:novelUrl", async (req, res) => {
let novelUrl = req.params.novelUrl;
let url = `${baseUrl}novel/${novelUrl}/`;
request(url, (err, response, body) => {
if (err) throw err;
const $ = cheerio.load(body);
let novel = {};
novel.extensionId = 7;
novel.sourceName = "WuxiaWorld";
novel.sourceUrl = url;
novel.novelUrl = `${novelUrl}/`;
novel.novelName = $("h2").text();
novel.novelCover = $("img.img-thumbnail").attr("src");
novel.novelSummary = $(".fr-view > p").text();
novel["Author(s)"] = $("div > dt")
.filter(function () {
return $(this).text().trim() === "Author:";
})
.next()
.text();
let genres = [];
$(".genres")
.find("div")
.each(function (res) {
genres.push($(this).find("a").text());
});
novel["Genre(s)"] = genres.join(",");
novel["Artist(s)"] = null;
novel.Status = null;
let novelChapters = [];
$(".chapter-item").each(function (result) {
let chapterName = $(this).text();
chapterName = chapterName.replace(/[\t\n]/g, "");
let releaseDate = null;
let chapterUrl = $(this).find("a").attr("href");
chapterUrl = chapterUrl.replace(`/novel/${novelUrl}/`, "");
novelChapters.push({
chapterName,
releaseDate,
chapterUrl,
});
});
novel.novelChapters = novelChapters;
res.json(novel);
});
});
router.get("/novel/:novelUrl/:chapterUrl", async (req, res) => {
let url = `${baseUrl}novel/${req.params.novelUrl}/${req.params.chapterUrl}`;
const novelUrl = `${req.params.novelUrl}/`;
const chapterUrl = `${req.params.chapterUrl}/`;
request(url, (err, response, body) => {
if (err) throw err;
const $ = cheerio.load(body);
let chapterName = $("#sidebar-toggler-container").next().text();
chapterName = chapterName.replace(/[\t\n]/g, "");
let chapterText = $("#chapter-content").text();
let nextChapter = null;
if ($("li.next > a").attr("href") !== "#") {
nextChapter = $("li.next > a")
.attr("href")
.replace(`/novel/${novelUrl}`, "");
}
let prevChapter = null;
if ($("li.prev > a").attr("href") !== "#") {
prevChapter = $("li.prev > a")
.attr("href")
.replace(`/novel/${novelUrl}`, "");
}
chapter = {
extensionId: 7,
novelUrl,
chapterUrl,
chapterName,
chapterText,
nextChapter,
prevChapter,
};
res.json(chapter);
});
});
router.get("/search/", (req, res) => {
let searchTerm = req.query.s;
searchTerm = encodeURI(searchTerm);
const searchUrl = `https://www.wuxiaworld.com/api/novels/search?query=`;
const url = `${searchUrl}${searchTerm}`;
request(url, (err, response, body) => {
if (err) throw err;
$ = cheerio.load(body);
let novels = [];
let data = JSON.parse(body);
data = data.items;
data.map((novel) => {
let novelName = novel.name;
let novelCover = novel.coverUrl;
let novelUrl = novel.slug;
novelUrl = novelUrl + "/";
novels.push({
extensionId: 7,
novelUrl,
novelName,
novelCover,
});
});
res.json(novels);
});
});
module.exports = router;