@@ -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