- albums = []
- for name in artist.names:
- if artist.aliases:
- self.log.debug('Searching album for aliase: "%s"', name)
- kwalbart = {'albumartist': name, 'artist': name}
- for album in self.list('album', 'albumartist', name):
- if album and album not in albums:
- albums.append(Album(name=album, **kwalbart))
- for album in self.list('album', 'artist', name):
- album_trks = [trk for trk in self.find('album', album)]
- if 'Various Artists' in [tr.albumartist for tr in album_trks]:
- self.log.debug('Discarding %s ("Various Artists" set)', album)
- continue
- arts = {trk.artist for trk in album_trks}
- # Avoid selecting album where artist is credited for a single
- # track of the album
- if len(set(arts)) < 2: # TODO: better heuristic, use a ratio instead
- if album not in albums:
- albums.append(Album(name=album, **kwalbart))
- elif album and album not in albums:
- self.log.debug('"{0}" probably not an album of "{1}"'.format(
- album, artist) + '({0})'.format('/'.join(arts)))
- return albums
+ # First, look for all potential albums
+ self.log.debug('Searching album for "%r"', artist)
+ if artist.aliases:
+ self.log.debug('Searching album for %s aliases: "%s"',
+ artist, artist.aliases)
+ for name_sz in artist.names_sz:
+ raw_albums = self.list('album', f"( albumartist == '{name_sz}')")
+ albums = [Album(a, albumartist=artist.name, artist=artist) for a in raw_albums if a]
+ candidates = []
+ for album in albums:
+ album_trks = self.find_tracks(album)
+ album_artists = {tr.albumartist for tr in album_trks if tr.albumartist}
+ if album.artist.names & album_artists:
+ candidates.append(album)
+ continue
+ if 'Various Artists' in album_artists:
+ self.log.debug('Discarding %s ("Various Artists" set)', album)
+ continue
+ if album_artists and album.artist.name not in album_artists:
+ self.log.debug('Discarding "%s", "%s" not set as albumartist', album, album.artist)
+ continue
+ # Attempt to detect false positive
+ # Avoid selecting albums where artist is credited for a single
+ # track of the album
+ album_trks = self.find(f"(album == '{album.name_sz}')")
+ arts = [trk.artist for trk in album_trks] # Artists in the album
+ # count artist occurences
+ ratio = arts.count(album.artist.name)/len(arts)
+ if ratio >= 0.8:
+ candidates.append(album)
+ else:
+ self.log.debug('"%s" probably not an album of "%s" (ratio=%.2f)',
+ album, artist, ratio)
+ continue
+ return candidates