89 lines
4.5 KiB
JavaScript
89 lines
4.5 KiB
JavaScript
'use strict';
|
|
|
|
const { SlowHttp } = require('../lib/http');
|
|
const { fallbackDirection } = require('../../reading-direction');
|
|
|
|
const decode = value => value.replace(/&/g, '&').replace(/'/g, "'").replace(/"/g, '"').replace(/<[^>]+>/g, '').trim();
|
|
|
|
function absolute(base, value) { return new URL(value, base).href; }
|
|
|
|
function parseChapters(html, baseUrl) {
|
|
const found = [];
|
|
const re = /<a\b[^>]*href="([^"]*\/chapters\/([A-Z0-9]+)[^"]*)"[^>]*>([\s\S]*?)<\/a>/gi;
|
|
for (const match of html.matchAll(re)) {
|
|
const text = decode(match[3]).replace(/\s+/g, ' ');
|
|
const season = Number(text.match(/^S(\d+)\s*-/i)?.[1] || 0) || null;
|
|
// A few WeebCentral catalogs use a series-specific label (for example
|
|
// "Rating 442", "Episode 128", "Night 182", "Log. 65", or "Act 60.6") in the same place where most catalogs
|
|
// say "Chapter 442". Treat those labels as chapter numbering instead of
|
|
// discarding the entire catalog.
|
|
const regular = text.match(/\b(?:Chapter|Rating|Episode|Night|Log|Act)\s*[.:]?\s*([0-9]+(?:\.[0-9]+)?)/i)?.[1];
|
|
const prologue = text.match(/Prologue(?:\s+([0-9]+))?/i)?.[1];
|
|
const number = regular || (prologue ? `0.${prologue.padStart(2, '0')}` : /Prologue/i.test(text) ? '0' : null);
|
|
if (number) found.push({
|
|
id: match[2], number, title: regular ? '' : `Prologue ${prologue || ''}`.trim(),
|
|
...(season ? { book: { number: season, title: `Season ${season}` } } : {}),
|
|
url: absolute(baseUrl, match[1]), raw: text,
|
|
});
|
|
}
|
|
return [...new Map(found.map(chapter => [chapter.id, chapter])).values()];
|
|
}
|
|
|
|
async function discoverWeebCatalog(seriesUrl, http) {
|
|
const seriesHtml = await (await http.get(seriesUrl, { accept: 'text/html' })).text();
|
|
const title = decode(seriesHtml.match(/<h1\b[^>]*>([\s\S]*?)<\/h1>/i)?.[1] || 'Untitled');
|
|
const listUrl = seriesHtml.match(/hx-get="([^"]+\/full-chapter-list)"/i)?.[1];
|
|
const listHtml = listUrl ? await (await http.get(absolute(seriesUrl, listUrl), { accept: 'text/html' })).text() : seriesHtml;
|
|
const chapterLinks = [...listHtml.matchAll(/<a\b[^>]*href="[^"]*\/chapters\/[A-Z0-9]+[^"]*"/gi)].length;
|
|
const chapters = parseChapters(listHtml, seriesUrl).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 = pageHtml.match(/['"](https?:\/\/weebcentral\.com\/chapters\/[^'"]+\/images\?is_prev=[^'"]+)['"]/i)?.[1]
|
|
|| `${chapter.url}/images?is_prev=False`;
|
|
const fragment = await (await http.get(`${manifestUrl}&reading_style=long_strip¤t_page=1`, { accept: 'text/html' })).text();
|
|
const pages = parsePageUrls(fragment, manifestUrl).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: pageHtml.match(/datetime="([^"]+)"/i)?.[1],
|
|
source: { name: 'WeebCentral', url: chapter.url }, pages,
|
|
};
|
|
}
|
|
|
|
function parsePageUrls(html, baseUrl) {
|
|
const urls = [];
|
|
for (const match of html.matchAll(/<img\b[^>]*(?:src|data-src)="([^"]+)"/gi)) {
|
|
const url = absolute(baseUrl, match[1].replace(/&/g, '&'));
|
|
if (!url.startsWith('data:')) urls.push(url);
|
|
}
|
|
return [...new Set(urls)];
|
|
}
|
|
|
|
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 };
|