Files
lnreader-plugins/scripts/multisrc/madara/template.ts
T
Rider21 fc52a6e665 Plugins (#718)
* fix

* test

* sync

* nitromanga -> madara

* test wuxiaworld

* xm

* add chapterNumber

* fix

chapterNumber > 0

* test filters

* nice

* tunovelaligera -> madara

* Squashed commit of the following:

commit 4c7bee5855
Author: nyagami <hoangquan05112002@gmail.com>
Date:   Mon Jan 29 17:56:23 2024 +0700

    fix: use english lang in plugin item consistent

commit b26d1129de
Author: uy/sun <hmy0119@hotmail.com>
Date:   Sun Jan 28 20:22:43 2024 +0800

    Chinese: support showLatestNovels (#733)

    * improve: better chapterText

    * feat: support latest page

commit 6ff206eab3
Author: nyagami <hoangquan05112002@gmail.com>
Date:   Sat Jan 27 09:26:19 2024 +0700

    Refactor: Using English key (#728)

    * fix: use english key instead of native

    * fix: ukrainian

    * fix: web test with new plugins map

commit 62fc988019
Author: Marcin Majewski <skilletssgames@gmail.com>
Date:   Fri Jan 26 14:42:54 2024 +0100

    Fix search box styles (#727)

commit 3a1e0c43eb
Author: uy/sun <hmy0119@hotmail.com>
Date:   Fri Jan 26 19:46:47 2024 +0800

    Add 69shu and 69xinshu (#726)

    * fix(linovelib):  use NovelStatus and translate filter label

    * feat(69xinshu): add 69xinshu

    * feat(69shu): add 69shu

    * remove: empty userAgent

* updated the filters and removed dead sites.

Error: net::ERR_NAME_NOT_RESOLVED at https://cratenovel.com/novel
Error: net::ERR_CONNECTION_REFUSED at https://clicknovel.net/novel
Error: net::ERR_NAME_NOT_RESOLVED at https://noobchan.xyz/novel
Error: net::ERR_CONNECTION_TIMED_OUT at https://sugarbbscan.com/series

* test

* Squashed commit of the following:

commit cadf7a72fe
Author: Felipe Bonfim <felipebonfim2006@gmail.com>
Date:   Tue Jan 30 14:55:34 2024 -0400

    kiniga added (#739)

    * kiniga added

    * kiniga updated icon

commit 38fc1860da
Author: K1ngfish3r <trulyihu@gmail.com>
Date:   Mon Jan 29 23:27:42 2024 +0800

    (add src) LNMTL & ScribbleHub (#738)

    * quick

    * quick panda fix

    * remove console.log

commit 4c7bee5855
Author: nyagami <hoangquan05112002@gmail.com>
Date:   Mon Jan 29 17:56:23 2024 +0700

    fix: use english lang in plugin item consistent

commit b26d1129de
Author: uy/sun <hmy0119@hotmail.com>
Date:   Sun Jan 28 20:22:43 2024 +0800

    Chinese: support showLatestNovels (#733)

    * improve: better chapterText

    * feat: support latest page

commit 6ff206eab3
Author: nyagami <hoangquan05112002@gmail.com>
Date:   Sat Jan 27 09:26:19 2024 +0700

    Refactor: Using English key (#728)

    * fix: use english key instead of native

    * fix: ukrainian

    * fix: web test with new plugins map

commit 62fc988019
Author: Marcin Majewski <skilletssgames@gmail.com>
Date:   Fri Jan 26 14:42:54 2024 +0100

    Fix search box styles (#727)

commit 3a1e0c43eb
Author: uy/sun <hmy0119@hotmail.com>
Date:   Fri Jan 26 19:46:47 2024 +0800

    Add 69shu and 69xinshu (#726)

    * fix(linovelib):  use NovelStatus and translate filter label

    * feat(69xinshu): add 69xinshu

    * feat(69shu): add 69shu

    * remove: empty userAgent

* add wuxiamtl, fannovel

* fix

* Update ranobelib.ts

* parseMadaraDate deleted

* remove multisrc name from Plugin item

---------

Co-authored-by: nyagami <hoangquan05112002@gmail.com>
2024-02-04 12:47:12 +07:00

325 lines
9.2 KiB
TypeScript

import { fetchApi, fetchFile } from "@libs/fetch";
import { Filters, FilterTypes } from "@libs/filterInputs";
import { Plugin } from "@typings/plugin";
import { load as parseHTML } from "cheerio";
import { defaultCover } from "@libs/defaultCover";
import { NovelStatus } from "@libs/novelStatus";
import dayjs from "dayjs";
const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));
const includesAny = (str: string, keywords: string[]) =>
new RegExp(keywords.join("|")).test(str);
interface MadaraOptionPath {
genres?: string;
novels?: string;
novel?: string;
chapter?: string;
}
const MadaraDefaultPath = {
genres: "novel-genre",
novels: "novel",
novel: "novel",
chapter: "novel",
};
interface MadaraOptions {
useNewChapterEndpoint?: boolean;
path?: MadaraOptionPath;
lang?: string;
orderBy?: string;
}
export interface MadaraMetadata {
id: string;
sourceSite: string;
sourceName: string;
options?: MadaraOptions;
filters?: Filters;
}
class MadaraPlugin implements Plugin.PluginBase {
id: string;
name: string;
icon: string;
site: string;
version: string;
options?: MadaraOptions;
filters?: Filters | undefined;
constructor(metadata: MadaraMetadata) {
this.id = metadata.id;
this.name = metadata.sourceName;
const iconFileName = metadata.sourceName.replace(/\s+/g, "").toLowerCase();
this.icon = `multisrc/madara/icons/${iconFileName}.png`;
this.site = metadata.sourceSite;
this.version = "1.0.0";
this.options = metadata.options;
this.filters = metadata.filters;
}
async popularNovels(
pageNo: number,
{
filters,
showLatestNovels,
}: Plugin.PopularNovelsOptions<typeof this.filters>,
): Promise<Plugin.NovelItem[]> {
const novels: Plugin.NovelItem[] = [];
let url = this.site;
if (filters?.genres?.value) {
url +=
(this.options?.path?.genres || MadaraDefaultPath.genres) +
"/" + filters.genres.value;
} else {
url += this.options?.path?.novels || MadaraDefaultPath.novels;
}
url += "/page/" + pageNo + "/?m_orderby=" +
(showLatestNovels ? "latest" : filters?.sort?.value || "rating");
const body = await fetchApi(url).then((res) => res.text());
const loadedCheerio = parseHTML(body);
loadedCheerio(".manga-title-badges").remove();
loadedCheerio(".page-item-detail").each(function () {
const novelName = loadedCheerio(this).find(".post-title").text().trim();
const image = loadedCheerio(this).find("img");
const novelCover = image.attr("data-src") || image.attr("src");
const novelUrl =
loadedCheerio(this).find(".post-title").find("a").attr("href") || "";
if (!novelName || !novelUrl) return;
const novel: Plugin.NovelItem = {
name: novelName,
cover: novelCover,
url: novelUrl,
};
novels.push(novel);
});
return novels;
}
async parseNovelAndChapters(novelUrl: string): Promise<Plugin.SourceNovel> {
const novel: Plugin.SourceNovel = {
url: novelUrl,
};
const body = await fetchApi(novelUrl).then((res) => res.text());
let loadedCheerio = parseHTML(body);
loadedCheerio(".manga-title-badges, #manga-title span").remove();
novel.name =
loadedCheerio(".post-title h1").text().trim() ||
loadedCheerio("#manga-title h1").text();
novel.cover =
loadedCheerio(".summary_image > a > img").attr("data-lazy-src") ||
loadedCheerio(".summary_image > a > img").attr("data-src") ||
loadedCheerio(".summary_image > a > img").attr("src") ||
loadedCheerio(".summary_image > a > img").attr("src") ||
defaultCover;
loadedCheerio(".post-content_item, .post-content").each(function () {
const detailName = loadedCheerio(this).find("h5").text().trim();
const detail = loadedCheerio(this).find(".summary-content").text().trim();
switch (detailName) {
case "Genre(s)":
case "Género(s)":
case "التصنيفات":
novel.genres = detail;
break;
case "Author(s)":
case "Autor(es)":
case "المؤلف":
case "المؤلف (ين)":
novel.author = detail;
break;
case "Status":
case "Estado":
novel.status =
detail.includes("OnGoing") || detail.includes("مستمرة")
? NovelStatus.Ongoing
: NovelStatus.Completed;
break;
}
});
loadedCheerio("div.summary__content .code-block,script").remove();
novel.summary =
loadedCheerio("div.summary__content").text().trim() ||
loadedCheerio("#tab-manga-about").text().trim() ||
loadedCheerio('.post-content_item h5:contains("Summary")')
.next()
.text()
.trim();
const chapters: Plugin.ChapterItem[] = [];
let html = "";
if (this.options?.useNewChapterEndpoint) {
html = await fetchApi(novelUrl + "ajax/chapters/", {
method: "POST",
}).then((res) => res.text());
} else {
const novelId =
loadedCheerio(".rating-post-id").attr("value") ||
loadedCheerio("#manga-chapters-holder").attr("data-id") ||
"";
const formData = new FormData();
formData.append("action", "manga_get_chapters");
formData.append("manga", novelId);
html = await fetchApi(this.site + "wp-admin/admin-ajax.php", {
method: "POST",
body: formData,
}).then((res) => res.text());
}
if (html !== "0") {
loadedCheerio = parseHTML(html);
}
const totalChapters = loadedCheerio(".wp-manga-chapter").length;
loadedCheerio(".wp-manga-chapter").each((chapterIndex, element) => {
const chapterName = loadedCheerio(element).find("a").text().trim();
let releaseDate = loadedCheerio(element)
.find("span.chapter-release-date")
.text()
.trim();
if (releaseDate) {
releaseDate = this.parseData(releaseDate);
} else {
releaseDate = dayjs().format("LL");
}
let chapterUrl = loadedCheerio(element).find("a").attr("href") || "";
chapters.push({
name: chapterName,
url: chapterUrl,
releaseTime: releaseDate || null,
chapterNumber: totalChapters - chapterIndex,
});
});
novel.chapters = chapters.reverse();
return novel;
}
async parseChapter(chapterUrl: string): Promise<string> {
const body = await fetchApi(chapterUrl).then((res) => res.text());
const loadedCheerio = parseHTML(body);
const chapterText =
loadedCheerio(".text-left").html() ||
loadedCheerio(".text-right").html() ||
loadedCheerio(".entry-content").html() ||
loadedCheerio(".c-blog-post > div > div:nth-child(2)").html() ||
"";
return chapterText;
}
async searchNovels(
searchTerm: string,
pageNo?: number | undefined,
): Promise<Plugin.NovelItem[]> {
const novels: Plugin.NovelItem[] = [];
const url = this.site + "?s=" + searchTerm + "&post_type=wp-manga";
const body = await fetchApi(url).then((res) => res.text());
const loadedCheerio = parseHTML(body);
loadedCheerio(".c-tabs-item__content").each(function () {
const novelName = loadedCheerio(this).find(".post-title").text().trim();
let image = loadedCheerio(this).find("img");
const novelCover = image.attr("data-src") || image.attr("src");
let novelUrl =
loadedCheerio(this).find(".post-title").find("a").attr("href") || "";
const novel = {
name: novelName,
cover: novelCover,
url: novelUrl,
};
novels.push(novel);
});
return novels;
}
fetchImage = fetchFile;
parseData = (date: string) => {
const dayJSDate = dayjs(); // today
const timeAgo = date.match(/\d+/)?.[0] || "";
const timeAgoInt = parseInt(timeAgo, 10);
if (!timeAgo) return date; // there is no number!
if (includesAny(date, ["detik", "segundo", "second", "วินาที"])) {
dayJSDate.subtract(timeAgoInt, "second"); // go back N seconds
} else if (
includesAny(date, [
"menit",
"dakika",
"min",
"minute",
"minuto",
"นาที",
"دقائق",
])
) {
dayJSDate.subtract(timeAgoInt, "minute"); // go back N minute
} else if (
includesAny(date, [
"jam",
"saat",
"heure",
"hora",
"hour",
"ชั่วโมง",
"giờ",
"ore",
"ساعة",
"小时",
])
) {
dayJSDate.subtract(timeAgoInt, "hours"); // go back N hours
} else if (
includesAny(date, [
"hari",
"gün",
"jour",
"día",
"dia",
"day",
"วัน",
"ngày",
"giorni",
"أيام",
"天",
])
) {
dayJSDate.subtract(timeAgoInt, "days"); // go back N days
} else if (includesAny(date, ["week", "semana"])) {
dayJSDate.subtract(timeAgoInt, "week"); // go back N a week
} else if (includesAny(date, ["month", "mes"])) {
dayJSDate.subtract(timeAgoInt, "month"); // go back N months
} else if (includesAny(date, ["year", "año"])) {
dayJSDate.subtract(timeAgoInt, "year"); // go back N years
} else {
return date;
}
return dayJSDate.format("LL");
};
}