fix: Update NovelBuddy parseNovel to Include Rating and Fix Genres (#2123)

* Refactor parseNovel method, include rating and fix genres parsing

* Bump version from 1.0.3 to 1.0.4
This commit is contained in:
Ahmed Mohamed
2026-04-16 20:38:34 +02:00
committed by GitHub
parent d3e50df3c2
commit 85065b9372
+125 -66
View File
@@ -7,7 +7,7 @@ class NovelBuddy implements Plugin.PluginBase {
id = 'novelbuddy'; id = 'novelbuddy';
name = 'NovelBuddy.io'; name = 'NovelBuddy.io';
site = 'https://novelbuddy.io/'; site = 'https://novelbuddy.io/';
version = '1.0.3'; version = '1.0.4';
icon = 'src/en/novelbuddy/icon.png'; icon = 'src/en/novelbuddy/icon.png';
parseNovels(loadedCheerio: CheerioAPI) { parseNovels(loadedCheerio: CheerioAPI) {
@@ -60,98 +60,157 @@ class NovelBuddy implements Plugin.PluginBase {
} }
async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> { async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> {
const result = await fetchApi(this.site + novelPath); const response = await fetchApi(this.site + novelPath);
const body = await result.text(); const body = await response.text();
let loadedCheerio = parseHTML(body); const $ = parseHTML(body);
const coverSrc =
$('.img-cover img').attr('data-src') ||
$('.img-cover img').attr('src') ||
'';
const normalizeUrl = (url?: string): string => {
if (!url) return '';
if (url.startsWith('//')) return `https:${url}`;
if (url.startsWith('/')) return `${this.site}${url.replace(/^\//, '')}`;
return url;
};
const normalizeText = (text: string): string =>
text.replace(/\s+/g, ' ').replace(/\s*,\s*$/, '').trim();
const getLinkTextList = (root: ReturnType<typeof $>): string[] =>
root
.find('a')
.map((_, el) => normalizeText($(el).text()))
.toArray()
.filter(Boolean);
const novel: Plugin.SourceNovel = { const novel: Plugin.SourceNovel = {
path: novelPath, path: novelPath,
name: loadedCheerio('.name h1').text().trim() || 'Untitled', name: normalizeText($('.name h1').first().text()) || 'Untitled',
cover: 'https:' + loadedCheerio('.img-cover img').attr('data-src'), cover: normalizeUrl(coverSrc),
summary: loadedCheerio('.section-body.summary .content').text().trim(), summary: normalizeText($('.section-body.summary .content').text()),
chapters: [], chapters: [],
}; };
loadedCheerio('.meta.box p').each((i, el) => { $('.meta.box p').each((_, el) => {
const detailName = loadedCheerio(el).find('strong').text(); const row = $(el);
const detail = loadedCheerio(el).find('a'); const label = normalizeText(row.find('strong').first().text());
const linkTexts = getLinkTextList(row);
switch (detailName) { switch (label) {
case 'Authors :': case 'Authors :':
novel.author = detail novel.author = linkTexts.join(', ');
.find('span')
.map((a, ex) => loadedCheerio(ex).text())
.toArray()
.join(', ');
break; break;
case 'Artists :':
case 'Artist :':
novel.artist = linkTexts.join(', ');
break;
case 'Status :': case 'Status :':
novel.status = detail.text(); novel.status = linkTexts[0] || normalizeText(row.text().replace(label, ''));
break; break;
case 'Genres :': case 'Genres :':
novel.genres = detail.text().trim(); novel.genres = linkTexts.join(',');
break; break;
} }
}); });
const novelId = loadedCheerio('script')
.text()
.match(/bookId = (\d+);/)![1];
const chapter: Plugin.ChapterItem[] = [];
const getChapters = async (id: string) => { const ratingText = normalizeText($('.rating .score').first().text()).replace(
/[^0-9.]/g,
'',
);
const rating = Number.parseFloat(ratingText);
if (!Number.isNaN(rating)) {
novel.rating = rating;
}
const scriptText = $('script')
.map((_, el) => $(el).html() || '')
.toArray()
.join('\n');
const novelIdMatch = scriptText.match(/bookId\s*=\s*(\d+)\s*;/);
if (!novelIdMatch) {
return novel;
}
const novelId = novelIdMatch[1];
const getChapters = async (id: string): Promise<Plugin.ChapterItem[]> => {
const chapterListUrl = `${this.site}api/manga/${id}/chapters?source=detail`; const chapterListUrl = `${this.site}api/manga/${id}/chapters?source=detail`;
const data = await fetchApi(chapterListUrl); const chapterResponse = await fetchApi(chapterListUrl);
const chapterlist = await data.text(); const chapterHtml = await chapterResponse.text();
loadedCheerio = parseHTML(chapterlist); const $$ = parseHTML(chapterHtml);
const chapters: Plugin.ChapterItem[] = [];
loadedCheerio('li').each((i, el) => { const months = [
const chapterName = loadedCheerio(el) 'jan',
.find('.chapter-title') 'feb',
.text() 'mar',
.trim(); 'apr',
'may',
'jun',
'jul',
'aug',
'sep',
'oct',
'nov',
'dec',
];
const releaseDate = loadedCheerio(el) const dateRegex = new RegExp(
.find('.chapter-update') `(${months.join('|')})\\s+(\\d{1,2}),\\s+(\\d{4})`,
.text() 'i',
.trim(); );
const months = [ $$('li').each((_, el) => {
'jan', const item = $$(el);
'feb',
'mar',
'apr',
'may',
'jun',
'jul',
'aug',
'sep',
'oct',
'nov',
'dec',
];
const monthsJoined = months.join('|');
const rx = new RegExp( const chapterName = normalizeText(item.find('.chapter-title').text());
`(${monthsJoined}) (\\d{1,2}), (\\d{4})`, const releaseDateText = normalizeText(item.find('.chapter-update').text());
'i', const chapterHref = item.find('a').attr('href');
).exec(releaseDate);
if (!rx) return;
const year = +rx[3];
const month = months.indexOf(rx[1].toLowerCase());
const day = +rx[2];
const chapterUrl = loadedCheerio(el).find('a').attr('href')?.slice(1); if (!chapterHref) return;
if (!chapterUrl) return; const chapterPath = chapterHref.startsWith('/')
? chapterHref.slice(1)
: chapterHref;
chapter.push({ const chapterItem: Plugin.ChapterItem = {
name: chapterName, name: chapterName || 'Untitled',
releaseTime: new Date(year, month, day).toISOString(), path: chapterPath,
path: chapterUrl, };
});
const dateMatch = dateRegex.exec(releaseDateText);
if (dateMatch) {
const year = Number.parseInt(dateMatch[3], 10);
const month = months.indexOf(dateMatch[1].toLowerCase());
const day = Number.parseInt(dateMatch[2], 10);
if (month >= 0) {
chapterItem.releaseTime = new Date(year, month, day).toISOString();
}
} else if (releaseDateText) {
chapterItem.releaseTime = releaseDateText;
}
const chapterNumberMatch = chapterName.match(
/chapter\s+(\d+(?:\.\d+)?)/i,
);
if (chapterNumberMatch) {
chapterItem.chapterNumber = Number.parseFloat(chapterNumberMatch[1]);
}
chapters.push(chapterItem);
}); });
return chapter;
return chapters;
}; };
novel.chapters = (await getChapters(novelId)).reverse(); novel.chapters = (await getChapters(novelId)).reverse();