Lightnovelpub: Refactor with state enum (#1644)
* refactor * parseChapter
This commit is contained in:
@@ -7,7 +7,7 @@ import dayjs from 'dayjs';
|
|||||||
class LightNovelPub implements Plugin.PagePlugin {
|
class LightNovelPub implements Plugin.PagePlugin {
|
||||||
id = 'lightnovelpub';
|
id = 'lightnovelpub';
|
||||||
name = 'LightNovelPub';
|
name = 'LightNovelPub';
|
||||||
version = '2.1.0';
|
version = '2.2.0';
|
||||||
icon = 'src/en/lightnovelpub/icon.png';
|
icon = 'src/en/lightnovelpub/icon.png';
|
||||||
site = 'https://www.lightnovelpub.com/';
|
site = 'https://www.lightnovelpub.com/';
|
||||||
headers = {
|
headers = {
|
||||||
@@ -17,37 +17,39 @@ class LightNovelPub implements Plugin.PagePlugin {
|
|||||||
|
|
||||||
parseNovels(html: string) {
|
parseNovels(html: string) {
|
||||||
const novels: Plugin.NovelItem[] = [];
|
const novels: Plugin.NovelItem[] = [];
|
||||||
let tempNovel = {} as Plugin.NovelItem;
|
let tempNovel: Partial<Plugin.NovelItem> = {};
|
||||||
let isParsingNovel = false;
|
let state: ParsingState = ParsingState.Idle;
|
||||||
const parser = new Parser({
|
const parser = new Parser({
|
||||||
onopentag(name, attribs) {
|
onopentag(name, attribs) {
|
||||||
if (isParsingNovel) {
|
if (attribs['class'] === 'novel-item') {
|
||||||
if (name === 'a') {
|
state = ParsingState.Novel;
|
||||||
|
}
|
||||||
|
if (state !== ParsingState.Novel) return;
|
||||||
|
|
||||||
|
switch (name) {
|
||||||
|
case 'a':
|
||||||
tempNovel.path = attribs['href'].slice(1);
|
tempNovel.path = attribs['href'].slice(1);
|
||||||
tempNovel.name = attribs['title'];
|
tempNovel.name = attribs['title'];
|
||||||
}
|
break;
|
||||||
if (name === 'img') {
|
case 'img':
|
||||||
tempNovel.cover = attribs['data-src'] || attribs['src'];
|
tempNovel.cover = attribs['data-src'] || attribs['src'];
|
||||||
}
|
break;
|
||||||
if (tempNovel.path && tempNovel.cover) {
|
|
||||||
novels.push(tempNovel);
|
|
||||||
tempNovel = {} as Plugin.NovelItem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onattribute(name, value) {
|
|
||||||
if (name === 'class' && value === 'novel-item') {
|
|
||||||
isParsingNovel = true;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onclosetag(name) {
|
onclosetag(name) {
|
||||||
if (name === 'li') {
|
if (name === 'li') {
|
||||||
isParsingNovel = false;
|
if (tempNovel.path && tempNovel.cover) {
|
||||||
|
novels.push(tempNovel as Plugin.NovelItem);
|
||||||
|
tempNovel = {};
|
||||||
|
}
|
||||||
|
state = ParsingState.Idle;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.write(html);
|
parser.write(html);
|
||||||
parser.end();
|
parser.end();
|
||||||
|
|
||||||
return novels;
|
return novels;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,13 +57,15 @@ class LightNovelPub implements Plugin.PagePlugin {
|
|||||||
page: number,
|
page: number,
|
||||||
{ filters }: Plugin.PopularNovelsOptions<typeof this.filters>,
|
{ filters }: Plugin.PopularNovelsOptions<typeof this.filters>,
|
||||||
): Promise<Plugin.NovelItem[]> {
|
): Promise<Plugin.NovelItem[]> {
|
||||||
let link = `${this.site}browse/`;
|
const linkParts = [
|
||||||
link += `${filters.genres.value}/`;
|
this.site + 'browse',
|
||||||
link += `${filters.order.value}/`;
|
filters.genres.value,
|
||||||
link += `${filters.status.value}/`;
|
filters.order.value,
|
||||||
link += page;
|
filters.status.value,
|
||||||
|
page.toString(),
|
||||||
|
];
|
||||||
|
|
||||||
const body = await fetchApi(link).then(r => r.text());
|
const body = await fetchApi(linkParts.join('/')).then(r => r.text());
|
||||||
|
|
||||||
return this.parseNovels(body);
|
return this.parseNovels(body);
|
||||||
}
|
}
|
||||||
@@ -70,186 +74,305 @@ class LightNovelPub implements Plugin.PagePlugin {
|
|||||||
novelPath: string,
|
novelPath: string,
|
||||||
): Promise<Plugin.SourceNovel & { totalPages: number }> {
|
): Promise<Plugin.SourceNovel & { totalPages: number }> {
|
||||||
const body = await fetchApi(this.site + novelPath).then(r => r.text());
|
const body = await fetchApi(this.site + novelPath).then(r => r.text());
|
||||||
const novel: Plugin.SourceNovel & { totalPages: number } = {
|
const novel: Partial<Plugin.SourceNovel> & Partial<{ totalPages: number }> =
|
||||||
path: novelPath,
|
{
|
||||||
name: '',
|
path: novelPath,
|
||||||
summary: '',
|
chapters: [],
|
||||||
totalPages: 1,
|
};
|
||||||
chapters: [],
|
let state: ParsingState = ParsingState.Idle;
|
||||||
};
|
const summaryParts: string[] = [];
|
||||||
let isHeaderStats = false;
|
|
||||||
let isStatus = false;
|
|
||||||
let isTotalChapters = false;
|
|
||||||
let isNovelName = false;
|
|
||||||
let isAuthorName = false;
|
|
||||||
let isSummary = false;
|
|
||||||
let isGenres = false;
|
|
||||||
let isTags = false;
|
|
||||||
const genreArray: string[] = [];
|
const genreArray: string[] = [];
|
||||||
let isCover = false;
|
|
||||||
const parser = new Parser({
|
const parser = new Parser({
|
||||||
onopentag(name, attribs) {
|
onopentag(name, attribs) {
|
||||||
if (name === 'h1' && attribs['class']?.includes('novel-title')) {
|
switch (name) {
|
||||||
isNovelName = true;
|
case 'h1':
|
||||||
}
|
if (attribs['class']?.includes('novel-title')) {
|
||||||
if (name === 'div' && attribs['class']?.includes('content')) {
|
state = ParsingState.NovelName;
|
||||||
isSummary = true;
|
}
|
||||||
}
|
break;
|
||||||
if (name === 'figure' && attribs['class'] === 'cover') {
|
case 'figure':
|
||||||
isCover = true;
|
if (attribs['class'] === 'cover') {
|
||||||
}
|
state = ParsingState.Cover;
|
||||||
if (isCover) {
|
}
|
||||||
if (name === 'img') {
|
break;
|
||||||
novel.cover = attribs['data-src'] || attribs['src'];
|
case 'img':
|
||||||
}
|
if (state === ParsingState.Cover) {
|
||||||
}
|
novel.cover = attribs['data-src'] || attribs['src'];
|
||||||
},
|
}
|
||||||
onopentagname(name) {
|
break;
|
||||||
if (isHeaderStats && name === 'strong') {
|
case 'strong':
|
||||||
isStatus = true;
|
if (state === ParsingState.HeaderStats) {
|
||||||
}
|
if (attribs['class']) {
|
||||||
if (isSummary && name === 'br') {
|
state = ParsingState.Status;
|
||||||
novel.summary += '\n';
|
} else {
|
||||||
}
|
state = ParsingState.TotalChapters;
|
||||||
if (isGenres && name === 'a') {
|
}
|
||||||
isTags = true;
|
}
|
||||||
}
|
break;
|
||||||
},
|
case 'br':
|
||||||
onattribute(name, value) {
|
if (state === ParsingState.Summary) {
|
||||||
if (name === 'class' && value === 'header-stats') {
|
summaryParts.push('<LINE_BREAK>');
|
||||||
isTotalChapters = true;
|
}
|
||||||
isHeaderStats = true;
|
break;
|
||||||
}
|
case 'a':
|
||||||
if (name === 'itemprop' && value === 'author') {
|
if (state === ParsingState.Genres) {
|
||||||
isAuthorName = true;
|
state = ParsingState.Tags;
|
||||||
}
|
}
|
||||||
if (name === 'class' && value === 'categories') {
|
break;
|
||||||
isGenres = true;
|
case 'div':
|
||||||
|
if (attribs['class']) {
|
||||||
|
if (attribs['class'].includes('content')) {
|
||||||
|
state = ParsingState.Summary;
|
||||||
|
} else {
|
||||||
|
const map: Record<string, ParsingState> = {
|
||||||
|
'categories': ParsingState.Genres,
|
||||||
|
'header-stats': ParsingState.HeaderStats,
|
||||||
|
'expand': ParsingState.Idle,
|
||||||
|
};
|
||||||
|
state = map[attribs['class']] ?? state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (attribs['itemprop'] === 'author') {
|
||||||
|
state = ParsingState.AuthorName;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ontext(data) {
|
ontext(data) {
|
||||||
if (isTotalChapters) {
|
switch (state) {
|
||||||
novel.totalPages = Math.ceil(parseInt(data, 10) / 100);
|
case ParsingState.TotalChapters:
|
||||||
}
|
if (!novel.totalPages) {
|
||||||
if (isStatus) {
|
novel.totalPages = Math.ceil(parseInt(data, 10) / 100);
|
||||||
novel.status = data.trim();
|
}
|
||||||
}
|
break;
|
||||||
if (isNovelName) {
|
case ParsingState.Status:
|
||||||
novel.name += data.trim();
|
novel.status = data.trim();
|
||||||
}
|
break;
|
||||||
if (isAuthorName) {
|
case ParsingState.NovelName:
|
||||||
novel.author = data;
|
novel.name = (novel.name || '') + data.trim();
|
||||||
}
|
break;
|
||||||
if (isSummary) {
|
case ParsingState.AuthorName:
|
||||||
novel.summary += data;
|
novel.author = data;
|
||||||
}
|
break;
|
||||||
if (isTags) {
|
case ParsingState.Summary:
|
||||||
genreArray.push(data);
|
summaryParts.push(data);
|
||||||
|
break;
|
||||||
|
case ParsingState.Tags:
|
||||||
|
genreArray.push(data);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onclosetag(name) {
|
onclosetag(name) {
|
||||||
if (name === 'strong') {
|
switch (name) {
|
||||||
isTotalChapters = false;
|
case 'strong':
|
||||||
isStatus = false;
|
if (state === ParsingState.TotalChapters) {
|
||||||
}
|
state = ParsingState.HeaderStats;
|
||||||
if (name === 'i') {
|
} else if (state === ParsingState.Status) {
|
||||||
isStatus = false;
|
state = ParsingState.Idle;
|
||||||
}
|
}
|
||||||
if (name === 'h1') {
|
break;
|
||||||
isNovelName = false;
|
case 'i':
|
||||||
}
|
if (state === ParsingState.Status) {
|
||||||
if (name === 'span') {
|
state = ParsingState.Idle;
|
||||||
isAuthorName = false;
|
}
|
||||||
}
|
break;
|
||||||
if (name === 'div') {
|
case 'h1':
|
||||||
isHeaderStats = false;
|
if (state === ParsingState.NovelName) {
|
||||||
isSummary = false;
|
state = ParsingState.Idle;
|
||||||
isGenres = false;
|
}
|
||||||
}
|
break;
|
||||||
if (name === 'a') {
|
case 'span':
|
||||||
isTags = false;
|
if (state === ParsingState.AuthorName) {
|
||||||
}
|
state = ParsingState.Idle;
|
||||||
if (name === 'figure') {
|
}
|
||||||
isCover = false;
|
break;
|
||||||
|
case 'div':
|
||||||
|
if (
|
||||||
|
state === ParsingState.Summary ||
|
||||||
|
state === ParsingState.Genres
|
||||||
|
) {
|
||||||
|
state = ParsingState.Idle;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
if (state === ParsingState.Tags) {
|
||||||
|
state = ParsingState.Genres;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'figure':
|
||||||
|
if (state === ParsingState.Cover) {
|
||||||
|
state = ParsingState.Idle;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
if (state === ParsingState.Summary) {
|
||||||
|
summaryParts.push('<PARAGRAPH_BREAK>');
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onend() {
|
||||||
|
const text = summaryParts
|
||||||
|
.join('')
|
||||||
|
.replace(/<PARAGRAPH_BREAK>/g, '\n\n')
|
||||||
|
.replace(/<LINE_BREAK>/g, '\n')
|
||||||
|
.replace(/\r\n/g, '\n')
|
||||||
|
.replace(/ /g, ' ');
|
||||||
|
|
||||||
|
const paragraphs = text
|
||||||
|
.split('\n\n')
|
||||||
|
.map(p => p.trim().replace(/[ \t]+/g, ' '))
|
||||||
|
.filter(p => p.length > 0);
|
||||||
|
|
||||||
|
novel.summary = paragraphs.join('\n\n');
|
||||||
|
summaryParts.length = 0;
|
||||||
|
|
||||||
|
novel.genres = genreArray.join(', ');
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.write(body);
|
parser.write(body);
|
||||||
parser.end();
|
parser.end();
|
||||||
novel.genres = genreArray.join(', ');
|
|
||||||
return novel;
|
return novel as Plugin.SourceNovel & { totalPages: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
async parsePage(novelPath: string, page: string): Promise<Plugin.SourcePage> {
|
async parsePage(novelPath: string, page: string): Promise<Plugin.SourcePage> {
|
||||||
const url = this.site + novelPath + '/chapters/page-' + page;
|
const url = this.site + novelPath + '/chapters/page-' + page;
|
||||||
const body = await fetchApi(url).then(res => res.text());
|
const body = await fetchApi(url).then(res => res.text());
|
||||||
const chapter: Plugin.ChapterItem[] = [];
|
const chapters: Plugin.ChapterItem[] = [];
|
||||||
let tempChapter = {} as Plugin.ChapterItem;
|
let tempChapter: Partial<Plugin.ChapterItem> = {};
|
||||||
let isChapterItem = false;
|
let state: ParsingState = ParsingState.Idle;
|
||||||
let isChapterList = false;
|
|
||||||
const parser = new Parser({
|
const parser = new Parser({
|
||||||
onopentag(name, attribs) {
|
onopentag(name, attribs) {
|
||||||
if (isChapterList) {
|
if (attribs['class'] === 'chapter-list') {
|
||||||
if (name === 'li') {
|
state = ParsingState.ChapterList;
|
||||||
isChapterItem = true;
|
return;
|
||||||
tempChapter.chapterNumber = Number(attribs['data-orderno']);
|
|
||||||
}
|
|
||||||
if (isChapterItem) {
|
|
||||||
if (name === 'a') {
|
|
||||||
tempChapter.name = attribs['title'];
|
|
||||||
tempChapter.path = attribs['href'].slice(1);
|
|
||||||
}
|
|
||||||
if (name === 'time') {
|
|
||||||
tempChapter.releaseTime = dayjs(
|
|
||||||
attribs['datetime'],
|
|
||||||
).toISOString();
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
tempChapter.chapterNumber &&
|
|
||||||
tempChapter.path &&
|
|
||||||
tempChapter.releaseTime
|
|
||||||
) {
|
|
||||||
chapter.push(tempChapter);
|
|
||||||
tempChapter = {} as Plugin.ChapterItem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
onattribute(name, value) {
|
switch (state) {
|
||||||
if (name === 'class' && value === 'chapter-list') {
|
case ParsingState.ChapterList:
|
||||||
isChapterList = true;
|
if (name === 'li') {
|
||||||
|
state = ParsingState.ChapterItem;
|
||||||
|
tempChapter = {
|
||||||
|
chapterNumber: Number(attribs['data-orderno'] || 0),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ParsingState.ChapterItem:
|
||||||
|
switch (name) {
|
||||||
|
case 'a':
|
||||||
|
tempChapter.name = attribs['title'];
|
||||||
|
tempChapter.path = attribs['href']?.slice(1);
|
||||||
|
break;
|
||||||
|
case 'time':
|
||||||
|
tempChapter.releaseTime = dayjs(
|
||||||
|
attribs['datetime'],
|
||||||
|
).toISOString();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onclosetag(name) {
|
onclosetag(name) {
|
||||||
if (name === 'a') {
|
switch (state) {
|
||||||
isChapterItem = false;
|
case ParsingState.ChapterItem:
|
||||||
}
|
if (name === 'li') {
|
||||||
if (name === 'ul') {
|
if (
|
||||||
isChapterList = false;
|
tempChapter.chapterNumber !== undefined &&
|
||||||
|
tempChapter.path &&
|
||||||
|
tempChapter.releaseTime
|
||||||
|
) {
|
||||||
|
chapters.push(tempChapter as Plugin.ChapterItem);
|
||||||
|
}
|
||||||
|
state = ParsingState.ChapterList;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ParsingState.ChapterList:
|
||||||
|
if (name === 'ul') {
|
||||||
|
state = ParsingState.Idle;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.write(body);
|
parser.write(body);
|
||||||
parser.end();
|
parser.end();
|
||||||
const chapters = chapter;
|
|
||||||
return { chapters };
|
return { chapters };
|
||||||
}
|
}
|
||||||
|
|
||||||
async parseChapter(chapterPath: string): Promise<string> {
|
async parseChapter(chapterPath: string): Promise<string> {
|
||||||
const html = await fetchApi(this.site + chapterPath).then(r => r.text());
|
const html = await fetchApi(this.site + chapterPath).then(r => r.text());
|
||||||
const parts: string[] = [];
|
|
||||||
const regexPatterns: RegExp[] = [
|
let depth = 0;
|
||||||
/(<div id="chapter-container[^]+?)<div class="chapternav/,
|
let state: ParsingState = ParsingState.Idle;
|
||||||
];
|
const chapterHtml: string[] = [];
|
||||||
const extractContent = (patterns: RegExp[]) => {
|
|
||||||
patterns.forEach(regex => {
|
type EscapeChar = '&' | '<' | '>' | '"' | "'";
|
||||||
const match = html.match(regex)?.[1];
|
const escapeRegex = /[&<>"']/g;
|
||||||
if (match) parts.push(match);
|
const escapeMap: Record<EscapeChar, string> = {
|
||||||
});
|
'&': '&',
|
||||||
|
'<': '<',
|
||||||
|
'>': '>',
|
||||||
|
'"': '"',
|
||||||
|
"'": ''',
|
||||||
};
|
};
|
||||||
extractContent(regexPatterns);
|
const escapeHtml = (text: string): string =>
|
||||||
const chapterText = parts.join();
|
text.replace(escapeRegex, char => escapeMap[char as EscapeChar]);
|
||||||
return chapterText.replace(/(?<=\/p>).*?(?=<p>)/g, '');
|
|
||||||
|
const parser = new Parser({
|
||||||
|
onopentag(name, attribs) {
|
||||||
|
switch (state) {
|
||||||
|
case ParsingState.Idle:
|
||||||
|
if (name === 'div' && attribs['id'] === 'chapter-container') {
|
||||||
|
state = ParsingState.Chapter;
|
||||||
|
depth++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ParsingState.Chapter:
|
||||||
|
if (name === 'div') depth++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state === ParsingState.Chapter) {
|
||||||
|
const attr = Object.keys(attribs).map(key => {
|
||||||
|
const value = attribs[key].replace(/"/g, '"');
|
||||||
|
return ` ${key}="${value}"`;
|
||||||
|
});
|
||||||
|
chapterHtml.push(`<${name}${attr.join('')}>`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
ontext(text) {
|
||||||
|
if (state === ParsingState.Chapter) {
|
||||||
|
chapterHtml.push(escapeHtml(text));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onclosetag(name) {
|
||||||
|
if (state === ParsingState.Chapter) {
|
||||||
|
if (!parser['isVoidElement'](name)) {
|
||||||
|
chapterHtml.push(`</${name}>`);
|
||||||
|
}
|
||||||
|
if (name === 'div') depth--;
|
||||||
|
if (depth === 0) {
|
||||||
|
state = ParsingState.Stopped;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
parser.write(html);
|
||||||
|
parser.end();
|
||||||
|
|
||||||
|
return chapterHtml.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {
|
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {
|
||||||
@@ -356,3 +479,21 @@ class LightNovelPub implements Plugin.PagePlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default new LightNovelPub();
|
export default new LightNovelPub();
|
||||||
|
|
||||||
|
enum ParsingState {
|
||||||
|
Idle,
|
||||||
|
Novel,
|
||||||
|
HeaderStats,
|
||||||
|
Status,
|
||||||
|
Stopped,
|
||||||
|
Chapter,
|
||||||
|
ChapterItem,
|
||||||
|
ChapterList,
|
||||||
|
TotalChapters,
|
||||||
|
NovelName,
|
||||||
|
AuthorName,
|
||||||
|
Summary,
|
||||||
|
Genres,
|
||||||
|
Tags,
|
||||||
|
Cover,
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user