Add sources: royalroad & pawread (#823)

* royalroad & pawread take 2

* remove console.log
This commit is contained in:
K1ngfish3r
2024-03-09 17:39:43 +08:00
committed by GitHub
parent 4842366870
commit 05e80588a0
6 changed files with 391 additions and 2 deletions
-1
View File
@@ -68,7 +68,6 @@ class EarlyNovelPlugin implements Plugin.PagePlugin {
const body = await fetchApi(link).then((res) => res.text());
const loadedCheerio = parseHTML(body);
console.log(body);
return this.parseNovels(loadedCheerio);
}
+1 -1
View File
@@ -85,7 +85,7 @@ class NovelBuddy implements Plugin.PluginBase {
const chapterListUrl = `${this.site}api/manga/${id}/chapters?source=detail`;
const data = await fetchApi(chapterListUrl);
const chapterlist = await data.text();
console.log(chapterlist);
loadedCheerio = parseHTML(chapterlist);
loadedCheerio("li").each((i,el) => {
+223
View File
@@ -0,0 +1,223 @@
import { CheerioAPI, load, load as parseHTML } from "cheerio";
import { fetchApi, fetchFile } from "@libs/fetch";
import { Plugin } from "@typings/plugin";
import { Filters, FilterTypes } from "@libs/filterInputs";
class PawRead implements Plugin.PluginBase {
id = "pawread";
name = "PawRead";
version = "1.0.0";
icon = "src/en/pawread/icon.png";
site = "https://www.pawread.com/";
parseNovels(loadedCheerio:CheerioAPI){
const novels: Plugin.NovelItem[] = [];
loadedCheerio(".list-comic").each((idx, ele) => {
const novelName = loadedCheerio(ele).find('h3').text();
const novelCover = loadedCheerio(ele).find('img').attr('src');
const novelUrl = loadedCheerio(ele).find('h3 a').attr('href')?.slice(1);
if (!novelUrl) return;
const novel = {
name: novelName,
cover: novelCover,
path: novelUrl,
};
novels.push(novel);
});
return novels;
}
async popularNovels(
page: number,
{ filters }: Plugin.PopularNovelsOptions<typeof this.filters>
): Promise<Plugin.NovelItem[]> {
let link = `${this.site}list/`;
const filterValues = [
filters.status.value,
filters.lang.value,
filters.genre.value
];
link += filterValues.filter(value => value !== '').join('-');
if (filterValues.some(value => value !== ''))
link += '/';
if (filters.order.value)
link += '-';
link += filters.sort.value;
link += `/?page=${page}`;
const body = await fetchApi(link).then((r) => r.text());
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> {
const result = await fetchApi(this.site + novelPath);
const body = await result.text();
const loadedCheerio = parseHTML(body);
const novel: Plugin.SourceNovel = {
path: novelPath,
name: loadedCheerio(".col-md-9 h1").text() || "Untitled",
summary: loadedCheerio("#simple-des").text().trim(),
status: loadedCheerio('h4 span').text(),
chapters: [],
};
novel.cover = loadedCheerio('#tab1_board .col-md-3 div')
.attr('style')
?.match(/url\((.*?)\)/i)![1];
novel.author = loadedCheerio('.glyphicon-user:first')
.next()
.text()
.replace("Author: ", "");
novel.genres = loadedCheerio('a.btn-default')
.map((i, el) => loadedCheerio(el).text())
.toArray()
.join(",");
const chapter: Plugin.ChapterItem[] = [];
loadedCheerio(".col-md-10").each((idx, ele) => {
const releaseDate = loadedCheerio(ele).find('small').text().trim();
if (releaseDate === "Advanced Chapter") return;
const chapterName = loadedCheerio(ele).find("span").text().trim();
const chapterUrl = loadedCheerio(ele).parent().attr("onclick")?.match(/\d+/);
if (!chapterUrl) return;
chapter.push({
name: chapterName,
path: `${novelPath}${chapterUrl[0]}.html`,
releaseTime: new Date(releaseDate).toISOString(),
});
});
novel.chapters = chapter;
return novel;
}
async parseChapter(chapterPath: string): Promise<string> {
const result = await fetchApi(this.site + chapterPath);
const body = await result.text();
const loadedCheerio = parseHTML(body);
const chapterText = loadedCheerio("#chapter_item").html() || '';
return chapterText;
}
async searchNovels(searchTerm: string, page: number): Promise<Plugin.NovelItem[]> {
const searchUrl = `${this.site}search/?keywords=${searchTerm}&page=${page}`;
const result = await fetchApi(searchUrl);
const body = await result.text();
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
async fetchImage(url: string): Promise<string | undefined> {
return fetchFile(url);
}
filters = {
status: {
value: "",
label: "Status",
options: [
{ label: "All", value: "" },
{ label: "Completed", value: "wanjie" },
{ label: "Ongoing", value: "lianzai" },
{ label: "Hiatus", value: "hiatus" },
],
type: FilterTypes.Picker,
},
lang: {
value: "",
label: "Languages",
options: [
{ label: "All", value: "" },
{ label: "Chinese", value: "chinese" },
{ label: "Korean", value: "korean" },
{ label: "Japanese", value: "japanese" },
],
type: FilterTypes.Picker,
},
genre: {
value: "",
label: "Genres",
options: [
{ label: "All", value: "" },
{ label: "Fantasy", value: "Fantasy" },
{ label: "Action", value: "Action" },
{ label: "Xuanhuan", value: "Xuanhuan" },
{ label: "Romance", value: "Romance" },
{ label: "Comedy", value: "Comedy" },
{ label: "Mystery", value: "Mystery" },
{ label: "Mature", value: "Mature" },
{ label: "Harem", value: "Harem" },
{ label: "Wuxia", value: "Wuxia" },
{ label: "Xianxia", value: "Xianxia" },
{ label: "Tragedy", value: "Tragedy" },
{ label: "Sci-fi", value: "Scifi" },
{ label: "Historical", value: "Historical" },
{ label: "Ecchi", value: "Ecchi" },
{ label: "Adventure", value: "Adventure" },
{ label: "Adult", value: "Adult" },
{ label: "Supernatural", value: "Supernatural" },
{ label: "Psychological", value: "Psychological" },
{ label: "Drama", value: "Drama" },
{ label: "Horror", value: "Horror" },
{ label: "Josei", value: "Josei" },
{ label: "Mecha", value: "Mecha" },
{ label: "Seinen", value: "Seinen" },
{ label: "Shoujo", value: "Shoujo" },
{ label: "Shounen", value: "Shounen" },
{ label: "Smut", value: "Smut" },
{ label: "Yaoi", value: "Yaoi" },
{ label: "Yuri", value: "Yuri" },
{ label: "Martial Arts", value: "MartialArts" },
{ label: "School Life", value: "SchoolLife" },
{ label: "Shoujo Ai", value: "ShoujoAi" },
{ label: "Shounen Ai", value: "ShounenAi" },
{ label: "Slice of Life", value: "SliceofLife" },
{ label: "Gender Bender", value: "GenderBender" },
{ label: "Sports", value: "Sports" },
{ label: "Urban", value: "Urban" },
{ label: "Adventurer", value: "Adventurer" }
],
type: FilterTypes.Picker,
},
sort: {
value: "click",
label: "Sort By",
options: [
{ label: "Time Updated", value: "update" },
{ label: "Time Posted", value: "post" },
{ label: "Clicks", value: "click" },
],
type: FilterTypes.Picker,
},
order: {
value: false,
label: "Order ↑ ↓",
type: FilterTypes.Switch,
},
} satisfies Filters;
}
export default new PawRead();
+167
View File
@@ -0,0 +1,167 @@
import { CheerioAPI, load, load as parseHTML } from "cheerio";
import { fetchApi, fetchFile } from "@libs/fetch";
import { Plugin } from "@typings/plugin";
import { Filters, FilterTypes } from "@libs/filterInputs";
class RoyalRoad implements Plugin.PluginBase {
id = "royalroad";
name = "Royal Road";
version = "1.0.0";
icon = "src/en/royalroad/icon.png";
site = "https://www.royalroad.com/";
parseNovels(loadedCheerio:CheerioAPI){
const novels: Plugin.NovelItem[] = [];
loadedCheerio(".fiction-list-item").each((idx, ele) => {
const novelName = loadedCheerio(ele).find('a.bold').text();
const novelCover = loadedCheerio(ele).find('img').attr('src');
const novelUrl = loadedCheerio(ele).find('a.bold').attr('href')?.slice(1);
if (!novelUrl) return;
const novel = {
name: novelName,
cover: novelCover,
path: novelUrl,
};
novels.push(novel);
});
return novels;
}
async popularNovels(
page: number,
{ filters }: Plugin.PopularNovelsOptions<typeof this.filters>
): Promise<Plugin.NovelItem[]> {
let link = `${this.site}fictions/`;
link += filters.order.value;
link += `?page=${page}`;
if (filters.genre.value !== '')
link += `&genre=${filters.genre.value}`;
const body = await fetchApi(link).then((r) => r.text());
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> {
const result = await fetchApi(this.site + novelPath);
const body = await result.text();
const loadedCheerio = parseHTML(body);
const novel: Plugin.SourceNovel = {
path: novelPath,
name: loadedCheerio("h1").text() || "Untitled",
cover: loadedCheerio('.cover-art-container img').attr('src'),
author: loadedCheerio('h4 a').text(),
summary: loadedCheerio(".description").text().trim(),
status: loadedCheerio('span.label-sm:last').text().trim(),
chapters: [],
};
novel.genres = loadedCheerio('span.tags a')
.map((i, el) => loadedCheerio(el).text())
.toArray()
.join(",");
const chapterlisting = JSON.parse(
loadedCheerio('script:contains("window.chapters")')
.html()
?.match(/window.chapters = \[(.*?)\]/)![0]
?.replace("window.chapters = ", "") || ""
);
interface ChapterEntry {
title: string;
date: string;
url: string;
}
const chapter : Plugin.ChapterItem[] = chapterlisting.map(
(chapter: ChapterEntry) => ({
name: chapter.title,
path: chapter.url.slice(1),
releaseTime: chapter.date,
}))
novel.chapters = chapter;
return novel;
}
async parseChapter(chapterPath: string): Promise<string> {
const result = await fetchApi(this.site + chapterPath);
const body = await result.text();
const loadedCheerio = parseHTML(body);
const chapterText = loadedCheerio(".chapter-content").html() || '';
return chapterText;
}
async searchNovels(searchTerm: string, page: number): Promise<Plugin.NovelItem[]> {
const searchUrl = `${this.site}fictions/search?page=${page}&title=${searchTerm}`;
const result = await fetchApi(searchUrl);
const body = await result.text();
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
async fetchImage(url: string): Promise<string | undefined> {
return fetchFile(url);
}
filters = {
order: {
value: "weekly-popular",
label: "Order By",
options: [
{ label: "Best Rated", value: "best-rated" },
{ label: "Trending", value: "trending" },
{ label: "Ongoing Fictions", value: "active-popular" },
{ label: "Complete", value: "complete" },
{ label: "Popular this week", value: "weekly-popular" },
{ label: "Latest Updates", value: "latest-updates" },
{ label: "Newest Fictions", value: "new" },
{ label: "Rising Stars", value: "rising-stars" },
{ label: "Writathon", value: "writathon" },
],
type: FilterTypes.Picker,
},
genre: {
value: "",
label: "Genres",
options: [
{ label: "ALL", value: "" },
{ label: "Action", value: "action" },
{ label: "Adventure", value: "adventure" },
{ label: "Comedy", value: "comedy" },
{ label: "Contemporary", value: "contemporary" },
{ label: "Drama", value: "drama" },
{ label: "Fantasy", value: "fantasy" },
{ label: "Historical", value: "historical" },
{ label: "Horror", value: "horror" },
{ label: "Mystery", value: "mystery" },
{ label: "Psychological", value: "psychological" },
{ label: "Romance", value: "romance" },
{ label: "Satire", value: "satire" },
{ label: "Sci-fi", value: "sci_fi" },
{ label: "Short Story", value: "one_shot" },
{ label: "Tragedy", value: "tragedy" }
],
type: FilterTypes.Picker,
}
} satisfies Filters;
}
export default new RoyalRoad();