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
+140 -81
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,102 +60,161 @@ 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() const ratingText = normalizeText($('.rating .score').first().text()).replace(
.match(/bookId = (\d+);/)![1]; /[^0-9.]/g,
const chapter: Plugin.ChapterItem[] = []; '',
);
const getChapters = async (id: string) => { 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 chapterName = loadedCheerio(el) const months = [
.find('.chapter-title') 'jan',
.text() 'feb',
.trim(); 'mar',
'apr',
const releaseDate = loadedCheerio(el) 'may',
.find('.chapter-update') 'jun',
.text() 'jul',
.trim(); 'aug',
'sep',
const months = [ 'oct',
'jan', 'nov',
'feb', 'dec',
'mar', ];
'apr',
'may', const dateRegex = new RegExp(
'jun', `(${months.join('|')})\\s+(\\d{1,2}),\\s+(\\d{4})`,
'jul', 'i',
'aug', );
'sep',
'oct', $$('li').each((_, el) => {
'nov', const item = $$(el);
'dec',
]; const chapterName = normalizeText(item.find('.chapter-title').text());
const monthsJoined = months.join('|'); const releaseDateText = normalizeText(item.find('.chapter-update').text());
const chapterHref = item.find('a').attr('href');
const rx = new RegExp(
`(${monthsJoined}) (\\d{1,2}), (\\d{4})`, if (!chapterHref) return;
'i',
).exec(releaseDate); const chapterPath = chapterHref.startsWith('/')
if (!rx) return; ? chapterHref.slice(1)
const year = +rx[3]; : chapterHref;
const month = months.indexOf(rx[1].toLowerCase());
const day = +rx[2]; const chapterItem: Plugin.ChapterItem = {
name: chapterName || 'Untitled',
const chapterUrl = loadedCheerio(el).find('a').attr('href')?.slice(1); path: chapterPath,
};
if (!chapterUrl) return;
const dateMatch = dateRegex.exec(releaseDateText);
chapter.push({ if (dateMatch) {
name: chapterName, const year = Number.parseInt(dateMatch[3], 10);
releaseTime: new Date(year, month, day).toISOString(), const month = months.indexOf(dateMatch[1].toLowerCase());
path: chapterUrl, 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();
return novel; return novel;
} }