Added new novel strategy
This commit is contained in:
+4
-2
@@ -46,8 +46,10 @@ export interface NovelDefinition {
|
||||
slug: string;
|
||||
type: 'epub' | 'web-scraped';
|
||||
source: {
|
||||
url?: string;
|
||||
urls?: string[];
|
||||
url?: string; // web-scraped TOC strategy: the table-of-contents page URL
|
||||
urls?: string[]; // epub: download URL(s)
|
||||
urlTemplate?: string; // web-scraped increment strategy: template with {n} placeholder, e.g. "https://example.com/chapter-{n}"
|
||||
startIndex?: number; // increment: chapter number of the first {n} value (default 1)
|
||||
};
|
||||
selectors?: {
|
||||
toc?: TocSelector;
|
||||
|
||||
+21
-1
@@ -145,7 +145,7 @@ export function registerNovelFromConfig(
|
||||
cache: Cache,
|
||||
appConfig: AppConfig,
|
||||
): number {
|
||||
const sourceUrl = def.source.url ?? def.source.urls?.[0] ?? '';
|
||||
const sourceUrl = def.source.url ?? def.source.urls?.[0] ?? def.source.urlTemplate ?? '';
|
||||
const hash = createHash('sha256').update(def.slug + sourceUrl).digest('hex');
|
||||
|
||||
let registeredId = -1;
|
||||
@@ -162,7 +162,27 @@ export function registerNovelFromConfig(
|
||||
registry.updateRange(registeredId, start, end),
|
||||
),
|
||||
});
|
||||
} else if (def.source.urlTemplate) {
|
||||
// Increment strategy: chapter URLs follow a predictable pattern — no TOC fetch needed.
|
||||
// {n} is replaced with the chapter number directly; endsAt is unknown so set to MAX_SAFE_INTEGER.
|
||||
const startIndex = def.source.startIndex ?? 1;
|
||||
const template = def.source.urlTemplate;
|
||||
registeredId = registry.register({
|
||||
hash,
|
||||
slug: def.slug,
|
||||
startsAt: startIndex,
|
||||
endsAt: Number.MAX_SAFE_INTEGER,
|
||||
url: template,
|
||||
fetch: async (from, to) => {
|
||||
const urls = Array.from(
|
||||
{ length: to - from + 1 },
|
||||
(_, i) => template.replace('{n}', String(from + i)),
|
||||
);
|
||||
return fetchChapters(def, registeredId, from, urls, cache);
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// TOC strategy: scrape the table-of-contents page to discover all chapter URLs.
|
||||
registeredId = registry.register({
|
||||
hash,
|
||||
slug: def.slug,
|
||||
|
||||
Reference in New Issue
Block a user