mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 12:50:09 +00:00
fcae3bcf09
Switch nightly discovery refresh to scrape Audible's curated HTML storefronts (popular, new releases, category pages) while keeping real-time user paths on the JSON catalog API. Add robust HTML resilience knobs (increased retries, capped jittered backoff, AdaptivePacer changes and per-batch cooldowns) to avoid failing nightly jobs during 503 storms. Implement multi-narrator capture via a new extractAllNarrators helper and update parsers to preserve all narrator anchors. Introduce two-pass dedup: in-memory deduplicateAndCollectGroups + collapseByExistingWorks that consults the works table, export metadataScore for consistent representative selection, and persist dedup groups (fire-and-forget). Wire collapseByExistingWorks into search/author/series routes and make defensive dedup in the refresh processor. Add HTML parsing helpers, runtime/lang-aware parsing, jitteredBackoff cap, and tests for the new behaviors.
94 lines
3.1 KiB
TypeScript
94 lines
3.1 KiB
TypeScript
/**
|
|
* Component: Series Detail API Route
|
|
* Documentation: documentation/integrations/audible.md
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getCurrentUser } from '@/lib/middleware/auth';
|
|
import { RMABLogger } from '@/lib/utils/logger';
|
|
import { scrapeSeriesPage } from '@/lib/integrations/audible-series';
|
|
import { enrichAudiobooksWithMatches } from '@/lib/utils/audiobook-matcher';
|
|
import { deduplicateAndCollectGroups } from '@/lib/utils/deduplicate-audiobooks';
|
|
import { persistDedupGroups, collapseByExistingWorks } from '@/lib/services/works.service';
|
|
import { annotateWithIgnoreStatus } from '@/lib/utils/ignored-audiobooks';
|
|
|
|
const logger = RMABLogger.create('API.Series.Detail');
|
|
|
|
/**
|
|
* GET /api/series/{asin}
|
|
* Fetch series detail: metadata + books (enriched with availability) + similar series
|
|
*/
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ asin: string }> }
|
|
) {
|
|
try {
|
|
const currentUser = getCurrentUser(request);
|
|
if (!currentUser) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized', message: 'Authentication required' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const { asin } = await params;
|
|
|
|
if (!asin || !/^[A-Z0-9]{10}$/.test(asin)) {
|
|
return NextResponse.json(
|
|
{ error: 'ValidationError', message: 'Valid series ASIN is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const page = parseInt(request.nextUrl.searchParams.get('page') || '1', 10);
|
|
|
|
logger.info(`Fetching series detail: ${asin}, page ${page}`);
|
|
|
|
const detail = await scrapeSeriesPage(asin, page);
|
|
if (!detail) {
|
|
return NextResponse.json(
|
|
{ error: 'NotFound', message: 'Series not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Two-pass dedup: local title/narrator/duration matching first, then collapse
|
|
// any remaining duplicates that the works table already knows are the same book
|
|
// (handles cases where source metadata diverges across paths or pages).
|
|
const { books: dedupedBooks, groups } = deduplicateAndCollectGroups(detail.books);
|
|
|
|
if (groups.length > 0) {
|
|
persistDedupGroups(groups).catch(() => {});
|
|
}
|
|
|
|
const collapsedBooks = await collapseByExistingWorks(dedupedBooks);
|
|
|
|
// Enrich books with library availability and request status
|
|
const userId = currentUser.sub || undefined;
|
|
const enrichedBooks = await enrichAudiobooksWithMatches(collapsedBooks, userId);
|
|
|
|
// Annotate with per-user ignore status
|
|
const annotatedBooks = await annotateWithIgnoreStatus(enrichedBooks, userId);
|
|
|
|
logger.info(`Series detail complete: "${detail.title}" (${annotatedBooks.length} books, page ${page})`);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
series: {
|
|
...detail,
|
|
books: annotatedBooks,
|
|
},
|
|
hasMore: detail.hasMore,
|
|
page: detail.page,
|
|
});
|
|
} catch (error) {
|
|
logger.error('Failed to fetch series detail', {
|
|
error: error instanceof Error ? error.message : String(error),
|
|
});
|
|
return NextResponse.json(
|
|
{ error: 'FetchError', message: 'Failed to fetch series details' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|