20c2b75b01
* refactor: Move Plugins Out and Consolidate with Multisrc * feat: Overhaul Playground UI * feat: Add Filters to New UI * feat: Persist Selected Plugin * feat: Add Direct Parse Novel/Chapter Options and Feedback Toasts [skip ci] * feat: UX Improvements * chore: Update Plugin Path in Publish Workflow * fix: Add Backward Compatibility * chore: Update Plugin Publishing Script
172 lines
5.3 KiB
TypeScript
172 lines
5.3 KiB
TypeScript
import { CheerioAPI, load as parseHTML } from 'cheerio';
|
|
import { fetchApi } from '@libs/fetch';
|
|
import { Plugin } from '@/types/plugin';
|
|
import { Filters } from '@libs/filterInputs';
|
|
import { NovelStatus } from '@libs/novelStatus';
|
|
|
|
type SearchedNovel = {
|
|
name: string;
|
|
slug: string;
|
|
coverUrl: string;
|
|
};
|
|
type SearchedResult = {
|
|
data?: SearchedNovel[];
|
|
};
|
|
|
|
class LightNovelVN implements Plugin.PagePlugin {
|
|
id = 'lightnovel.vn';
|
|
name = 'Light Novel VN';
|
|
version = '1.0.0';
|
|
icon = 'src/vi/lightnovelvn/icon.png';
|
|
filters?: Filters | undefined;
|
|
site = 'https://lightnovel.vn';
|
|
async popularNovels(pageNo: number): Promise<Plugin.NovelItem[]> {
|
|
const url = `${this.site}/truyen-hot-ds?page=${pageNo}`;
|
|
const body = await fetchApi(url).then(r => r.text());
|
|
|
|
const loadedCheerio = parseHTML(body);
|
|
|
|
const novels: Plugin.NovelItem[] = [];
|
|
|
|
loadedCheerio(".flex.flex-col[itemtype='https://schema.org/Book']").each(
|
|
(idx, ele) => {
|
|
const novelName = loadedCheerio(ele)
|
|
.find('h3[itemprop="name"] > a')
|
|
.text()
|
|
.trim();
|
|
const img = loadedCheerio(ele).find('noscript').html();
|
|
const novelCover = img?.match(/srcSet="([^\s]+)/)?.[1];
|
|
const novelUrl = loadedCheerio(ele)
|
|
.find('h3[itemprop="name"] > a')
|
|
.attr('href');
|
|
if (novelUrl)
|
|
novels.push({
|
|
name: novelName,
|
|
cover: novelCover,
|
|
path: novelUrl.replace(this.site, ''),
|
|
});
|
|
},
|
|
);
|
|
|
|
return novels;
|
|
}
|
|
parseChapters(loadedCheerio: CheerioAPI) {
|
|
const chapters: Plugin.ChapterItem[] = [];
|
|
loadedCheerio('ul.chapter-list > li').each((idx, ele) => {
|
|
const chNum = Number(loadedCheerio(ele).find('div').first().text());
|
|
const chapterUrl = loadedCheerio(ele).find('a').attr('href');
|
|
const name = loadedCheerio(ele).find('a').text().trim();
|
|
if (chapterUrl) {
|
|
chapters.push({
|
|
path: chapterUrl.replace(this.site, ''),
|
|
name,
|
|
chapterNumber: chNum,
|
|
});
|
|
}
|
|
});
|
|
return chapters;
|
|
}
|
|
async parseNovel(
|
|
novelPath: string,
|
|
): Promise<Plugin.SourceNovel & { totalPages: number }> {
|
|
const url = this.site + novelPath;
|
|
const body = await fetchApi(url).then(r => r.text());
|
|
|
|
const loadedCheerio = parseHTML(body);
|
|
|
|
const novel: Plugin.SourceNovel & { totalPages: number } = {
|
|
path: novelPath,
|
|
chapters: [],
|
|
name: 'Không có tiêu đề',
|
|
totalPages: 1,
|
|
};
|
|
|
|
novel.name = loadedCheerio('h1[itemprop="name"]').text().trim();
|
|
|
|
novel.cover = loadedCheerio('header div:nth-child(2) img')
|
|
.attr('srcset')
|
|
?.split(/\s+/)[0];
|
|
|
|
const genres: string[] = [];
|
|
loadedCheerio('a[itemprop="genre"]').each(function () {
|
|
genres.push(loadedCheerio(this).text());
|
|
});
|
|
novel.genres = genres.join(',');
|
|
|
|
novel.status = loadedCheerio('span.font-bold.text-size22:last').text();
|
|
if (novel.status === 'Đang ra') {
|
|
novel.status = NovelStatus.Ongoing;
|
|
} else if (novel.status === 'Hoàn thành') {
|
|
novel.status = NovelStatus.Completed;
|
|
} else {
|
|
novel.status = NovelStatus.Unknown;
|
|
}
|
|
novel.author = loadedCheerio('a[itemprop="author"] > span').text();
|
|
|
|
novel.summary = loadedCheerio('#bookIntro').text().replace(/\s+/g, ' ');
|
|
const delay = (ms: number) => new Promise(res => setTimeout(res, ms));
|
|
await delay(1000);
|
|
const chapterListUrl = url + '/danh-sach-chuong';
|
|
|
|
const chapterListBody = await fetchApi(chapterListUrl).then(r => r.text());
|
|
const loadedChapterList = parseHTML(chapterListBody);
|
|
novel.chapters = this.parseChapters(loadedChapterList);
|
|
loadedChapterList('nav[aria-label="Pagination"] a').each((index, ele) => {
|
|
const href = ele.attribs['href'];
|
|
if (href) {
|
|
const page = Number(href.match(/\?page=(\d+)/)?.[1]);
|
|
if (page && page > novel.totalPages) {
|
|
novel.totalPages = page;
|
|
}
|
|
}
|
|
});
|
|
return novel;
|
|
}
|
|
async parsePage(novelPath: string, page: string): Promise<Plugin.SourcePage> {
|
|
const url = `${this.site}${novelPath}/danh-sach-chuong?page=${page}`;
|
|
const chapterListBody = await fetchApi(url).then(r => r.text());
|
|
const chapters = this.parseChapters(parseHTML(chapterListBody));
|
|
return {
|
|
chapters,
|
|
};
|
|
}
|
|
async parseChapter(chapterPath: string): Promise<string> {
|
|
const body = await fetchApi(this.site + chapterPath).then(r => r.text());
|
|
|
|
const loadedCheerio = parseHTML(body);
|
|
|
|
const chapterText = loadedCheerio('div.chapter-content').html() || '';
|
|
|
|
return chapterText;
|
|
}
|
|
async searchNovels(
|
|
searchTerm: string,
|
|
pageNo: number,
|
|
): Promise<Plugin.NovelItem[]> {
|
|
if (pageNo > 1) return [];
|
|
const url = `${this.site}/api/book-search`;
|
|
const formData = new FormData();
|
|
formData.append('keyword', searchTerm);
|
|
|
|
const result: SearchedResult = await fetchApi(url, {
|
|
method: 'POST',
|
|
body: formData,
|
|
}).then(r => r.json());
|
|
|
|
const novels: Plugin.NovelItem[] =
|
|
result.data?.map(item => {
|
|
return {
|
|
name: item.name,
|
|
path: '/truyen/' + item.slug,
|
|
cover: (this.site + item.coverUrl).replace(
|
|
'default.jpg',
|
|
'150.jpg?w=256&q=',
|
|
),
|
|
};
|
|
}) || [];
|
|
return novels;
|
|
}
|
|
}
|
|
|
|
export default new LightNovelVN();
|