Feat: Vite + React (#991)

* remove test_web

* integrate reactjs-vite

* update lint-staged

* simple cors proxy server which handles cookies and redirections

* overrider global fetch with proxy call

* simple examples for set cookies and fetch request

* app bar

* add redux

* loadPlugins.
error: cant load urlencode module

* add ts-node for transforming

* plugin search & select

* plugin slice

* accordion components

* path item & popular novel

* optional for filters

* search novel

* parse novel

* parseChapter

* update cookies

* default filters

* fix: import path

* bundler module resolution

* new json_plugins

* fix: generate multisrc (missing filters)

* fix: host scripts

* fix: plugin code path

* remove typechecking action
This commit is contained in:
nyagami
2024-06-16 18:13:41 +07:00
committed by GitHub
parent a4cb781f32
commit d36e8ec85f
333 changed files with 5857 additions and 28579 deletions
-499
View File
@@ -1,499 +0,0 @@
import { Parser } from 'htmlparser2';
import { HTMLParser2Util, Plugin } from '@typings/plugin';
import { NovelStatus } from '@libs/novelStatus';
import { FilterTypes, Filters } from '@libs/filterInputs';
enum ParseNovelAction {
Unknown = 'Unknown',
GetName = 'GetName',
GetSummary = 'GetSummary',
GetInfos = 'GetInfos',
GetGenres = 'GetGenres',
GetCover = 'GetCover',
GetVolumes = 'GetVolumes',
}
class HakoPlugin implements Plugin.PluginBase {
id = 'ln.hako';
name = 'Hako';
icon = 'src/vi/hakolightnovel/icon.png';
site = 'https://ln.hako.vn';
version = '1.1.0';
parseNovels(url: string) {
return fetch(url)
.then(res => res.text())
.then(html => {
const novels: Plugin.NovelItem[] = [];
let tempNovel = {} as Plugin.NovelItem;
let isGettingUrl = false;
let isParsingNovel = false;
const parser = new Parser({
onopentag(name, attribs) {
if (attribs['class']?.includes('thumb-item-flow')) {
isParsingNovel = true;
}
if (isParsingNovel) {
if (attribs['class']?.includes('series-title')) {
isGettingUrl = true;
}
if (attribs['class']?.includes('img-in-ratio')) {
tempNovel.cover = attribs['data-bg'];
}
if (isGettingUrl && name === 'a') {
tempNovel.name = attribs['title'];
tempNovel.path = attribs['href'];
novels.push(tempNovel);
tempNovel = {} as Plugin.NovelItem; // re-assign new reference
isGettingUrl = false;
isParsingNovel = false;
}
}
},
});
parser.write(html);
parser.end();
return novels;
});
}
popularNovels(
pageNo: number,
{
showLatestNovels,
filters,
}: Plugin.PopularNovelsOptions<typeof this.filters>,
): Promise<Plugin.NovelItem[]> {
let link = this.site + '/danh-sach';
if (filters) {
if (filters.alphabet.value) {
link += '/' + filters.alphabet.value;
}
const params = new URLSearchParams();
for (const novelType of filters.type.value) {
params.append(novelType, '1');
}
for (const status of filters.status.value) {
params.append(status, '1');
}
params.append('sapxep', filters.sort.value);
link += '?' + params.toString() + '&page=' + pageNo;
} else {
link += '?page=' + pageNo;
}
return this.parseNovels(link);
}
parseNovel(novelPath: string): Promise<Plugin.SourceNovel> {
const novel: Plugin.SourceNovel = {
path: novelPath,
name: '',
author: '',
artist: '',
summary: '',
genres: '',
status: '',
};
const chapters: Plugin.ChapterItem[] = [];
const getNameHandler: HTMLParser2Util.Handler = {
isDone: false,
isStarted: false,
onopentag(name, attribs) {
if (name === 'a') {
this.isStarted = true;
}
},
ontext(data) {
novel.name += data;
},
onclosetag(name) {
if (this.isStarted) {
this.isDone = true;
}
},
};
const getSummaryHandler: HTMLParser2Util.Handler & {
newLine: boolean;
} = {
newLine: false,
ontext(data) {
if (this.newLine) {
this.newLine = false;
novel.summary += '\n' + data;
} else {
novel.summary += data;
}
},
onclosetag(name) {
this.newLine = true;
},
};
const getGenresHandler: HTMLParser2Util.Handler = {
ontext(data) {
novel.genres += data;
},
};
enum InfoItem {
Author,
Artist,
Status,
Unknown,
}
const getInfosHandler: HTMLParser2Util.Handler & { info: InfoItem } = {
isStarted: false,
info: InfoItem.Unknown,
onopentag(name, attribs) {
if (attribs['class'] === 'info-item') {
switch (this.info) {
case InfoItem.Unknown:
if (!novel.author) {
this.info = InfoItem.Author;
}
break;
case InfoItem.Author:
this.info = InfoItem.Artist;
break;
case InfoItem.Artist:
this.info = InfoItem.Status;
break;
// we dont need the other info (if exist)
case InfoItem.Status:
this.info = InfoItem.Unknown;
break;
default:
break;
}
}
if (name === 'a') {
this.isStarted = true;
}
},
ontext(data) {
if (this.isStarted) {
switch (this.info) {
case InfoItem.Author:
novel.author += data;
break;
case InfoItem.Artist:
novel.artist += data;
break;
case InfoItem.Status:
novel.status += data;
break;
default:
break;
}
}
},
onclosetag(name) {
if (this.isStarted) {
this.isStarted = false;
}
if (name === 'a' && this.info === InfoItem.Status) {
this.isDone = true;
}
},
};
const getChapterListHandler: HTMLParser2Util.Handler & {
currentVolume: string;
num: number;
part: number;
readingTime: boolean;
tempChapter: Plugin.ChapterItem;
} = {
currentVolume: '',
num: 0,
part: 1,
isStarted: false,
readingTime: false,
tempChapter: {} as Plugin.ChapterItem,
onopentag(name, attribs) {
if (this.isStarted) {
if (name === 'a' && attribs['title'] !== null) {
const chapterName = attribs['title'];
let chapterNumber = Number(
chapterName.match(/Chương\s*(\d+)/i)?.[1],
);
if (chapterNumber) {
if (this.num === chapterNumber) {
chapterNumber = this.num + this.part / 10;
this.part += 1;
} else {
this.num = chapterNumber;
this.part = 1;
}
} else {
chapterNumber = this.num + this.part / 10;
this.part++;
}
this.tempChapter = {
path: attribs['href'],
name: chapterName,
page: this.currentVolume,
chapterNumber: chapterNumber,
};
} else if (attribs['class'] === 'chapter-time') {
this.readingTime = true;
}
}
},
ontext(data) {
if (this.readingTime) {
const chapterTime = data.split('/').map(x => Number(x));
this.tempChapter.releaseTime = new Date(
chapterTime[2],
chapterTime[1],
chapterTime[0],
).toISOString();
chapters.push(this.tempChapter);
this.readingTime = false;
this.tempChapter = {} as Plugin.ChapterItem;
}
},
onclosetag() {
if (this.readingTime) this.readingTime = false;
},
};
const getVolumesHandler: HTMLParser2Util.Handler & {
isParsingChapterList: boolean;
} = {
isStarted: false,
isDone: false,
isParsingChapterList: false,
onopentag(name, attribs) {
if (attribs['class'] === 'sect-title') {
this.isStarted = true;
getChapterListHandler.currentVolume = '';
}
if (name === 'ul') {
getChapterListHandler.isStarted = true;
getChapterListHandler.num = 0;
getChapterListHandler.part = 1;
}
getChapterListHandler.onopentag?.(name, attribs);
},
ontext(data) {
if (this.isStarted) {
getChapterListHandler.currentVolume += data.trim();
}
getChapterListHandler.ontext?.(data);
},
onclosetag(name, isImplied) {
getChapterListHandler.onclosetag?.(name, isImplied);
this.isStarted = false;
if (name === 'ul') {
getChapterListHandler.isStarted = false;
}
},
};
const parseNovelRouter: HTMLParser2Util.HandlerRouter<ParseNovelAction> = {
handlers: {
Unknown: undefined,
GetName: getNameHandler,
GetCover: undefined,
GetSummary: getSummaryHandler,
GetGenres: getGenresHandler,
GetInfos: getInfosHandler,
GetVolumes: getVolumesHandler,
},
action: ParseNovelAction.Unknown,
onopentag(name, attribs) {
if (attribs['class'] === 'series-name') {
this.action = ParseNovelAction.GetName;
} else if (!novel.cover && attribs['class']?.includes('img-in-ratio')) {
const background = attribs['style'];
if (background) {
novel.cover = background.substring(
background.indexOf('http'),
background.length - 2,
);
}
} else if (attribs['class'] === 'summary-content') {
this.action = ParseNovelAction.GetSummary;
} else if (attribs['class'] === 'series-gerne-item') {
this.action = ParseNovelAction.GetGenres;
} else if (attribs['class'] === 'info-item') {
this.action = ParseNovelAction.GetInfos;
} else if (attribs['class']?.includes('volume-list')) {
this.action = ParseNovelAction.GetVolumes;
}
},
onclosetag(name) {
switch (this.action) {
case ParseNovelAction.GetName:
if (this.handlers.GetName?.isDone) {
this.action = ParseNovelAction.Unknown;
}
break;
case ParseNovelAction.GetSummary:
if (name === 'div') {
this.action = ParseNovelAction.Unknown;
}
break;
case ParseNovelAction.GetGenres:
this.action = ParseNovelAction.Unknown;
novel.genres += ',';
break;
case ParseNovelAction.GetInfos:
if (this.handlers.GetInfos?.isDone) {
this.action = ParseNovelAction.Unknown;
}
break;
case ParseNovelAction.GetVolumes:
if (this.handlers.GetVolumes?.isDone) {
this.action = ParseNovelAction.Unknown;
}
default:
break;
}
},
};
return fetch(this.site + novelPath)
.then(res => res.text())
.then(html => {
const parser = new Parser({
onopentag(name, attributes) {
parseNovelRouter.onopentag?.(name, attributes);
if (parseNovelRouter.action) {
parseNovelRouter.handlers[parseNovelRouter.action]?.onopentag?.(
name,
attributes,
);
}
},
ontext(data) {
if (parseNovelRouter.action) {
parseNovelRouter.handlers[parseNovelRouter.action]?.ontext?.(
data,
);
}
},
onclosetag(name, isImplied) {
if (parseNovelRouter.action) {
parseNovelRouter.handlers[parseNovelRouter.action]?.onclosetag?.(
name,
isImplied,
);
}
parseNovelRouter.onclosetag?.(name, isImplied);
},
});
parser.write(html);
parser.end();
novel.chapters = chapters;
switch (novel.status?.trim()) {
case 'Đang tiến hành':
novel.status = NovelStatus.Ongoing;
break;
case 'Tạm ngưng':
novel.status = NovelStatus.OnHiatus;
break;
case 'Completed':
novel.status = NovelStatus.Completed;
break;
default:
novel.status = NovelStatus.Unknown;
}
novel.genres = novel.genres?.replace(/,*\s*$/, '');
novel.name = novel.name.trim();
novel.summary = novel.summary?.trim();
return novel;
});
}
parseChapter(chapterPath: string): Promise<string> {
return fetch(this.site + chapterPath)
.then(res => res.text())
.then(
html =>
html.match(
/(<div id="chapter-content".+?>[^]+)<div style="text-align: center;/,
)?.[1] || 'Không tìm thấy nội dung',
);
}
searchNovels(
searchTerm: string,
pageNo: number,
): Promise<Plugin.NovelItem[]> {
const url =
this.site + '/tim-kiem?keywords=' + searchTerm + '&page=' + pageNo;
return this.parseNovels(url);
}
imageRequestInit: Plugin.ImageRequestInit = {
headers: {
Referer: 'https://ln.hako.vn',
},
};
filters = {
alphabet: {
type: FilterTypes.Picker,
value: '',
label: 'Chữ cái',
options: [
{ label: 'Tất cả', value: '' },
{ label: 'Khác', value: 'khac' },
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' },
{ label: 'C', value: 'c' },
{ label: 'D', value: 'd' },
{ label: 'E', value: 'e' },
{ label: 'F', value: 'f' },
{ label: 'G', value: 'g' },
{ label: 'H', value: 'h' },
{ label: 'I', value: 'i' },
{ label: 'J', value: 'j' },
{ label: 'K', value: 'k' },
{ label: 'L', value: 'l' },
{ label: 'M', value: 'm' },
{ label: 'N', value: 'n' },
{ label: 'O', value: 'o' },
{ label: 'P', value: 'p' },
{ label: 'Q', value: 'q' },
{ label: 'R', value: 'r' },
{ label: 'S', value: 's' },
{ label: 'T', value: 't' },
{ label: 'U', value: 'u' },
{ label: 'V', value: 'v' },
{ label: 'W', value: 'w' },
{ label: 'X', value: 'x' },
{ label: 'Y', value: 'y' },
{ label: 'Z', value: 'z' },
],
},
type: {
type: FilterTypes.CheckboxGroup,
label: 'Phân loại',
value: [],
options: [
{ label: 'Truyện dịch', value: 'truyendich' },
{ label: 'Truyện sáng tác', value: 'sangtac' },
{ label: 'Convert', value: 'convert' },
],
},
status: {
type: FilterTypes.CheckboxGroup,
label: 'Tình trạng',
value: [],
options: [
{ label: 'Đang tiến hành', value: 'dangtienhanh' },
{ label: 'Tạm ngưng', value: 'tamngung' },
{ label: 'Đã hoàn thành', value: 'hoanthanh' },
],
},
sort: {
type: FilterTypes.Picker,
label: 'Sắp xếp',
value: 'top',
options: [
{ label: 'A-Z', value: 'tentruyen' },
{ label: 'Z-A', value: 'tentruyenza' },
{ label: 'Mới cập nhật', value: 'capnhat' },
{ label: 'Truyện mới', value: 'truyenmoi' },
{ label: 'Theo dõi', value: 'theodoi' },
{ label: 'Top toàn thời gian', value: 'top' },
{ label: 'Top tháng', value: 'topthang' },
{ label: 'Số từ', value: 'sotu' },
],
},
} satisfies Filters;
}
export default new HakoPlugin();
-309
View File
@@ -1,309 +0,0 @@
import { CheerioAPI, load as parseHTML } from 'cheerio';
import { fetchApi, fetchFile } from '@libs/fetch';
import { Plugin } from '@typings/plugin';
import { FilterTypes, Filters } from '@libs/filterInputs';
import { NovelStatus } from '@libs/novelStatus';
interface VolumeData {
story: string;
navigation: string;
value: string;
}
interface ChapterSelect {
chap_selector: string;
eps_selector: boolean | string;
}
interface VolumeList {
chapters: Plugin.ChapterItem[];
volumes?: VolumeData[];
}
class TruyenConect implements Plugin.PagePlugin {
id = 'truyenconect';
name = 'Truyen Conect';
icon = 'src/vi/truyenconect/icon.png';
site = 'https://truyenconect.com';
version = '1.0.0';
async sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
parseNovels(loadedCheerio: CheerioAPI, selector: string) {
const novels: Plugin.NovelItem[] = [];
loadedCheerio(selector).each((idx, ele) => {
const url = loadedCheerio(ele).find('a').attr('href');
if (url) {
novels.push({
name: loadedCheerio(ele).find('img').attr('alt') || '',
path: url.replace(this.site, ''),
cover: loadedCheerio(ele).find('img').attr('data-src'),
});
}
});
return novels;
}
async popularNovels(
pageNo: number,
{
filters,
showLatestNovels,
}: Plugin.PopularNovelsOptions<typeof this.filters>,
): Promise<Plugin.NovelItem[]> {
let link = this.site;
let selector = '.c-page__content > .grid9.block .item-thumb.c-image-hover';
if (showLatestNovels) {
selector =
'.c-page__content .page-content-listing.item-big_thumbnail .item-thumb.c-image-hover';
} else if (filters.category.value) {
link += '/' + filters.category.value;
selector =
'table.manga-shortcodes.manga-chapters-listing td[width="10%"]';
if (filters.category.value === 'the-loai') {
selector = '.item-thumb.hover-details.c-image-hover';
link += '/' + filters.genre.value;
}
link += '?page=' + pageNo;
}
const body = await fetch(link).then(r => r.text());
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio, selector);
}
parseChapters(loadedCheerio: CheerioAPI) {
const chapters: Plugin.ChapterItem[] = [];
loadedCheerio('option').each((idx, ele) => {
let url = ele.attribs['value'];
if (!url) return;
const chapterId = url.match(/\/(\d+)-/)?.[1];
if (chapterId) {
url = url.replace(chapterId + '-', '') + '-' + chapterId;
}
let num = url.match(/chuong-(\d+)/)?.[1];
chapters.push({
path: url.replace(this.site, ''),
name: loadedCheerio(ele).text().trim(),
chapterNumber: Number(num) || undefined,
});
});
return chapters.reverse();
}
parseVolumes(loadedCheerio: CheerioAPI) {
const volumes: VolumeData[] = [];
loadedCheerio('option').each((idx, ele) => {
volumes.push({
story: ele.attribs['data-story'],
navigation: ele.attribs['data-navigation'],
value: ele.attribs['value'],
});
});
return volumes;
}
async getVolumes(firstChapterUrl: string) {
const chapterId = firstChapterUrl.match(/-(\d+)$/)?.[1];
if (!chapterId) throw new Error('Không tìm thấy chương');
const url = this.site + '/truyen/get-chap-selector?chap=' + chapterId;
const data: ChapterSelect = await fetch(url).then(r => r.json());
const volumeList: VolumeList = {
chapters: this.parseChapters(parseHTML(data.chap_selector)),
};
if (data.eps_selector) {
volumeList.volumes = this.parseVolumes(
parseHTML(data.eps_selector as string),
);
}
return volumeList;
}
async parseNovel(
novelPath: string,
): Promise<Plugin.SourceNovel & { totalPages: number }> {
const url = this.site + novelPath;
const result = await fetch(url);
const body = await result.text();
let loadedCheerio = parseHTML(body);
const novel: Plugin.SourceNovel & { totalPages: number } = {
name: loadedCheerio('.post-title > h1').text().trim(),
path: novelPath,
chapters: [],
totalPages: 1,
};
novel.cover = loadedCheerio('.summary_image > a > img').attr('data-src');
loadedCheerio('.post-content_item').each(function () {
const detailName = loadedCheerio(this)
.find('.summary-heading > h5')
.text()
.trim();
const detail = loadedCheerio(this).find('.summary-content').html();
if (!detail) return;
switch (detailName) {
case 'Thể loại':
novel.genres = loadedCheerio(detail)
.children('a')
.map((i, el) => loadedCheerio(el).text())
.toArray()
.join(',');
break;
case 'Tác giả':
novel.author = loadedCheerio(detail).text().trim();
break;
case 'Hoạ sĩ':
novel.artist = loadedCheerio(detail).text().trim();
break;
case 'Trạng thái':
const status = detail.trim();
if (status === 'Full') novel.status = NovelStatus.Completed;
else if (status === 'Tạm ngưng') novel.status = NovelStatus.OnHiatus;
else if (status === 'Đang tiến hành')
novel.status = NovelStatus.Ongoing;
else novel.status = NovelStatus.Unknown;
break;
}
});
loadedCheerio('.description-summary > div.summary__content > div').remove();
novel.summary = loadedCheerio('.description-summary > div.summary__content')
.text()
.trim();
const firstChapLink = loadedCheerio('#init-links > a').first().attr('href');
if (!firstChapLink) throw new Error('Không tìm thấy truyện');
await this.sleep(1000);
const volumeList = await this.getVolumes(firstChapLink);
if (volumeList.volumes) {
novel.totalPages = volumeList.volumes.length;
novel.chapters = volumeList.chapters;
} else {
novel.totalPages = 1;
novel.chapters = volumeList.chapters;
}
return novel;
}
async parsePage(novelPath: string, page: string): Promise<Plugin.SourcePage> {
const url = this.site + novelPath;
const result = await fetch(url);
const body = await result.text();
let loadedCheerio = parseHTML(body);
const firstChapLink = loadedCheerio('#init-links > a').first().attr('href');
if (!firstChapLink) throw new Error('Không tìm thấy truyện');
await this.sleep(1000);
const volumeList = await this.getVolumes(firstChapLink);
const volumeIndex = Number(page) - 1;
if (!volumeList.volumes) throw new Error('Không tìm thấy truyện');
if (volumeIndex >= volumeList.volumes.length)
throw new Error('Không tìm thấy volume');
const volume = volumeList.volumes[volumeIndex];
const query = `dataEpisodes=${volume.value}&datastory=${volume.story}&dataNavigation=${encodeURIComponent(volume.navigation)}`;
const chaptersUrl = `${this.site}/truyen/getreadingchapAction?${query}`;
await this.sleep(1000);
const res: { err: number; html: string } = await fetch(chaptersUrl).then(
r => r.json(),
);
if (res.err) throw new Error(res.html);
return {
chapters: this.parseChapters(parseHTML(res.html)),
};
}
async parseChapter(chapterPath: string): Promise<string> {
const result = await fetch(this.site + chapterPath);
const body = await result.text();
const loadedCheerio = parseHTML(body);
let chapterText = loadedCheerio('.reading-content').html() || '';
return chapterText;
}
async searchNovels(
searchTerm: string,
pageNo: number,
): Promise<Plugin.NovelItem[]> {
const url = `${this.site}?key=${searchTerm}&page=${pageNo}`;
const result = await fetch(url);
const body = await result.text();
const loadedCheerio = parseHTML(body);
const novels: Plugin.NovelItem[] = [];
loadedCheerio('.tab-thumb.c-image-hover > a').each((idx, ele) => {
const novelName = ele.attribs['title'];
const novelCover = loadedCheerio(ele).find('img').first().attr('src');
const novelUrl = ele.attribs['href'];
if (!novelUrl) return;
novels.push({
name: novelName,
path: novelUrl.replace(this.site, ''),
cover: novelCover,
});
});
return novels;
}
filters = {
category: {
label: 'Lọc theo',
value: '',
type: FilterTypes.Picker,
options: [
{ label: '', value: '' },
{ label: 'Thể loại', value: 'the-loai' },
{ label: 'Truyện dịch', value: 'truyen-dich' },
{ label: 'Truyện convert', value: 'convert' },
],
},
genre: {
label: 'Thể loại',
value: 'action',
type: FilterTypes.Picker,
options: [
{ label: 'Action', value: 'action' },
{ label: 'Adult', value: 'adult' },
{ label: 'Adventure', value: 'adventure' },
{ label: 'Chinese novel', value: 'chinese-novel' },
{ label: 'Chuyển Sinh', value: 'chuyen-sinh' },
{ label: 'English Novel', value: 'english-novel' },
{ label: 'Harem', value: 'harem' },
{ label: 'Ecchi', value: 'ecchi' },
{ label: 'Fantasy', value: 'fantasy' },
{ label: 'Drama', value: 'drama' },
{ label: 'Game', value: 'game' },
{ label: 'Tiên hiệp', value: 'tien-hiep' },
{ label: 'Kiếm Hiệp', value: 'kiem-hiep' },
{ label: 'Ngôn Tình', value: 'ngon-tinh' },
{ label: 'Isekai', value: 'isekai' },
{ label: 'Lịch Sử', value: 'lich-su' },
{ label: 'Web Novel', value: 'web-novel' },
{ label: 'Xuyên không', value: 'xuyen-khong' },
{ label: 'Trọng sinh', value: 'trong-sinh' },
{ label: 'Trinh thám', value: 'trinh-tham' },
{ label: 'Dị giới', value: 'di-gioi' },
{ label: 'Huyền ảo', value: 'huyen-ao' },
{ label: 'Sắc Hiệp', value: 'sac-hiep' },
{ label: 'Dị năng', value: 'di-nang' },
{ label: 'Linh dị', value: 'linh-di' },
{ label: 'Đô thị', value: 'do-thi' },
{ label: 'Comedy', value: 'comedy' },
{ label: 'School Life', value: 'school-life' },
{ label: 'Romance', value: 'romance' },
{ label: 'Martial-arts', value: 'martial-arts' },
{ label: 'Light Novel', value: 'light-novel' },
{ label: 'Huyền huyễn', value: 'huyen-huyen' },
{ label: 'Kỳ Huyễn', value: 'ky-huyen' },
{ label: 'Khoa Huyễn', value: 'khoa-huyen' },
{ label: 'Võng Du', value: 'vong-du' },
{ label: 'Đồng Nhân', value: 'dong-nhan' },
],
},
} satisfies Filters;
}
export default new TruyenConect();
-175
View File
@@ -1,175 +0,0 @@
import { CheerioAPI, load as parseHTML } from 'cheerio';
import { fetchApi, fetchFile } from '@libs/fetch';
import { Plugin } from '@typings/plugin';
import { Filters } from '@libs/filterInputs';
import { NovelStatus } from '@libs/novelStatus';
import { NovelItem } from '../../test_web/static/js';
interface SearchedNovel {
name: string;
slug: string;
coverUrl: string;
}
interface 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,
options: Plugin.PopularNovelsOptions<Filters>,
): Promise<Plugin.NovelItem[]> {
const url = `${this.site}/truyen-hot-ds?page=${pageNo}`;
const body = await fetch(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 fetch(url).then(r => r.text());
let 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);
let 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`;
let formData = new FormData();
formData.append('keyword', searchTerm);
const result: SearchedResult = await fetchApi(url, {
method: 'POST',
body: formData,
}).then(r => r.json());
const novels: 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();
-123
View File
@@ -1,123 +0,0 @@
import { fetchFile } from '@libs/fetch';
import { Filters } from '@libs/filterInputs';
import { NovelStatus } from '@libs/novelStatus';
import { Plugin } from '@typings/plugin';
import { CheerioAPI, load as parseHTML } from 'cheerio';
class Nettruyen implements Plugin.PagePlugin {
id = 'nettruyen';
name = 'Nettruyen';
icon = 'src/vi/nettruyen/icon.png';
site = 'https://nettruyen.com.vn';
version = '1.0.0';
parseNovels(loadedCheerio: CheerioAPI) {
const novels: Plugin.NovelItem[] = [];
loadedCheerio('#listchuong > ul > li > a.thumb').each((idx, ele) => {
const path = ele.attribs['href'];
if (!path) return;
const name = ele.attribs['title'];
const cover =
this.site + '/' + loadedCheerio(ele).find('img').first().attr('src');
novels.push({
path: '/' + path,
name,
cover,
});
});
return novels;
}
async popularNovels(
pageNo: number,
options: Plugin.PopularNovelsOptions<Filters>,
): Promise<Plugin.NovelItem[]> {
const url = `${this.site}/xem-nhieu/trang-${pageNo}.html`;
const body = await fetch(url).then(r => r.text());
return this.parseNovels(parseHTML(body));
}
parseChapters(loadedCheerio: CheerioAPI) {
const chapters: Plugin.ChapterItem[] = [];
loadedCheerio('ul.list-unstyled > li > a').each((idx, ele) => {
const path = ele.attribs['href'];
if (!path) return;
const name = ele.attribs['title'];
chapters.push({
path: '/' + path,
name,
chapterNumber: Number(name.match(/Chương (\d+)/)?.[1]),
});
});
return chapters;
}
async parseNovel(
novelPath: string,
): Promise<Plugin.SourceNovel & { totalPages: number }> {
const url = this.site + novelPath;
const body = await fetch(url).then(r => r.text());
const loadedCheerio = parseHTML(body);
const novel: Plugin.SourceNovel & { totalPages: number } = {
path: novelPath,
name: loadedCheerio('.gioithieutruyen.profile-info > h1').text().trim(),
chapters: [],
totalPages:
Number(loadedCheerio('.phantrang > span:nth-child(2)').text()) || 1,
};
novel.cover =
this.site + '/' + loadedCheerio('.img-thumb > img').attr('src');
loadedCheerio('.thongtintruyen.note.note-info').each((idx, ele) => {
const strong = loadedCheerio(ele).find('strong').text().trim();
switch (strong) {
case 'Tác giả':
novel.author = loadedCheerio(ele)
.text()
.replace(/Tác giả\s+\n?:/, '');
break;
case 'Trạng thái':
const text = loadedCheerio(ele).text();
if (text.includes('Đang ra')) {
novel.status = NovelStatus.Ongoing;
} else if (text.includes('Hoàn thành')) {
novel.status = NovelStatus.Completed;
} else {
novel.status = NovelStatus.Unknown;
}
break;
case 'Thể loại':
novel.genres = loadedCheerio('a > span')
.toArray()
.map(gEle => loadedCheerio(gEle).text())
.join(',');
break;
default:
break;
}
});
novel.summary = loadedCheerio('.gioithieutruyen > .gioithieutruyen')
.text()
.trim();
novel.chapters = this.parseChapters(loadedCheerio);
return novel;
}
async parsePage(novelPath: string, page: string): Promise<Plugin.SourcePage> {
const id = novelPath.match(/-(\d+)\//)?.[1];
if (!id) throw new Error('Cant parse page');
const url = `${this.site}/ajax.chuong.php?id=${id}&page=${page}&url=${novelPath.replace(/\//g, '')}&loai=truyendich`;
const body = await fetch(url).then(r => r.text());
const chapters = this.parseChapters(parseHTML(body));
return {
chapters,
};
}
async parseChapter(chapterPath: string): Promise<string> {
const url = this.site + chapterPath;
const body = await fetch(url).then(r => r.text());
const loadedCheerio = parseHTML(body);
return loadedCheerio('#noidungchap').html() || '';
}
async searchNovels(
searchTerm: string,
pageNo: number,
): Promise<Plugin.NovelItem[]> {
throw new Error('Method not implemented.');
}
}
export default new Nettruyen();
-156
View File
@@ -1,156 +0,0 @@
import { CheerioAPI, load as parseHTML } from 'cheerio';
import { fetchFile } from '@libs/fetch';
import { Plugin } from '@typings/plugin';
import { Filters } from '@libs/filterInputs';
import { NovelStatus } from '@libs/novelStatus';
class TruyenFull implements Plugin.PagePlugin {
id = 'truyenchu';
name = 'Truyện Chữ';
icon = 'src/vi/truyenchu/icon.png';
site = 'https://truyenchu.vn';
version = '1.0.0';
parseNovels(loadedCheerio: CheerioAPI) {
const novels: Plugin.NovelItem[] = [];
loadedCheerio('.list-truyen .row').each((idx, ele) => {
const novelName = loadedCheerio(ele).find('h3.truyen-title > a').text();
const novelCover =
this.site +
loadedCheerio(ele)
.find("div[data-classname='cover']")
.attr('data-image');
const novelUrl = loadedCheerio(ele)
.find('h3.truyen-title > a')
.attr('href');
if (novelUrl) {
novels.push({
name: novelName,
cover: novelCover,
path: novelUrl.replace(this.site, ''),
});
}
});
return novels;
}
parseChapters(html: string) {
const listChapterHTML = html.match(
/("list_chapter":\s?\\{0}"(.+)\\{0}"),/,
)?.[1];
if (!listChapterHTML) throw new Error('Không tải được chương');
const listChapter = JSON.parse(`{${listChapterHTML}}`);
const loadedChapterList = parseHTML(listChapter.list_chapter);
const chapters: Plugin.ChapterItem[] = [];
loadedChapterList('ul > li > a').each((idx, ele) => {
const path = ele.attribs['href'].replace(this.site, '');
if (path) {
chapters.push({
name: ele.attribs['title'],
path,
chapterNumber: Number(path.match(/\/chuong-(\d+)/)?.[1]),
});
}
});
return chapters;
}
async popularNovels(
pageNo: number,
options: Plugin.PopularNovelsOptions<Filters>,
): Promise<Plugin.NovelItem[]> {
const url = `${this.site}/danh-sach/truyen-hot?page=${pageNo}`;
const result = await fetch(url);
const body = await result.text();
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
async parseNovel(
novelPath: string,
): Promise<Plugin.SourceNovel & { totalPages: number }> {
const url = this.site + novelPath;
const result = await fetch(url);
const body = await result.text();
let loadedCheerio = parseHTML(body);
let lastPage = 1;
const regex = new RegExp(`${novelPath}\\?page=\\d+`, 'g');
body.match(regex)?.forEach(v => {
const page = Number(v.match(/\?page=(\d+)/)?.[1]);
if (page && page > lastPage) {
lastPage = page;
}
});
const novel: Plugin.SourceNovel & { totalPages: number } = {
path: novelPath,
name: loadedCheerio('div.book > img').attr('alt') || 'Không có tiêu đề',
chapters: [],
totalPages: lastPage,
};
novel.cover = this.site + loadedCheerio('div.book > img').attr('src');
novel.summary = loadedCheerio('div.desc-text').text().trim();
novel.author = loadedCheerio('h3:contains("Tác giả:")')
.parent()
.contents()
.text()
.replace('Tác giả:', '');
novel.genres = loadedCheerio('h3:contains("Thể loại")')
.siblings()
.map((i, el) => loadedCheerio(el).text())
.toArray()
.join(',');
novel.status = loadedCheerio('h3:contains("Trạng thái")').next().text();
if (novel.status === 'Full') {
novel.status = NovelStatus.Completed;
} else if (novel.status === 'Đang ra') {
novel.status = NovelStatus.Ongoing;
} else {
novel.status = NovelStatus.Unknown;
}
novel.chapters = this.parseChapters(body);
return novel;
}
async parsePage(novelPath: string, page: string): Promise<Plugin.SourcePage> {
const url = `${this.site}${novelPath}?page=${page}`;
const result = await fetch(url);
const body = await result.text();
const chapters = this.parseChapters(body);
return {
chapters,
};
}
async parseChapter(chapterPath: string): Promise<string> {
const result = await fetch(this.site + chapterPath);
const body = await result.text();
const loadedCheerio = parseHTML(body);
let chapterText =
(loadedCheerio('.chapter-title').html() || '') +
(loadedCheerio('#chapter-c').html() || '');
return chapterText;
}
async searchNovels(
searchTerm: string,
pageNo: number,
): Promise<Plugin.NovelItem[]> {
const searchUrl = `${this.site}/tim-kiem?tukhoa=${searchTerm}&page=${pageNo}`;
const result = await fetch(searchUrl);
const body = await result.text();
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
}
export default new TruyenFull();
-147
View File
@@ -1,147 +0,0 @@
import { CheerioAPI, load as parseHTML } from 'cheerio';
import { fetchFile } from '@libs/fetch';
import { Plugin } from '@typings/plugin';
import { Filters } from '@libs/filterInputs';
import { NovelStatus } from '@libs/novelStatus';
class TruyenFull implements Plugin.PagePlugin {
id = 'truyenfull';
name = 'Truyện Full';
icon = 'src/vi/truyenfull/icon.png';
site = 'https://truyenfull.vn';
version = '1.0.0';
parseNovels(loadedCheerio: CheerioAPI) {
const novels: Plugin.NovelItem[] = [];
loadedCheerio('.list-truyen .row').each((idx, ele) => {
const novelName = loadedCheerio(ele).find('h3.truyen-title > a').text();
const novelCover = loadedCheerio(ele)
.find("div[data-classname='cover']")
.attr('data-image');
const novelUrl = loadedCheerio(ele)
.find('h3.truyen-title > a')
.attr('href');
if (novelUrl) {
novels.push({
name: novelName,
cover: novelCover,
path: novelUrl.replace(this.site, ''),
});
}
});
return novels;
}
parseChapters(loadedCheerio: CheerioAPI): Plugin.ChapterItem[] {
return loadedCheerio('ul.list-chapter > li > a')
.toArray()
.map(ele => {
const path = ele.attribs['href'].replace(this.site, '');
return {
name: loadedCheerio(ele).text().trim(),
path,
chapterNumber: Number(path.match(/\/chuong-(\d+)\//)?.[1]),
};
});
}
async popularNovels(
pageNo: number,
options: Plugin.PopularNovelsOptions<Filters>,
): Promise<Plugin.NovelItem[]> {
const url = `${this.site}/danh-sach/truyen-hot/trang-${pageNo}/`;
const result = await fetch(url);
const body = await result.text();
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
async parseNovel(
novelPath: string,
): Promise<Plugin.SourceNovel & { totalPages: number }> {
const url = this.site + novelPath;
const result = await fetch(url);
const body = await result.text();
let loadedCheerio = parseHTML(body);
let lastPage = 1;
loadedCheerio('ul.pagination.pagination-sm > li > a').each(function () {
const page = Number(this.attribs['href'].match(/\/trang-(\d+)\//)?.[1]);
if (page && page > lastPage) lastPage = page;
});
const novel: Plugin.SourceNovel & { totalPages: number } = {
path: novelPath,
name: loadedCheerio('div.book > img').attr('alt') || 'Không có tiêu đề',
chapters: [],
totalPages: lastPage,
};
novel.cover = loadedCheerio('div.book > img').attr('src');
novel.summary = loadedCheerio('div.desc-text').text().trim();
novel.author = loadedCheerio('h3:contains("Tác giả:")')
.parent()
.contents()
.text()
.replace('Tác giả:', '');
novel.genres = loadedCheerio('h3:contains("Thể loại")')
.siblings()
.map((i, el) => loadedCheerio(el).text())
.toArray()
.join(',');
novel.status = loadedCheerio('h3:contains("Trạng thái")').next().text();
if (novel.status === 'Full') {
novel.status = NovelStatus.Completed;
} else if (novel.status === 'Đang ra') {
novel.status = NovelStatus.Ongoing;
} else {
novel.status = NovelStatus.Unknown;
}
novel.chapters = this.parseChapters(loadedCheerio);
return novel;
}
async parsePage(novelPath: string, page: string): Promise<Plugin.SourcePage> {
const url = `${this.site}${novelPath}trang-${page}/#list-chapter`;
const result = await fetch(url);
const body = await result.text();
const loadedCheerio = parseHTML(body);
const chapters = this.parseChapters(loadedCheerio);
return {
chapters,
};
}
async parseChapter(chapterPath: string): Promise<string> {
const result = await fetch(this.site + chapterPath);
const body = await result.text();
const loadedCheerio = parseHTML(body);
let chapterText =
(loadedCheerio('.chapter-title').html() || '') +
(loadedCheerio('#chapter-c').html() || '');
return chapterText;
}
async searchNovels(
searchTerm: string,
pageNo: number,
): Promise<Plugin.NovelItem[]> {
const searchUrl = `${this.site}/tim-kiem?tukhoa=${searchTerm}&page=${pageNo}`;
const result = await fetch(searchUrl);
const body = await result.text();
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
}
}
export default new TruyenFull();