+ def _detects_var_artists_album(self, album, artist):
+ """Detects either an album is a "Various Artists" or a
+ single artist release."""
+ art_first_track = None
+ for track in self.player.find_album(artist, album):
+ if not art_first_track: # set artist for the first track
+ art_first_track = track.artist
+ alb_art = track.albumartist
+ # Special heuristic used when AlbumArtist is available
+ if (alb_art):
+ if artist == alb_art:
+ # When album artist field is similar to the artist we're
+ # looking an album for, the album is considered good to
+ # queue
+ return False
+ else:
+ self.log.debug(track)
+ self.log.debug('album art says "%s", looking for "%s",'
+ ' not queueing this album' %
+ (alb_art, artist))
+ return True
+ return False
+
+ def _get_album_history(self, artist=None):
+ """Retrieve album history"""
+ duration = self.daemon_conf.getint('sima', 'history_duration')
+ albums_list = set()
+ for trk in self.sdb.get_history(artist=artist, duration=duration):
+ albums_list.add(trk[1])
+ return albums_list
+
+ def find_album(self, artists):
+ """Find albums to queue.
+ """
+ self.to_add = list()
+ nb_album_add = 0
+ target_album_to_add = int(self.plugin_conf.get('album_to_add'))
+ for artist in artists:
+ self.log.info('Looking for an album to add for "%s"...' % artist)
+ albums = set(self.player.find_albums(artist))
+ # albums yet in history for this artist
+ albums_yet_in_hist = albums & self._get_album_history(artist=artist)
+ albums_not_in_hist = list(albums - albums_yet_in_hist)
+ # Get to next artist if there are no unplayed albums
+ if not albums_not_in_hist:
+ self.log.info('No album found for "%s"' % artist)
+ continue
+ album_to_queue = str()
+ random.shuffle(albums_not_in_hist)
+ for album in albums_not_in_hist:
+ tracks = self.player.find('album', album)
+ if self._detects_var_artists_album(album, artist):
+ continue
+ if tracks and self.sdb.get_bl_album(tracks[0], add_not=True):
+ self.log.info('Blacklisted album: "%s"' % album)
+ self.log.debug('using track: "%s"' % tracks[0])
+ continue
+ # Look if one track of the album is already queued
+ # Good heuristic, at least enough to guess if the whole album is
+ # already queued.
+ if tracks[0] in self.player.queue:
+ self.log.debug('"%s" already queued, skipping!' %
+ tracks[0].album)
+ continue
+ album_to_queue = album
+ if not album_to_queue:
+ self.log.info('No album found for "%s"' % artist)
+ continue
+ self.log.info('last.fm album candidate: {0} - {1}'.format(
+ artist, album_to_queue))
+ nb_album_add += 1
+ self.to_add.extend(self.player.find_album(artist, album_to_queue))
+ if nb_album_add == target_album_to_add:
+ return True
+