From: kaliko Date: Mon, 21 Dec 2020 16:40:41 +0000 (+0100) Subject: Uniform use of MetaContainer X-Git-Tag: 0.16.1~4 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=c806cb3dbeee61c9c4d023f75a862a33d32ef026;p=mpd-sima.git Uniform use of MetaContainer --- diff --git a/sima/lib/plugin.py b/sima/lib/plugin.py index c38683a..37ce214 100644 --- a/sima/lib/plugin.py +++ b/sima/lib/plugin.py @@ -24,7 +24,7 @@ Plugin object to derive from import random from .track import Track -from .meta import Album, Artist +from .meta import Album, Artist, MetaContainer class Plugin: @@ -163,10 +163,10 @@ class AdvancedPlugin(Plugin): Move around items in alist in order to have first not recently played (or about to be played) artists. - :param list(str) alist: artist name list (Not an Artist object) + :param {Artist} alist: Artist objects list/container """ - queued_artist = {_.artist for _ in self.player.queue} - not_queued_artist = set(alist) - queued_artist + queued_artist = MetaContainer([Artist(_.artist) for _ in self.player.queue]) + not_queued_artist = alist - queued_artist duration = self.main_conf.getint('sima', 'history_duration') hist = [] for art in self.sdb.get_artists_history(alist, @@ -176,7 +176,7 @@ class AdvancedPlugin(Plugin): hist.insert(0, art) else: hist.append(art) - # Find not recently played (not in history) + # Find not recently played (not in history) & not in queue reorg = [art for art in not_queued_artist if art not in hist] reorg.extend(hist) return reorg diff --git a/sima/lib/simadb.py b/sima/lib/simadb.py index 76b29cf..a3e5570 100644 --- a/sima/lib/simadb.py +++ b/sima/lib/simadb.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2009-2013, 2019 kaliko +# Copyright (c) 2009-2013, 2019-2020 kaliko # Copyright (c) 2009, Eric Casteleijn # Copyright (c) 2008 Rick van Hattem # @@ -324,6 +324,8 @@ class SimaDB(object): def get_artists_history(self, artists, duration=__HIST_DURATION__): """ + :param list artists: list of object that can evaluate equality with + artist name, iterable of str or Artist object """ date = datetime.utcnow() - timedelta(hours=duration) connection = self.get_database_connection() diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index 4b3e298..0770ea8 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -214,9 +214,7 @@ class WebService(AdvancedPlugin): # get them reorg to pick up best element ret_extra = self.get_reorg_artists_list(ret_extra) # tries to pickup less artist from extra art - if len(ret) < 4: - ret_extra = MetaContainer(ret_extra) - else: + if len(ret) > 4: ret_extra = MetaContainer(ret_extra[:max(4, len(ret))//2]) if ret_extra: self.log.debug('extra found in library: %s', @@ -245,7 +243,7 @@ class WebService(AdvancedPlugin): # Move around similars items to get in unplayed|not recently played # artist first. self.log.info('Got %d artists in library', len(ret)) - candidates = self.get_reorg_artists_list(list(ret)) + candidates = self.get_reorg_artists_list(ret) if candidates: self.log.info(' / '.join(map(str, candidates))) return candidates @@ -266,7 +264,9 @@ class WebService(AdvancedPlugin): nb_album_add = 0 target_album_to_add = self.plugin_conf.getint('album_to_add') for artist in artists: - album = self.album_candidate(artist) + album = self.album_candidate(artist, unplayed=True) + if not album: + continue nb_album_add += 1 candidates = self.player.find_tracks(album) if self.plugin_conf.getboolean('shuffle_album'): @@ -313,10 +313,10 @@ class WebService(AdvancedPlugin): for artist in artists: self.log.debug('Trying to find titles to add for "%r"', artist) found = self.player.find_tracks(artist) - random.shuffle(found) if not found: self.log.debug('Found nothing to queue for %s', artist) continue + random.shuffle(found) # find tracks not in history for artist track_candidate = self.filter_track(found) if track_candidate: diff --git a/sima/plugins/internal/tags.py b/sima/plugins/internal/tags.py index e0d26d2..76a2b7b 100644 --- a/sima/plugins/internal/tags.py +++ b/sima/plugins/internal/tags.py @@ -29,7 +29,7 @@ from musicpd import CommandError # local import from ...lib.plugin import AdvancedPlugin -from ...lib.meta import Artist +from ...lib.meta import Artist, MetaContainer from ...utils.utils import PluginException @@ -115,13 +115,15 @@ class Tags(AdvancedPlugin): queue_mode = self.plugin_conf.get('queue_mode', 'track') target = self.plugin_conf.getint(f'{queue_mode}_to_add') # look for artists acording to filter - artists = self.player.list('artist', self.mpd_filter) - random.shuffle(artists) + artists = MetaContainer([Artist(name=a) for a in self.player.list('artist', self.mpd_filter)]) + if not artists: + self.log.info('Tags plugin found nothing to queue') + return candidates artists = self.get_reorg_artists_list(artists) - self.log.debug('Tags artists found: %s', ' / '.join(artists)) + self.log.debug('Tags plugin found: %s', ' / '.join(map(str, artists))) for artist in artists: self.log.debug('looking for %s', artist) - tracks = self.player.find_tracks(Artist(name=artist)) + tracks = self.player.find_tracks(artist) trk = self.filter_track(tracks) if not trk: continue