refactor: Classes for Plugins (#631)
* set target to ES5 * refactor types * use .gitkeep * use dot to make dist/js dirs on top * add hako * add Hako * add madara * fix: url path js -> .js * fix: branch * freestyle for using UA, cookie * add parseNovels for Hako * update readme * fix: conflict types * add Kolnovel, remove node-fetch * add bestlightnovel * add plugins * WQMangaStreamScraper * PandaMTL * NobleMTL * LightNovelBrasil * WPMangaStream * add filters for popularNovels * fix undefined json * Linovelib (#26) * linovelib CN * icon for linovelib * fix undefined chapter * add check * fix: add artist * TuNovelaLigera ES + Madara Fix (#28) * app repo test * POST is now working * update madara template + run generator * template fix didnt get pushed commited idk why * template update POST * generated * fix: useNewChapterEndpoint update (#29) * useNewChapterEndpoint update * Update Selectors --------- Co-authored-by: K1ngfish3r <trulyihu@gmail.com>
This commit is contained in:
+149
-185
@@ -1,197 +1,161 @@
|
||||
import { load as parseHTML } from "cheerio";
|
||||
import { CheerioAPI, load as parseHTML } from "cheerio";
|
||||
import { fetchApi, fetchFile } from "@libs/fetch";
|
||||
import { Chapter, Novel, Plugin } from "@typings/plugin";
|
||||
// import dayjs from "dayjs";
|
||||
// import { FilterInputs } from "@libs/filterInputs";
|
||||
// import { NovelStatus } from "@libs/novelStatus";
|
||||
import { Plugin } from "@typings/plugin";
|
||||
import { isUrlAbsolute } from "@libs/isAbsoluteUrl";
|
||||
// import { parseMadaraDate } from "@libs/parseMadaraDate";
|
||||
import { Filter } from "@libs/filterInputs";
|
||||
class HakoPlugin implements Plugin.PluginBase {
|
||||
id: string;
|
||||
name: string;
|
||||
icon: string;
|
||||
site: string;
|
||||
version: string;
|
||||
userAgent: string;
|
||||
cookieString: string;
|
||||
constructor(){
|
||||
this.id = "ln.hako";
|
||||
this.name = "Hako";
|
||||
this.icon = "src/vi/hakolightnovel/icon.png";
|
||||
this.site = "https://ln.hako.vn";
|
||||
this.version = "1.0.0";
|
||||
this.userAgent = "";
|
||||
this.cookieString = "";
|
||||
}
|
||||
parseNovels(loadedCheerio: CheerioAPI){
|
||||
const novels: Plugin.NovelItem[] = [];
|
||||
loadedCheerio(".row > .thumb-item-flow").each((index, ele) => {
|
||||
let url = loadedCheerio(ele)
|
||||
.find("div.thumb_attr.series-title > a")
|
||||
.attr("href");
|
||||
|
||||
const pluginId = "ln.hako";
|
||||
const baseUrl = "https://ln.hako.vn";
|
||||
|
||||
export const id = pluginId; // string and must be unique
|
||||
export const name = "Hako";
|
||||
export const icon = "src/vi/hakolightnovel/icon.png"; // The relative path to the icon without @icons . For example: 'src/vi/hakolightnovel/icon.png'
|
||||
export const version = "1.0.0"; // xx.xx.xx
|
||||
export const site = baseUrl; // the link to the site
|
||||
|
||||
|
||||
export const popularNovels: Plugin.popularNovels = async (pageNo) => {
|
||||
const novels: Novel.Item[] = [];
|
||||
const link =
|
||||
baseUrl + "/danh-sach?truyendich=1&sapxep=topthang&page=" + pageNo;
|
||||
const result = await fetchApi(link);
|
||||
const body = await result.text();
|
||||
|
||||
const loadedCheerio = parseHTML(body);
|
||||
|
||||
loadedCheerio("main.row > .thumb-item-flow").each(function () {
|
||||
let url = loadedCheerio(this)
|
||||
.find("div.thumb_attr.series-title > a")
|
||||
.attr("href");
|
||||
|
||||
if (url && !isUrlAbsolute(url)) {
|
||||
url = baseUrl + url;
|
||||
}
|
||||
|
||||
if (url) {
|
||||
const name = loadedCheerio(this)
|
||||
.find(".series-title")
|
||||
.text()
|
||||
.trim();
|
||||
let cover = loadedCheerio(this)
|
||||
.find(".img-in-ratio")
|
||||
.attr("data-bg");
|
||||
|
||||
if (cover && !isUrlAbsolute(cover)) {
|
||||
cover = baseUrl + cover;
|
||||
if (url && !isUrlAbsolute(url)) {
|
||||
url = this.site + url;
|
||||
}
|
||||
|
||||
const novel = {
|
||||
name,
|
||||
url,
|
||||
cover,
|
||||
};
|
||||
if (url) {
|
||||
const name = loadedCheerio(ele)
|
||||
.find(".series-title")
|
||||
.text()
|
||||
.trim();
|
||||
let cover = loadedCheerio(ele)
|
||||
.find(".img-in-ratio")
|
||||
.attr("data-bg");
|
||||
|
||||
novels.push(novel);
|
||||
}
|
||||
});
|
||||
if (cover && !isUrlAbsolute(cover)) {
|
||||
cover = this.site + cover;
|
||||
}
|
||||
|
||||
return novels;
|
||||
};
|
||||
const novel = {name, url, cover};
|
||||
|
||||
export const parseNovelAndChapters: Plugin.parseNovelAndChapters = async (
|
||||
novelUrl
|
||||
) => {
|
||||
const novel: Novel.instance = {
|
||||
url: novelUrl,
|
||||
};
|
||||
const result = await fetchApi(novelUrl);
|
||||
const body = await result.text();
|
||||
|
||||
let loadedCheerio = parseHTML(body);
|
||||
|
||||
novel.name = loadedCheerio(".series-name").text();
|
||||
|
||||
const background =
|
||||
loadedCheerio(".series-cover > .a6-ratio > div").attr("style") || "";
|
||||
const novelCover = background.substring(
|
||||
background.indexOf("http"),
|
||||
background.length - 2
|
||||
);
|
||||
|
||||
novel.cover = novelCover
|
||||
? isUrlAbsolute(novelCover)
|
||||
? novelCover
|
||||
: baseUrl + novelCover
|
||||
: "";
|
||||
|
||||
novel.summary = loadedCheerio(".summary-content").text().trim();
|
||||
|
||||
novel.author = loadedCheerio(
|
||||
"#mainpart > div:nth-child(2) > div > div:nth-child(1) > section > main > div.top-part > div > div.col-12.col-md-9 > div.series-information > div:nth-child(2) > span.info-value > a"
|
||||
)
|
||||
.text()
|
||||
.trim();
|
||||
|
||||
novel.genres = loadedCheerio(".series-gernes")
|
||||
.text()
|
||||
.trim()
|
||||
.replace(/ +/g, " ")
|
||||
.split("\n")
|
||||
.filter((e) => e.trim())
|
||||
.join(", ");
|
||||
|
||||
novel.status = loadedCheerio(
|
||||
"#mainpart > div:nth-child(2) > div > div:nth-child(1) > section > main > div.top-part > div > div.col-12.col-md-9 > div.series-information > div:nth-child(4) > span.info-value > a"
|
||||
)
|
||||
.text()
|
||||
.trim();
|
||||
|
||||
const chapters: Chapter.Item[] = [];
|
||||
loadedCheerio(".list-chapters li").each(function () {
|
||||
let chapterUrl = loadedCheerio(this).find("a").attr("href");
|
||||
|
||||
if (chapterUrl && !isUrlAbsolute(chapterUrl)) {
|
||||
chapterUrl = baseUrl + chapterUrl;
|
||||
}
|
||||
|
||||
if (chapterUrl) {
|
||||
const chapterName = loadedCheerio(this)
|
||||
.find(".chapter-name")
|
||||
.text()
|
||||
.trim();
|
||||
const releaseTime = loadedCheerio(this)
|
||||
.find(".chapter-time")
|
||||
.text();
|
||||
|
||||
const chapter = {
|
||||
name: chapterName,
|
||||
releaseTime: releaseTime,
|
||||
url: chapterUrl,
|
||||
};
|
||||
|
||||
chapters.push(chapter);
|
||||
}
|
||||
});
|
||||
novel.chapters = chapters;
|
||||
return novel;
|
||||
};
|
||||
|
||||
export const parseChapter: Plugin.parseChapter = async (chapterUrl) => {
|
||||
const result = await fetchApi(chapterUrl);
|
||||
const body = await result.text();
|
||||
|
||||
const loadedCheerio = parseHTML(body);
|
||||
|
||||
const chapterText = loadedCheerio("#chapter-content").html() || "";
|
||||
|
||||
return chapterText;
|
||||
};
|
||||
|
||||
export const searchNovels: Plugin.searchNovels = async (searchTerm) => {
|
||||
const novels: Novel.Item[] = [];
|
||||
|
||||
const url = baseUrl + "/tim-kiem?keywords=" + searchTerm;
|
||||
const result = await fetchApi(url);
|
||||
const body = await result.text();
|
||||
|
||||
const loadedCheerio = parseHTML(body);
|
||||
|
||||
loadedCheerio("div.row > .thumb-item-flow").each(function () {
|
||||
let novelUrl = loadedCheerio(this)
|
||||
.find("div.thumb_attr.series-title > a")
|
||||
.attr("href");
|
||||
|
||||
if (novelUrl && !isUrlAbsolute(novelUrl)) {
|
||||
novelUrl = baseUrl + novelUrl;
|
||||
}
|
||||
|
||||
if (novelUrl) {
|
||||
const novelName = loadedCheerio(this).find(".series-title").text();
|
||||
let novelCover = loadedCheerio(this)
|
||||
.find(".img-in-ratio")
|
||||
.attr("data-bg");
|
||||
|
||||
if (novelCover && !isUrlAbsolute(novelCover)) {
|
||||
novelCover = baseUrl + novelCover;
|
||||
novels.push(novel);
|
||||
}
|
||||
});
|
||||
return novels;
|
||||
}
|
||||
async popularNovels(pageNo: number, options: Plugin.PopularNovelsOptions): Promise<Plugin.NovelItem[]> {
|
||||
const link = this.site + "/danh-sach?truyendich=1&sapxep=topthang&page=" + pageNo;
|
||||
const result = await fetch(link);
|
||||
const body = await result.text();
|
||||
const loadedCheerio = parseHTML(body);
|
||||
return this.parseNovels(loadedCheerio);
|
||||
}
|
||||
async parseNovelAndChapters(novelUrl: string): Promise<Plugin.SourceNovel> {
|
||||
const novel: Plugin.SourceNovel = {
|
||||
url: novelUrl,
|
||||
};
|
||||
const result = await fetch(novelUrl);
|
||||
const body = await result.text();
|
||||
|
||||
let loadedCheerio = parseHTML(body);
|
||||
|
||||
novel.name = loadedCheerio(".series-name").text();
|
||||
|
||||
const background =
|
||||
loadedCheerio(".series-cover > .a6-ratio > div").attr("style") || "";
|
||||
const novelCover = background.substring(
|
||||
background.indexOf("http"),
|
||||
background.length - 2
|
||||
);
|
||||
|
||||
novel.cover = novelCover
|
||||
? isUrlAbsolute(novelCover)
|
||||
? novelCover
|
||||
: this.site + novelCover
|
||||
: "";
|
||||
|
||||
novel.summary = loadedCheerio(".summary-content").text().trim();
|
||||
|
||||
novel.author = loadedCheerio(
|
||||
"#mainpart > div:nth-child(2) > div > div:nth-child(1) > section > main > div.top-part > div > div.col-12.col-md-9 > div.series-information > div:nth-child(2) > span.info-value > a"
|
||||
)
|
||||
.text()
|
||||
.trim();
|
||||
|
||||
novel.genres = loadedCheerio(".series-gernes")
|
||||
.text()
|
||||
.trim()
|
||||
.replace(/ +/g, " ")
|
||||
.split("\n")
|
||||
.filter((e) => e.trim())
|
||||
.join(", ");
|
||||
|
||||
novel.status = loadedCheerio(
|
||||
"#mainpart > div:nth-child(2) > div > div:nth-child(1) > section > main > div.top-part > div > div.col-12.col-md-9 > div.series-information > div:nth-child(4) > span.info-value > a"
|
||||
)
|
||||
.text()
|
||||
.trim();
|
||||
|
||||
const chapters: Plugin.ChapterItem[] = [];
|
||||
loadedCheerio(".list-chapters li").each((index, ele) => {
|
||||
let chapterUrl = loadedCheerio(ele).find("a").attr("href");
|
||||
|
||||
if (chapterUrl && !isUrlAbsolute(chapterUrl)) {
|
||||
chapterUrl = this.site + chapterUrl;
|
||||
}
|
||||
|
||||
if (chapterUrl) {
|
||||
const chapterName = loadedCheerio(ele)
|
||||
.find(".chapter-name")
|
||||
.text()
|
||||
.trim();
|
||||
const releaseTime = loadedCheerio(ele)
|
||||
.find(".chapter-time")
|
||||
.text();
|
||||
|
||||
const chapter = {
|
||||
name: chapterName,
|
||||
releaseTime: releaseTime,
|
||||
url: chapterUrl,
|
||||
};
|
||||
|
||||
chapters.push(chapter);
|
||||
}
|
||||
});
|
||||
novel.chapters = chapters;
|
||||
return novel;
|
||||
}
|
||||
async parseChapter(chapterUrl: string): Promise<string> {
|
||||
const result = await fetch(chapterUrl);
|
||||
const body = await result.text();
|
||||
|
||||
novels.push({
|
||||
name: novelName,
|
||||
url: novelUrl,
|
||||
cover: novelCover,
|
||||
});
|
||||
}
|
||||
});
|
||||
const loadedCheerio = parseHTML(body);
|
||||
|
||||
return novels;
|
||||
};
|
||||
const chapterText = loadedCheerio("#chapter-content").html() || "";
|
||||
|
||||
export const fetchImage: Plugin.fetchImage = async (url) => {
|
||||
const headers = {
|
||||
Referer: "https://ln.hako.vn",
|
||||
};
|
||||
return await fetchFile(url, { headers: headers });
|
||||
};
|
||||
return chapterText;
|
||||
}
|
||||
async searchNovels(searchTerm: string, pageNo?: number | undefined): Promise<Plugin.NovelItem[]> {
|
||||
const url = this.site + "/tim-kiem?keywords=" + searchTerm;
|
||||
const result = await fetch(url);
|
||||
const body = await result.text();
|
||||
const loadedCheerio = parseHTML(body);
|
||||
return this.parseNovels(loadedCheerio);
|
||||
}
|
||||
async fetchImage(url: string): Promise<string | undefined> {
|
||||
const headers = {
|
||||
Referer: "https://ln.hako.vn",
|
||||
};
|
||||
return await fetchFile(url, { headers: headers });
|
||||
}
|
||||
}
|
||||
|
||||
export default new HakoPlugin();
|
||||
|
||||
Reference in New Issue
Block a user