Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions plugins/english/wtrlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class WTRLAB implements Plugin.PluginBase {
id = 'WTRLAB';
name = 'WTR-LAB';
site = 'https://wtr-lab.com/';
version = '1.1.4';
version = '1.1.5';
icon = 'src/en/wtrlab/icon.png';
sourceLang = 'en/';
baggage = '';
Expand Down Expand Up @@ -341,7 +341,7 @@ class WTRLAB implements Plugin.PluginBase {
'';
}

const urlMatch = novelPath.match(/serie-(\d+)\/([^/]+)/);
const urlMatch = novelPath.match(/(?:serie|novel)-?(\d+)\/([^/]+)/);
if (urlMatch) {
rawId = parseInt(urlMatch[1]);
slug = urlMatch[2];
Expand Down Expand Up @@ -369,18 +369,17 @@ class WTRLAB implements Plugin.PluginBase {
}
let chapters: Plugin.ChapterItem[] = [];

if (rawId && slug && chapterCount > 0) {
if (rawId && slug) {
try {
chapters = await this.fetchAllChapters(rawId, chapterCount, slug);
chapters = await this.fetchAllChapters(rawId, slug);
} catch (error) {
console.error('Failed to fetch chapters via API:', error);
chapters = [];
}
} else {
console.warn('Could not extract rawId, slug, or chapterCount from page', {
console.warn('Could not extract rawId or slug from page', {
rawId,
slug,
chapterCount,
});
}

Expand Down Expand Up @@ -513,7 +512,7 @@ class WTRLAB implements Plugin.PluginBase {
let chapterNo: number | null = null;
let loadedCheerio = null;

const urlMatch = chapterPath.match(/serie-(\d+)\/[^/]+\/chapter-(\d+)/);
const urlMatch = chapterPath.match(/(?:serie|novel)-?(\d+)\/[^/]+\/chapter-(\d+)/);
if (urlMatch) {
rawId = parseInt(urlMatch[1], 10);
chapterNo = parseInt(urlMatch[2], 10);
Expand Down Expand Up @@ -622,14 +621,15 @@ class WTRLAB implements Plugin.PluginBase {

async fetchAllChapters(
rawId: number,
totalChapters: number,
slug: string,
): Promise<Plugin.ChapterItem[]> {
const allChapters: Plugin.ChapterItem[] = [];
const batchSize = 250;
const batchSize = 500;
let start = 1;
let hasMore = true;

for (let start = 1; start <= totalChapters; start += batchSize) {
const end = Math.min(start + batchSize - 1, totalChapters);
while (hasMore) {
const end = start + batchSize - 1;

try {
const response = await fetchApi(
Expand All @@ -644,27 +644,35 @@ class WTRLAB implements Plugin.PluginBase {
const data = await response.json();
const chapters = data.chapters ?? data.data?.chapters ?? [];

if (Array.isArray(chapters)) {
const batchChapters: Plugin.ChapterItem[] = chapters.map(
(apiChapter: ApiChapter) => ({
name: apiChapter.title,
path: `${this.sourceLang}serie-${rawId}/${slug}/chapter-${apiChapter.order}`,
releaseTime: apiChapter.updated_at?.substring(0, 10),
chapterNumber: apiChapter.order,
}),
);
if (!Array.isArray(chapters) || chapters.length === 0) {
hasMore = false;
break;
}

allChapters.push(...batchChapters);
const batchChapters: Plugin.ChapterItem[] = chapters.map(
(apiChapter: ApiChapter) => ({
name:
apiChapter.title ||
apiChapter.name ||
`Chapter ${apiChapter.order}`,
path: `${this.sourceLang}serie-${rawId}/${slug}/chapter-${apiChapter.order}`,
releaseTime: apiChapter.updated_at?.substring(0, 10),
chapterNumber: apiChapter.order,
}),
);

if (chapters.length < batchSize) {
break;
}
} else {
allChapters.push(...batchChapters);

if (chapters.length < batchSize) {
hasMore = false;
break;
}

start += batchSize;
} catch (error) {
console.error(`Failed to fetch chapters ${start}-${end}:`, error);
continue;
hasMore = false;
break;
}
}

Expand Down
Loading