From c9392c49c969c4652ddbabd59c761fced97c3808 Mon Sep 17 00:00:00 2001 From: Mattias Carlsson Date: Sun, 19 Apr 2026 22:09:46 +0200 Subject: [PATCH] If ASIN lookup fails, use the folder name instead of the tag. --- src/app/api/admin/bulk-import/scan/route.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/app/api/admin/bulk-import/scan/route.ts b/src/app/api/admin/bulk-import/scan/route.ts index aaacca8..9c98b19 100644 --- a/src/app/api/admin/bulk-import/scan/route.ts +++ b/src/app/api/admin/bulk-import/scan/route.ts @@ -174,7 +174,21 @@ export async function POST(request: NextRequest) { } if (!match) { - const searchResult = await audibleService.search(book.searchTerm); + // When an ASIN was extracted from the folder name but the direct + // lookup failed, prefer the folder name as the text search term + // over book.searchTerm. book.searchTerm may come from a single + // tagged file whose album tag is unreliable (e.g. a series name + // or intro track), whereas the folder name is the human-assigned + // title and is more likely to be accurate. + const textSearchTerm = book.extractedAsin + ? book.folderName + .replace(/[\[\(][A-Z0-9]{10}[\]\)]/g, '') // strip ASIN + .replace(/[\[\(]\d{4}[\]\)]/g, '') // strip year + .replace(/[_]/g, ' ') + .replace(/\s+/g, ' ') + .trim() + : book.searchTerm; + const searchResult = await audibleService.search(textSearchTerm); if (searchResult.results.length > 0) { match = searchResult.results[0]; }