Pawread: Refactor with Htmlparser2 (#1645)

* pawread refactor

* add slash to maintain backwards compat

* fix pretty
This commit is contained in:
K1ngfish3r
2025-04-24 17:16:19 +05:00
committed by GitHub
parent ba5c7bc25f
commit add1f2f6da
+296 -75
View File
@@ -1,4 +1,4 @@
import { CheerioAPI, load as parseHTML } from 'cheerio'; import { Parser } from 'htmlparser2';
import { fetchApi } from '@libs/fetch'; import { fetchApi } from '@libs/fetch';
import { Plugin } from '@typings/plugin'; import { Plugin } from '@typings/plugin';
import { Filters, FilterTypes } from '@libs/filterInputs'; import { Filters, FilterTypes } from '@libs/filterInputs';
@@ -6,30 +6,59 @@ import { Filters, FilterTypes } from '@libs/filterInputs';
class PawRead implements Plugin.PluginBase { class PawRead implements Plugin.PluginBase {
id = 'pawread'; id = 'pawread';
name = 'PawRead'; name = 'PawRead';
version = '2.0.1'; version = '2.1.0';
icon = 'src/en/pawread/icon.png'; icon = 'src/en/pawread/icon.png';
site = 'https://m.pawread.com/'; site = 'https://m.pawread.com/';
parseNovels(loadedCheerio: CheerioAPI) { parseNovels(html: string) {
const novels: Plugin.NovelItem[] = []; const novels: Plugin.NovelItem[] = [];
let tempNovel: Partial<Plugin.NovelItem> = {};
let state: ParsingState = ParsingState.Idle;
const parser = new Parser({
onopentag(name, attribs) {
if (
attribs.class &&
(attribs.class.includes('list-comic') ||
attribs.class.includes('itemBox'))
) {
state = ParsingState.Novel;
}
loadedCheerio('.list-comic, .itemBox').each((idx, ele) => { if (state !== ParsingState.Novel) return;
loadedCheerio(ele).find('.serialise').remove();
const novelName = loadedCheerio(ele).find('a').text().trim();
const novelCover = loadedCheerio(ele).find('img').attr('src');
const novelUrl = loadedCheerio(ele).find('a').attr('href')?.slice(1);
if (!novelUrl) return; switch (name) {
case 'a':
if (attribs.class === 'txtA' || attribs.class === 'title') {
tempNovel.path = attribs.href.split('/').slice(1, 3).join('/');
state = ParsingState.NovelName;
}
break;
case 'img':
tempNovel.cover = attribs.src;
break;
}
},
const novel = { ontext(text) {
name: novelName, if (state === ParsingState.NovelName) {
cover: novelCover, tempNovel.name = (tempNovel.name || '') + text;
path: novelUrl, }
}; },
novels.push(novel); onclosetag(name) {
if (name === 'a') {
if (tempNovel.name && tempNovel.cover) {
novels.push(tempNovel as Plugin.NovelItem);
tempNovel = {};
state = ParsingState.Idle;
}
}
},
}); });
parser.write(html);
parser.end();
return novels; return novels;
} }
@@ -40,101 +69,277 @@ class PawRead implements Plugin.PluginBase {
let link = `${this.site}list/`; let link = `${this.site}list/`;
const filterValues = [ const filterValues = [
filters.genre.value,
filters.status.value, filters.status.value,
filters.lang.value, filters.lang.value,
filters.genre.value, ].filter(value => value !== '');
];
link += filterValues.filter(value => value !== '').join('-'); if (filterValues.length > 0) {
if (filterValues.some(value => value !== '')) link += '/'; link += filterValues.join('-') + '/';
}
if (filters.order.value) link += '-'; link += (filters.order.value ? '-' : '') + filters.sort.value;
link += filters.sort.value;
link += `/?page=${page}`; link += `/?page=${page}`;
const body = await fetchApi(link).then(r => r.text()); const body = await fetchApi(link).then(r => r.text());
return this.parseNovels(body);
const loadedCheerio = parseHTML(body);
return this.parseNovels(loadedCheerio);
} }
async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> { async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> {
const result = await fetchApi(this.site + novelPath); const slash = novelPath.endsWith('/') ? '' : '/';
const result = await fetchApi(this.site + novelPath + slash);
const body = await result.text(); const body = await result.text();
const loadedCheerio = parseHTML(body); const novel: Partial<Plugin.SourceNovel> = {
const novel: Plugin.SourceNovel = {
path: novelPath, path: novelPath,
name: loadedCheerio('#Cover img').attr('title') || 'Untitled',
cover: loadedCheerio('#Cover img').attr('src'),
author: loadedCheerio('.icon01 <').text().trim(),
status: loadedCheerio('.txtItme:first').text().trim(),
chapters: [],
}; };
novel.summary = loadedCheerio('#full-des') let depth = 0;
.find('br') let state = ParsingState.Idle;
.replaceWith('\n') let tempChapter: Partial<Plugin.ChapterItem> = {};
.end()
.text()
.trim();
novel.genres = loadedCheerio('a.btn-default')
.map((i, el) => loadedCheerio(el).text().trim())
.toArray()
.join(',');
const chapter: Plugin.ChapterItem[] = []; const chapter: Plugin.ChapterItem[] = [];
const summaryParts: string[] = [];
const genreArray: string[] = [];
loadedCheerio('.item-box').each((idx, ele) => { const parser = new Parser({
const smallText = loadedCheerio(ele).find('span:last').text().trim(); onopentag(name, attribs) {
if (smallText === 'Advanced Chapter') return; switch (name) {
const releaseDate = smallText.split('.').map(x => Number(x)); case 'div':
const chapterName = loadedCheerio(ele).find('span:first').text().trim(); if (attribs.id === 'Cover') {
const chapterUrl = loadedCheerio(ele).attr('onclick')?.match(/\d+/)![0]; state = ParsingState.Cover;
if (!chapterUrl) return; }
if (attribs.class?.includes('item-box')) {
state = ParsingState.Chapter;
const path = attribs.onclick.match(/\d+/)![0];
tempChapter.path = `${novelPath}${slash}${path}.html`;
return;
}
if (state === ParsingState.Chapter) depth++;
break;
case 'img':
if (state === ParsingState.Cover) {
novel.name = attribs.title;
novel.cover = attribs.src;
}
break;
case 'p':
if (attribs.class === 'txtItme') {
if (!novel.status) {
state = ParsingState.Status;
} else if (!novel.author) {
state = ParsingState.Author;
}
} else if (attribs.id === 'full-des') {
state = ParsingState.Summary;
}
break;
case 'br':
summaryParts.push('\n');
break;
case 'a':
if (attribs.class?.includes('btn-default')) {
state = ParsingState.Genres;
}
break;
case 'span':
if (state === ParsingState.Chapter) depth++;
if (depth === 2) {
state = ParsingState.ChapterName;
} else if (depth === 1) {
state = ParsingState.ChapterTime;
}
break;
}
},
chapter.push({ ontext(text) {
name: chapterName, switch (state) {
path: `${novelPath}${chapterUrl}.html`, case ParsingState.Status:
releaseTime: new Date( novel.status = (novel.status || '') + text.trim();
releaseDate[0], break;
releaseDate[1], case ParsingState.Author:
releaseDate[2], novel.author = (novel.author || '') + text.trim();
).toISOString(), break;
}); case ParsingState.Genres:
genreArray.push(text.trim());
break;
case ParsingState.Summary:
summaryParts.push(text);
break;
case ParsingState.ChapterName:
tempChapter.name = (tempChapter.name || '') + text.trim();
break;
case ParsingState.ChapterTime:
if (text?.includes('Advanced')) return;
const releaseDate = text.split('.').map(x => Number(x));
if (releaseDate.length === 3) {
tempChapter.releaseTime = new Date(
releaseDate[0],
releaseDate[1] - 1,
releaseDate[2],
).toISOString();
}
break;
}
},
onclosetag(name) {
switch (name) {
case 'div':
if (state === ParsingState.Cover) {
state = ParsingState.Idle;
}
if (state === ParsingState.Chapter) {
depth--;
if (depth < 0) {
if (
tempChapter.path &&
tempChapter.name &&
tempChapter.releaseTime
) {
chapter.push(tempChapter as Plugin.ChapterItem);
}
tempChapter = {};
depth = 0;
state = ParsingState.Idle;
}
}
break;
case 'p':
if (
state === ParsingState.Status ||
state === ParsingState.Author ||
state === ParsingState.Summary
) {
state = ParsingState.Idle;
}
break;
case 'a':
if (state === ParsingState.Genres) {
state = ParsingState.Idle;
}
break;
case 'span':
if (
state === ParsingState.ChapterName ||
state === ParsingState.ChapterTime
) {
state = ParsingState.Chapter;
depth--;
}
break;
}
},
onend() {
novel.genres = genreArray.join(', ');
novel.summary = summaryParts.join('');
novel.chapters = chapter;
},
}); });
novel.chapters = chapter; parser.write(body);
return novel; parser.end();
return novel as Plugin.SourceNovel;
} }
async parseChapter(chapterPath: string): Promise<string> { async parseChapter(chapterPath: string): Promise<string> {
const result = await fetchApi(this.site + chapterPath); const html = await fetchApi(this.site + chapterPath).then(r => r.text());
const body = await result.text();
const loadedCheerio = parseHTML(body); let depth = 0;
let state: ParsingState = ParsingState.Idle;
const chapterHtml: string[] = [];
const steal = ['bit.ly', 'tinyurl', 'pawread']; type EscapeChar = '&' | '<' | '>' | '"' | "'" | ' ';
steal.map(tag => loadedCheerio(`p:icontains(${tag})`).remove()); const escapeRegex = /[&<>"' ]/g;
const chapterText = loadedCheerio('.main').html() || ''; const escapeMap: Record<EscapeChar, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
' ': '&nbsp;',
};
const escapeHtml = (text: string): string =>
text.replace(escapeRegex, char => escapeMap[char as EscapeChar]);
return chapterText; const parser = new Parser({
onopentag(name, attribs) {
switch (state) {
case ParsingState.Idle:
if (name === 'div' && attribs.class === 'main') {
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, '&quot;');
return ` ${key}="${value}"`;
});
chapterHtml.push(`<${name}${attr.join('')}>`);
}
},
ontext(data) {
if (state === ParsingState.Chapter) {
const text = escapeHtml(data);
const icontains = ['pawread', 'tinyurl', 'bit.ly'];
if (icontains.some(pattern => text?.includes(pattern))) {
state = ParsingState.Hidden;
chapterHtml.pop();
} else {
chapterHtml.push(text);
}
}
},
onclosetag(name) {
switch (state) {
case ParsingState.Chapter:
if (!parser['isVoidElement'](name)) {
chapterHtml.push(`</${name}>`);
}
if (name === 'div') depth--;
if (depth === 0) {
state = ParsingState.Stopped;
}
break;
case ParsingState.Hidden:
state = ParsingState.Chapter;
break;
}
},
});
parser.write(html);
parser.end();
return chapterHtml.join('');
} }
async searchNovels( async searchNovels(
searchTerm: string, searchTerm: string,
page: number, page: number,
): Promise<Plugin.NovelItem[]> { ): Promise<Plugin.NovelItem[]> {
const searchUrl = `${this.site}search/?keywords=${encodeURIComponent(searchTerm)}&page=${page}`; const params = new URLSearchParams({
keywords: searchTerm,
page: page.toString(),
});
const result = await fetchApi(searchUrl); const result = await fetchApi(`${this.site}search/?${params.toString()}`);
const body = await result.text(); const body = await result.text();
const loadedCheerio = parseHTML(body); return this.parseNovels(body);
return this.parseNovels(loadedCheerio);
} }
filters = { filters = {
@@ -224,3 +429,19 @@ class PawRead implements Plugin.PluginBase {
} }
export default new PawRead(); export default new PawRead();
enum ParsingState {
Idle,
Cover,
Genres,
Author,
Status,
Hidden,
Summary,
Stopped,
Chapter,
ChapterName,
ChapterTime,
NovelName,
Novel,
}