Refactor: Faqwiki (#1159)

* refactor

* mutlWordGenres

* cover fix

* replace slice

* replace trailing slash
This commit is contained in:
K1ngfish3r
2024-08-22 23:23:29 +08:00
committed by GitHub
parent b44bfe26a3
commit d1a4a91c49
+94 -69
View File
@@ -6,15 +6,18 @@ import { NovelStatus } from '@libs/novelStatus';
class FaqWikiUs implements Plugin.PluginBase { class FaqWikiUs implements Plugin.PluginBase {
id = 'FWK.US'; id = 'FWK.US';
name = 'Faq Wiki'; name = 'Faq Wiki';
site = 'https://faqwiki.us/'; site = 'https://www.faqwiki.us/';
version = '1.1.1'; version = '2.0.0';
icon = 'src/en/faqwikius/icon.png'; icon = 'src/en/faqwikius/icon.png';
parseNovels(loadedCheerio: CheerioAPI, searchTerm?: string) { parseNovels(loadedCheerio: CheerioAPI, searchTerm?: string) {
let novels: Plugin.NovelItem[] = []; let novels: Plugin.NovelItem[] = [];
loadedCheerio('figure.wp-block-image').each((index, element) => { loadedCheerio('.plt-page-item').each((index, element) => {
const name = loadedCheerio(element).text(); const name = loadedCheerio(element)
.text()
.replace('Novel – All Chapters', '')
.trim();
let cover = loadedCheerio(element).find('img').attr('data-ezsrc'); let cover = loadedCheerio(element).find('img').attr('data-ezsrc');
// Remove the appended query string // Remove the appended query string
@@ -28,7 +31,8 @@ class FaqWikiUs implements Plugin.PluginBase {
.find('a') .find('a')
.attr('href') .attr('href')
?.replace('tp:', 'tps:') ?.replace('tp:', 'tps:')
?.slice(this.site.length); ?.replace(this.site, '')
?.replace(/\/+$/, '');
if (!path) return; if (!path) return;
novels.push({ name, cover, path }); novels.push({ name, cover, path });
@@ -64,55 +68,72 @@ class FaqWikiUs implements Plugin.PluginBase {
.replace('Novel – All Chapters', '') .replace('Novel – All Chapters', '')
.trim(); .trim();
novel.cover = loadedCheerio('.wp-block-image') const img = loadedCheerio('.wp-block-image img');
.find('img') const cover = img.attr('data-ezsrc') || img.attr('src');
.attr('data-ezsrc') novel.cover = cover?.replace(/\?ezimgfmt=.*$/, ''); // Regular expression magic!
?.replace(/\?ezimgfmt=.*$/, ''); // Regular expression magic!
const status = loadedCheerio( const status = loadedCheerio(
"#lcp_instance_0 +:icontains('complete')", "#lcp_instance_0 +:icontains('complete')",
).text(); ).text();
novel.status = status ? NovelStatus.Completed : NovelStatus.Ongoing; novel.status = status ? NovelStatus.Completed : NovelStatus.Ongoing;
const div = loadedCheerio('.book-review-block__meta-item-value'); loadedCheerio('.entry-content strong').each((i, el) => {
const key = loadedCheerio(el).text().trim().toLowerCase();
const parent = loadedCheerio(el).parent();
const values = [parent.text().slice(key.length).trim()].concat(
parent
.nextUntil('p:has(strong)')
.map((e, ax) => loadedCheerio(ax).text().trim())
.get(),
);
let genreText = values.join(' ').trim();
const multiWordGenres = [
//add more when found
'Slice of Life',
'School Life',
];
multiWordGenres.forEach(genre => {
genreText = genreText.replace(
new RegExp(`\\b${genre}\\b`, 'g'),
genre.replace(/ /g, '_'),
);
});
const genres = genreText
.split(/\s+/)
.map(word => word.replace(/_/g, ' '))
.join(', ');
div.html(div.html()?.replace(/(?<=>)([^<]+)(?=<br\s*\/?>)/g, '<p>$1</p>')); switch (key) {
case 'description:':
novel.summary = values.join('\n');
break;
case 'author(s):':
novel.author = values[0];
break;
case 'genre:':
novel.genres = genres;
}
});
loadedCheerio('.book-review-block__meta-item-value strong').each( const chapters: Plugin.ChapterItem[] = [];
(i, el) => { loadedCheerio('.lcp_catlist li a').each((chapterIndex, element) => {
const key = loadedCheerio(el) const name = loadedCheerio(element)
.text() .text()
.replace(':', '') .replace(novel.name + '', '')
.trim() .replace('Novel' + '', '')
.toLowerCase(); .trim();
const value = loadedCheerio(el).next().text().replace(':', '').trim(); const path = loadedCheerio(element)
.attr('href')
switch (key) { ?.replace(this.site, '')
case 'description': ?.replace(/\/+$/, '');
novel.summary = value; const chapterNumber = chapterIndex + 1;
break; if (!path) return;
case 'author(s)': chapters.push({
novel.author = value; name,
break; path,
case 'genre': chapterNumber,
novel.genres = value.split(' ').join(','); });
} });
},
);
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; novel.chapters = chapters;
return novel; return novel;
@@ -123,33 +144,37 @@ class FaqWikiUs implements Plugin.PluginBase {
res.text(), res.text(),
); );
const loadedCheerio = parseHTML(body); const loadedCheerio = parseHTML(body);
loadedCheerio('span').remove(); const removal = ['.entry-content span', '.entry-content div', 'script'];
removal.map(e => {
loadedCheerio(e).remove();
});
const chapterParagraphs = loadedCheerio('.entry-content p'); const chapterText = loadedCheerio('.entry-content').html()!;
// const chapterParagraphs = loadedCheerio('.entry-content p');
let chapterContent; // Variable to store the result // let chapterContent; // Save this code in case needed
if (chapterParagraphs.length < 5) { // if (chapterParagraphs.length < 5) {
//some chapter in this site store their whole text in 1-4 <p>, // //some chapter in this site store their whole text in 1-4 <p>,
chapterContent = chapterParagraphs // chapterContent = chapterParagraphs
.map((index, element) => { // .map((index, element) => {
const text = loadedCheerio(element).html(); // const text = loadedCheerio(element).html();
return text; // return text;
}) // })
.get() // .get()
.join('\n\n'); // .join('\n\n');
} else { // } else {
// Multiple paragraphs case // // Multiple paragraphs case
chapterContent = chapterParagraphs // chapterContent = chapterParagraphs
.map((index, element) => { // .map((index, element) => {
const text = loadedCheerio(element).text().trim(); // const text = loadedCheerio(element).text().trim();
return `<p>${text}</p>`; // return `<p>${text}</p>`;
}) // })
.get() // .get()
.join('\n\n'); // .join('\n\n');
} // }
return chapterContent; return chapterText;
} }
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> { async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {