Checkpoint first-party source parser pilot
This commit is contained in:
1 parent
605fa5b9d8
commit
c5a92282a9
9 files changed
+320
-43
No files matched your search
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const { parse } = require('./registry');
|
||||
|
||||
async function parseSource(plugin, operation, input) {
|
||||
const endpoint = process.env.SOURCE_PLUGIN_URL;
|
||||
if (!endpoint) return parse(plugin, operation, input);
|
||||
const url = new URL('/v1/parse', endpoint);
|
||||
if (!['http:', 'https:'].includes(url.protocol)) throw new Error('Invalid source plugin endpoint');
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ plugin, operation, input }),
|
||||
signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
if (!response.ok) throw new Error(`Source parser unavailable (${response.status}); checkpoint preserved`);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
module.exports = { parseSource };
|
||||
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
// This list is shipped with MangaCircus. Neither the admin UI nor a remote
|
||||
// source may add executable modules at runtime.
|
||||
const manifests = Object.freeze({
|
||||
weebcentral: Object.freeze({
|
||||
id: 'weebcentral', version: 1, label: 'WeebCentral', status: 'pilot',
|
||||
sourceHosts: Object.freeze(['weebcentral.com']),
|
||||
implementation: 'first-party',
|
||||
provenance: 'MangaCircus existing WeebCentral adapter; no Uchiyomi code copied',
|
||||
operations: Object.freeze(['series', 'catalog', 'chapter', 'pages']),
|
||||
}),
|
||||
});
|
||||
|
||||
module.exports = { manifests };
|
||||
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
// Reviewed first-party modules only. No filesystem scanning or runtime installs.
|
||||
const { manifests } = require('./manifest');
|
||||
const plugins = Object.freeze({ weebcentral: require('./weebcentral') });
|
||||
|
||||
function parse(plugin, operation, input) {
|
||||
const manifest = manifests[plugin];
|
||||
const handler = manifest?.operations.includes(operation) ? plugins[plugin]?.[operation] : null;
|
||||
if (typeof handler !== 'function') throw new Error('Unknown source plugin or operation');
|
||||
if (!input || typeof input !== 'object' || typeof input.html !== 'string' || typeof input.url !== 'string') {
|
||||
throw new Error('Invalid parser input');
|
||||
}
|
||||
const url = new URL(input.url);
|
||||
if (!['https:', 'http:'].includes(url.protocol)) throw new Error('Invalid source URL');
|
||||
if (!manifest.sourceHosts.includes(url.hostname)) throw new Error('Source URL does not belong to plugin');
|
||||
return handler({ html: input.html, url: url.href });
|
||||
}
|
||||
|
||||
module.exports = { parse, manifests };
|
||||
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
// First-party, pure parsing functions. This module never fetches remote content.
|
||||
const decode = value => value.replace(/&/g, '&').replace(/'/g, "'").replace(/"/g, '"').replace(/<[^>]+>/g, '').trim();
|
||||
const absolute = (base, value) => new URL(value, base).href;
|
||||
|
||||
function series({ html, url }) {
|
||||
const title = decode(html.match(/<h1\b[^>]*>([\s\S]*?)<\/h1>/i)?.[1] || 'Untitled');
|
||||
const listUrl = html.match(/hx-get="([^"]+\/full-chapter-list)"/i)?.[1];
|
||||
return { title, listUrl: listUrl ? absolute(url, listUrl) : null };
|
||||
}
|
||||
|
||||
function catalog({ html, url }) {
|
||||
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;
|
||||
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(url, match[1]), raw: text,
|
||||
});
|
||||
}
|
||||
const chapters = [...new Map(found.map(chapter => [chapter.id, chapter])).values()];
|
||||
const chapterLinks = [...html.matchAll(/<a\b[^>]*href="[^"]*\/chapters\/[A-Z0-9]+[^"]*"/gi)].length;
|
||||
return { chapters, chapterLinks };
|
||||
}
|
||||
|
||||
function chapter({ html, url }) {
|
||||
return {
|
||||
manifestUrl: html.match(/['"](https?:\/\/weebcentral\.com\/chapters\/[^'"]+\/images\?is_prev=[^'"]+)['"]/i)?.[1]
|
||||
|| `${url}/images?is_prev=False`,
|
||||
added: html.match(/datetime="([^"]+)"/i)?.[1],
|
||||
};
|
||||
}
|
||||
|
||||
function pages({ html, url }) {
|
||||
const urls = [];
|
||||
for (const match of html.matchAll(/<img\b[^>]*(?:src|data-src)="([^"]+)"/gi)) {
|
||||
const pageUrl = absolute(url, match[1].replace(/&/g, '&'));
|
||||
if (!pageUrl.startsWith('data:')) urls.push(pageUrl);
|
||||
}
|
||||
return { pages: [...new Set(urls)] };
|
||||
}
|
||||
|
||||
module.exports = { series, catalog, chapter, pages };
|
||||
@@ -0,0 +1,48 @@
|
||||
'use strict';
|
||||
|
||||
const http = require('node:http');
|
||||
const { parse } = require('./registry');
|
||||
const MAX_BODY_BYTES = 2 * 1024 * 1024;
|
||||
|
||||
function createServer() {
|
||||
return http.createServer((request, response) => {
|
||||
if (request.method === 'GET' && request.url === '/health') {
|
||||
response.writeHead(200, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ ok: true }));
|
||||
return;
|
||||
}
|
||||
if (request.method !== 'POST' || request.url !== '/v1/parse') {
|
||||
response.writeHead(404).end();
|
||||
return;
|
||||
}
|
||||
let bytes = 0;
|
||||
const chunks = [];
|
||||
request.on('data', chunk => {
|
||||
bytes += chunk.length;
|
||||
if (bytes > MAX_BODY_BYTES) {
|
||||
response.writeHead(413).end();
|
||||
request.destroy();
|
||||
return;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
});
|
||||
request.on('end', () => {
|
||||
if (bytes > MAX_BODY_BYTES) return;
|
||||
try {
|
||||
const { plugin, operation, input } = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
||||
const output = parse(plugin, operation, input);
|
||||
response.writeHead(200, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify(output));
|
||||
} catch (error) {
|
||||
response.writeHead(400, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ error: error.message }));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
createServer().listen(Number(process.env.SOURCE_PLUGIN_PORT || 8094), process.env.SOURCE_PLUGIN_HOST || '127.0.0.1');
|
||||
}
|
||||
|
||||
module.exports = { createServer };
|
||||
Reference in new issue
Block a user