59 lines
3.0 KiB
JavaScript
59 lines
3.0 KiB
JavaScript
'use strict';
|
|
|
|
const { SlowHttp } = require('../lib/http');
|
|
const { fallbackDirection } = require('../../reading-direction');
|
|
const parser = require('../plugins/weebcentral');
|
|
const { parseSource } = require('../plugins/client');
|
|
|
|
const parseChapters = (html, baseUrl) => parser.catalog({ html, url: baseUrl }).chapters;
|
|
const parsePageUrls = (html, baseUrl) => parser.pages({ html, url: baseUrl }).pages;
|
|
|
|
async function discoverWeebCatalog(seriesUrl, http) {
|
|
const seriesHtml = await (await http.get(seriesUrl, { accept: 'text/html' })).text();
|
|
const { title, listUrl } = await parseSource('weebcentral', 'series', { html: seriesHtml, url: seriesUrl });
|
|
const listHtml = listUrl ? await (await http.get(listUrl, { accept: 'text/html' })).text() : seriesHtml;
|
|
const parsed = await parseSource('weebcentral', 'catalog', { html: listHtml, url: seriesUrl });
|
|
const chapterLinks = parsed.chapterLinks;
|
|
const chapters = parsed.chapters.sort((a, b) =>
|
|
Number(a.book?.number || 1) - Number(b.book?.number || 1) || Number(a.number) - Number(b.number));
|
|
return { title, chapters, chapterLinks,
|
|
...(chapterLinks && !chapters.length ? { availability: { reason: 'unrecognized-labels',
|
|
message: `WeebCentral lists ${chapterLinks} chapter links, but their numbering labels are not recognized by the parser.` } } : {}),
|
|
};
|
|
}
|
|
|
|
async function discoverWeebChapter(chapter, http) {
|
|
const pageHtml = await (await http.get(chapter.url, { accept: 'text/html' })).text();
|
|
const { manifestUrl, added } = await parseSource('weebcentral', 'chapter', { html: pageHtml, url: chapter.url });
|
|
const fragment = await (await http.get(`${manifestUrl}&reading_style=long_strip¤t_page=1`, { accept: 'text/html' })).text();
|
|
const parsed = await parseSource('weebcentral', 'pages', { html: fragment, url: manifestUrl });
|
|
const pages = parsed.pages.map(url => ({ url, headers: { referer: chapter.url } }));
|
|
if (!pages.length) throw new Error(`No pages found for chapter ${chapter.number}`);
|
|
return {
|
|
number: chapter.number, title: chapter.title || '',
|
|
...(chapter.book ? { book: chapter.book } : {}),
|
|
added,
|
|
source: { name: 'WeebCentral', url: chapter.url }, pages,
|
|
};
|
|
}
|
|
|
|
async function discoverWeebCentral(seriesUrl, options = {}) {
|
|
const http = new SlowHttp(options.http || {});
|
|
const catalog = await discoverWeebCatalog(seriesUrl, http);
|
|
const chapters = catalog.chapters
|
|
.sort((a, b) => Number(b.number) - Number(a.number))
|
|
.slice(0, options.limit || 3)
|
|
.sort((a, b) => Number(a.number) - Number(b.number));
|
|
const output = [];
|
|
for (const chapter of chapters) {
|
|
output.push(await discoverWeebChapter(chapter, http));
|
|
}
|
|
return {
|
|
version: 1,
|
|
series: { title: catalog.title, ...fallbackDirection(), sources: [{ name: 'WeebCentral', url: seriesUrl }], scraper: { ids: { weebcentral: seriesUrl.split('/series/')[1]?.split('/')[0] } } },
|
|
http: options.http || {}, chapters: output,
|
|
};
|
|
}
|
|
|
|
module.exports = { discoverWeebCentral, discoverWeebCatalog, discoverWeebChapter, parseChapters, parsePageUrls };
|