From: kaliko Date: Thu, 17 Jun 2021 11:46:53 +0000 (+0200) Subject: Remove to_add attibute X-Git-Tag: 0.18.0~31 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=af5f8a1cbde1e61ec1b44da676fae89c4358d184;p=mpd-sima.git Remove to_add attibute --- diff --git a/sima/lib/plugin.py b/sima/lib/plugin.py index 47e21f2..2cc284f 100644 --- a/sima/lib/plugin.py +++ b/sima/lib/plugin.py @@ -204,11 +204,17 @@ class AdvancedPlugin(Plugin): self.__class__.__name__, artist, album_to_queue) return album_to_queue - def filter_track(self, tracks, unplayed=False): + def filter_track(self, tracks, chosen=None, unplayed=False): """ Extract one unplayed track from a Track object list. * not in history * not already in the queue + + :param list(Track) tracks: List of tracks to chose from + :param list(Track) chosen: List of tracks previously chosen + :param bool unplayed: chose only unplayed (honoring history duration setting) + :return: A Track + :rtype: Track """ artist = tracks[0].Artist # In random play mode use complete playlist to filter @@ -227,7 +233,7 @@ class AdvancedPlugin(Plugin): # Should use albumartist heuristic as well if self.plugin_conf.getboolean('single_album', False): # pylint: disable=no-member albums = [tr.Album for tr in deny_list] - albums += [tr.Album for tr in self.to_add] + albums += [tr.Album for tr in chosen] if (trk.Album == self.player.current.Album or trk.Album in albums): self.log.debug('Found unplayed track ' + diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index 96aacc9..ca6b1f0 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -63,7 +63,6 @@ class WebService(AdvancedPlugin): super().__init__(daemon) self.history = daemon.short_history ## - self.to_add = list() self._cache = None self._flush_cache() wrapper = {'track': self._track, @@ -251,7 +250,7 @@ class WebService(AdvancedPlugin): def find_album(self, artists): """Find albums to queue. """ - self.to_add = list() + to_add = list() nb_album_add = 0 target_album_to_add = self.plugin_conf.getint('album_to_add') for artist in artists: @@ -267,19 +266,19 @@ class WebService(AdvancedPlugin): nbtracks = self.plugin_conf.getint('track_to_add_from_album') if nbtracks > 0: candidates = candidates[0:nbtracks] - self.to_add.extend(candidates) + to_add.extend(candidates) if nb_album_add == target_album_to_add: - return + return to_add def find_top(self, artists): """ find top tracks for artists in artists list. """ - self.to_add = list() + to_add = list() nbtracks_target = self.plugin_conf.getint('track_to_add') for artist in artists: - if len(self.to_add) == nbtracks_target: - return + if len(to_add) == nbtracks_target: + return to_add self.log.info('Looking for a top track for %s', artist) titles = deque() try: @@ -291,14 +290,17 @@ class WebService(AdvancedPlugin): found = self.player.search_track(artist, trk.title) if found: random.shuffle(found) - top_trk = self.filter_track(found) + top_trk = self.filter_track(found, to_add) if top_trk: - self.to_add.append(top_trk) + to_add.append(top_trk) break def _track(self): """Get some tracks for track queue mode + + :return: list of Tracks """ + to_add = [] artists = self.get_local_similar_artists() nbtracks_target = self.plugin_conf.getint('track_to_add') for artist in artists: @@ -309,30 +311,33 @@ class WebService(AdvancedPlugin): continue random.shuffle(found) # find tracks not in history for artist - track_candidate = self.filter_track(found) + track_candidate = self.filter_track(found, to_add) if track_candidate: - self.to_add.append(track_candidate) - if len(self.to_add) == nbtracks_target: + to_add.append(track_candidate) + self.log.info('%s plugin chose: %s', + self.ws.name, track_candidate) + if len(to_add) == nbtracks_target: break - if not self.to_add: - self.log.debug('Found no tracks to queue!') - return - for track in self.to_add: - self.log.info('%s plugin chose: %s', self.ws.name, track) + return to_add def _album(self): """Get albums for album queue mode + + :return: list of Tracks """ artists = self.get_local_similar_artists() - self.find_album(artists) + return self.find_album(artists) def _top(self): """Get some tracks for top track queue mode + + :return: list of Tracks """ artists = self.get_local_similar_artists() - self.find_top(artists) - for track in self.to_add: + chosen = self.find_top(artists) + for track in chosen: self.log.info('%s candidates: %s', self.ws.name, track) + return chosen def callback_need_track(self): self._cleanup_cache() @@ -343,14 +348,12 @@ class WebService(AdvancedPlugin): self.log.warning('No artist set for the last track in queue') self.log.debug(repr(self.player.current)) return None - self.queue_mode() + candidates = self.queue_mode() msg = ' '.join(['{0}: {1:>3d}'.format(k, v) for k, v in sorted(self.ws.stats.items())]) self.log.debug('http stats: ' + msg) - if not self.to_add: + if not candidates: self.log.info('%s plugin found nothing to queue', self.ws.name) - candidates = self.to_add - self.to_add = list() if self.plugin_conf.get('queue_mode') != 'album': random.shuffle(candidates) return candidates