* add prettier

* lint-staged

* prettier

* upgrade husky
This commit is contained in:
nyagami
2024-03-23 22:00:29 +07:00
committed by GitHub
parent c22979b10f
commit 5f9a067ab9
113 changed files with 79244 additions and 50841 deletions
+145 -119
View File
@@ -1,142 +1,168 @@
import { Plugin } from "@typings/plugin";
import { fetchApi, fetchFile } from "@libs/fetch";
import { CheerioAPI, load as parseHTML } from "cheerio";
import { NovelStatus } from "@libs/novelStatus";
import { Plugin } from '@typings/plugin';
import { fetchApi, fetchFile } from '@libs/fetch';
import { CheerioAPI, load as parseHTML } from 'cheerio';
import { NovelStatus } from '@libs/novelStatus';
class FaqWikiUs implements Plugin.PluginBase {
id = "FWK.US";
name = "Faq Wiki";
site = "https://faqwiki.us/";
version = "1.1.1";
icon = "src/en/faqwikius/icon.png";
id = 'FWK.US';
name = 'Faq Wiki';
site = 'https://faqwiki.us/';
version = '1.1.1';
icon = 'src/en/faqwikius/icon.png';
parseNovels(loadedCheerio:CheerioAPI, searchTerm?: string) {
let novels: Plugin.NovelItem[] = [];
loadedCheerio("figure.wp-block-image").each((index, element) => {
const name = loadedCheerio(element).text();
let cover = loadedCheerio(element).find("img").attr("data-ezsrc");
parseNovels(loadedCheerio: CheerioAPI, searchTerm?: string) {
let novels: Plugin.NovelItem[] = [];
// Remove the appended query string
if (cover) {
cover = cover.replace(/\?ezimgfmt=.*$/, ''); // Regular expression magic!
} else {
cover = loadedCheerio(element).find("img").attr("src");
}
loadedCheerio('figure.wp-block-image').each((index, element) => {
const name = loadedCheerio(element).text();
let cover = loadedCheerio(element).find('img').attr('data-ezsrc');
const path = loadedCheerio(element).find("a").attr("href")?.replace('tp:', 'tps:')?.slice(this.site.length);
if (!path) return;
// Remove the appended query string
if (cover) {
cover = cover.replace(/\?ezimgfmt=.*$/, ''); // Regular expression magic!
} else {
cover = loadedCheerio(element).find('img').attr('src');
}
novels.push({ name, cover, path });
})
const path = loadedCheerio(element)
.find('a')
.attr('href')
?.replace('tp:', 'tps:')
?.slice(this.site.length);
if (!path) return;
if (searchTerm) {
novels = novels.filter((novel) =>
novel.name.toLowerCase().includes(searchTerm.toLowerCase())
);
novels.push({ name, cover, path });
});
if (searchTerm) {
novels = novels.filter(novel =>
novel.name.toLowerCase().includes(searchTerm.toLowerCase()),
);
}
return novels;
}
async popularNovels(
page: number,
{ showLatestNovels }: Plugin.PopularNovelsOptions,
): Promise<Plugin.NovelItem[]> {
const body = await fetchApi(this.site).then(res => res.text());
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
async parseNovel(path: string): Promise<Plugin.SourceNovel> {
const body = await fetchApi(this.site + path).then(res => res.text());
const loadedCheerio = parseHTML(body);
const novel: Plugin.SourceNovel = {
path,
name: '',
};
novel.name = loadedCheerio('.entry-title')
.text()
.replace('Novel – All Chapters', '')
.trim();
novel.cover = loadedCheerio('.wp-block-image')
.find('img')
.attr('data-ezsrc')
?.replace(/\?ezimgfmt=.*$/, ''); // Regular expression magic!
const status = loadedCheerio(
"#lcp_instance_0 +:icontains('complete')",
).text();
novel.status = status ? NovelStatus.Completed : NovelStatus.Ongoing;
const div = loadedCheerio('.book-review-block__meta-item-value');
div.html(div.html()?.replace(/(?<=>)([^<]+)(?=<br\s*\/?>)/g, '<p>$1</p>')!);
loadedCheerio('.book-review-block__meta-item-value strong').each(
(i, el) => {
const key = loadedCheerio(el)
.text()
.replace(':', '')
.trim()
.toLowerCase();
const value = loadedCheerio(el).next().text().replace(':', '').trim();
switch (key) {
case 'description':
novel.summary = value;
break;
case 'author(s)':
novel.author = value;
break;
case 'genre':
novel.genres = value.split(' ').join(',');
}
},
);
return novels;
}
const chapters: Plugin.ChapterItem[] = loadedCheerio(
'.lcp_catlist > li > a',
)
.map((chapterIndex, element) => ({
name: loadedCheerio(element)
.text()
.replace(novel.name + '', '')
.replace('Novel' + '', '')
.trim(),
path: '' + loadedCheerio(element).attr('href')?.slice(this.site.length),
chapterNumber: chapterIndex + 1,
}))
.get();
async popularNovels(
page: number,
{ showLatestNovels }: Plugin.PopularNovelsOptions,
): Promise<Plugin.NovelItem[]> {
const body = await fetchApi(this.site).then((res) => res.text());
const loadedCheerio = parseHTML(body);
novel.chapters = chapters;
return novel;
}
return this.parseNovels(loadedCheerio);
}
async parseChapter(chapterPath: string): Promise<string> {
const body = await fetchApi(this.site + chapterPath).then(res =>
res.text(),
);
const loadedCheerio = parseHTML(body);
loadedCheerio('span').remove();
async parseNovel(path: string): Promise<Plugin.SourceNovel> {
const body = await fetchApi(this.site + path).then((res) => res.text());
const loadedCheerio = parseHTML(body);
const chapterParagraphs = loadedCheerio('.entry-content p');
const novel: Plugin.SourceNovel = {
path,
name: ""
};
let chapterContent; // Variable to store the result
novel.name = loadedCheerio(".entry-title").text().replace("Novel – All Chapters", "").trim();
novel.cover = loadedCheerio(".wp-block-image")
.find("img")
.attr("data-ezsrc")
?.replace(/\?ezimgfmt=.*$/, ''); // Regular expression magic!
const status = loadedCheerio("#lcp_instance_0 +:icontains('complete')").text();
novel.status = (status ? NovelStatus.Completed : NovelStatus.Ongoing)
const div = loadedCheerio(".book-review-block__meta-item-value");
div.html(div.html()?.replace(/(?<=>)([^<]+)(?=<br\s*\/?>)/g, '<p>$1</p>')!);
loadedCheerio(".book-review-block__meta-item-value strong").each((i,el) => {
const key = loadedCheerio(el).text().replace(':', '').trim().toLowerCase();
const value = loadedCheerio(el).next().text().replace(':', '').trim();
switch(key){
case "description":
novel.summary = value;
break;
case "author(s)":
novel.author = value;
break;
case "genre":
novel.genres = value.split(' ').join(',');
}
if (chapterParagraphs.length < 5) {
//some chapter in this site store their whole text in 1-4 <p>,
chapterContent = chapterParagraphs
.map((index, element) => {
const text = loadedCheerio(element).html();
return text;
})
const chapters: Plugin.ChapterItem[] = loadedCheerio(".lcp_catlist > li > a")
.map((chapterIndex, element) => ({
name: loadedCheerio(element).text().replace(novel.name + "", "").replace("Novel" + "", "").trim(),
path: ""+loadedCheerio(element).attr("href")?.slice(this.site.length),
chapterNumber: chapterIndex + 1,
}))
.get();
novel.chapters = chapters;
return novel;
.get()
.join('\n\n');
} else {
// Multiple paragraphs case
chapterContent = chapterParagraphs
.map((index, element) => {
const text = loadedCheerio(element).text().trim();
return `<p>${text}</p>`;
})
.get()
.join('\n\n');
}
async parseChapter(chapterPath: string): Promise<string> {
const body = await fetchApi(this.site + chapterPath).then((res) => res.text());
const loadedCheerio = parseHTML(body);
loadedCheerio("span").remove();
return chapterContent;
}
const chapterParagraphs = loadedCheerio(".entry-content p");
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {
const body = await fetchApi(this.site).then(res => res.text());
const loadedCheerio = parseHTML(body);
let chapterContent; // Variable to store the result
return this.parseNovels(loadedCheerio, searchTerm);
}
if (chapterParagraphs.length < 5) { //some chapter in this site store their whole text in 1-4 <p>,
chapterContent = chapterParagraphs.map((index, element) => {
const text = loadedCheerio(element).html()
return text;
}).get().join('\n\n');
}
else {
// Multiple paragraphs case
chapterContent = chapterParagraphs.map((index, element) => {
const text = loadedCheerio(element).text().trim();
return `<p>${text}</p>`;
}).get().join('\n\n');
}
return chapterContent;
}
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {
const body = await fetchApi(this.site).then((res) => res.text());
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio, searchTerm);
}
fetchImage = fetchFile;
fetchImage = fetchFile;
}
export default new FaqWikiUs();