Skip to content

Commit 113906e

Browse files
oxon1umclaude
andcommitted
fix: artist lookup returning wrong artist (Stromae)
- Fix getArtistByForeignId fallback to return null instead of first result - Fix getAlbumsByArtistForeignId to not rely on potentially wrong artist lookup - Prioritize search result images over existing artist images - Add debug logging for troubleshooting Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 60a57be commit 113906e

File tree

3 files changed

+30
-43
lines changed

3 files changed

+30
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
.DS_Store
55
coverage
66
tsconfig.tsbuildinfo
7+
docs/plans

app/api/search/artist/[artistId]/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ export async function GET(req: NextRequest, { params }: { params: Promise<{ arti
2020

2121
const lidarr = new LidarrClient(config.lidarrUrl, config.lidarrApiKey, config.debugMode);
2222

23-
// Debug: log what we're looking up
24-
if (config.debugMode) console.log("[artist-detail] Looking up artistId:", artistId);
23+
// Add log for incoming artistId
24+
if (config.debugMode) console.log("[artist-detail] Received artistId:", artistId);
2525

2626
// Get artist details from lookup
2727
const artist = await lidarr.getArtistByForeignId(artistId);
28+
if (!artist) {
29+
return jsonError("Artist not found", 404);
30+
}
2831
if (config.debugMode) console.log("[artist-detail] Artist result:", artist);
2932

3033
// Get albums (from lookup or from existing library)

lib/lidarr/client.ts

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,14 @@ export class LidarrClient {
295295
if (this.debug) console.log("[lidarr] getArtistByForeignId - matched search artist:", match ? { name: match.artistName, imagesCount: match.images?.length, hasOverview: !!match.overview } : null);
296296

297297
if (match) {
298-
// Merge: use search result for images, existing artist for overview (if search doesn't have it)
298+
// Merge: use search result for images (prefer fresh data), existing artist for overview (if search doesn't have it)
299299
const merged: LidarrArtist = {
300300
...match,
301301
overview: match.overview && match.overview.trim() ? match.overview : existingArtist.overview,
302-
// Prefer existing artist images if they exist, otherwise use search images
303-
images: existingArtist.images && existingArtist.images.length > 0 ? existingArtist.images : match.images
302+
// Prefer search result images if they exist, otherwise try existing artist images
303+
images: (match.images && match.images.length > 0) ? match.images :
304+
(existingArtist.images && existingArtist.images.length > 0) ? existingArtist.images :
305+
undefined
304306
};
305307
if (this.debug) console.log("[lidarr] getArtistByForeignId - merged artist:", { name: merged.artistName, hasOverview: !!merged.overview, imagesCount: merged.images?.length });
306308
return merged;
@@ -332,57 +334,38 @@ export class LidarrClient {
332334
if (searchResults && searchResults.length > 0) {
333335
const match = searchResults.find((a) => a.foreignArtistId === foreignArtistId);
334336
if (match) return match;
335-
// Return first result if no exact match (might be the right artist)
336-
if (this.debug) console.log("[lidarr] getArtistByForeignId - no exact match in fallback, using first:", searchResults[0].artistName);
337-
return searchResults[0];
337+
// No exact match found - don't return wrong artist, log warning
338+
if (this.debug) console.log("[lidarr] getArtistByForeignId - no exact match in fallback, returning null for:", foreignArtistId);
339+
return null;
338340
}
339341

340342
return null;
341343
}
342344

343345
async getAlbumsByArtistForeignId(foreignArtistId: string): Promise<LidarrArtistAlbum[]> {
344-
// First get the artist to know their name (needed for search)
345-
const artist = await this.getArtistByForeignId(foreignArtistId);
346-
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - artist:", artist ? { name: artist.artistName, id: artist.id } : null);
347-
348-
// If artist exists in library, get albums from library
349-
const existingArtist = await this.getExistingArtistByForeignId(foreignArtistId);
350-
if (existingArtist) {
351-
const allAlbums = await this.tryRequest<LidarrArtistAlbum[]>("/api/v1/album");
352-
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - all albums in library:", allAlbums?.length ?? 0);
346+
// Skip calling getArtistByForeignId - it might return wrong artist
347+
// Instead, directly get albums from library
353348

354-
if (allAlbums && allAlbums.length > 0) {
355-
const filtered = allAlbums.filter((album) => album.foreignArtistId === foreignArtistId);
356-
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - filtered by foreignArtistId:", filtered.length, "albums");
357-
if (filtered.length > 0) return filtered;
358-
}
359-
}
349+
// First, get all albums and filter by foreignArtistId
350+
const allAlbums = await this.tryRequest<LidarrArtistAlbum[]>("/api/v1/album");
351+
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - all albums:", allAlbums?.length ?? 0);
360352

361-
// Try searching by artist name to get albums
362-
if (artist && artist.artistName) {
363-
const encoded = encodeURIComponent(artist.artistName);
364-
const albums = await this.tryRequest<LidarrArtistAlbum[]>(`/api/v1/album/lookup?term=${encoded}`);
365-
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - search results:", albums?.length ?? 0, "albums");
366-
367-
// Filter to only albums matching the foreignArtistId
368-
if (albums && albums.length > 0) {
369-
const matching = albums.filter((a) => a.foreignArtistId === foreignArtistId);
370-
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - filtered by foreignArtistId:", matching.length, "albums");
371-
if (matching.length > 0) return matching;
372-
// If no exact match, return all (might be different releases)
373-
return albums;
374-
}
353+
if (allAlbums && allAlbums.length > 0) {
354+
const filtered = allAlbums.filter((album) => album.foreignArtistId === foreignArtistId);
355+
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - filtered by foreignArtistId:", filtered.length);
356+
if (filtered.length > 0) return filtered;
375357
}
376358

377-
// Last resort: try foreignArtistId as search term (might work for some IDs)
359+
// If no albums in library, try search by foreignArtistId directly
378360
const encoded = encodeURIComponent(foreignArtistId);
379-
const albums = await this.tryRequest<LidarrArtistAlbum[]>(`/api/v1/album/lookup?term=${encoded}`);
380-
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - fallback search results:", albums?.length ?? 0, "albums");
361+
const searchAlbums = await this.tryRequest<LidarrArtistAlbum[]>(`/api/v1/album/lookup?term=${encoded}`);
362+
if (this.debug) console.log("[lidarr] getAlbumsByArtistForeignId - search results:", searchAlbums?.length ?? 0);
381363

382-
if (albums && albums.length > 0) {
383-
const matching = albums.filter((a) => a.foreignArtistId === foreignArtistId);
364+
if (searchAlbums && searchAlbums.length > 0) {
365+
const matching = searchAlbums.filter((a) => a.foreignArtistId === foreignArtistId);
384366
if (matching.length > 0) return matching;
385-
return albums;
367+
// Return all if no exact match (might be different releases)
368+
return searchAlbums;
386369
}
387370

388371
return [];

0 commit comments

Comments
 (0)