]> kaliko git repositories - mpd-sima.git/commitdiff
Uniform use of MetaContainer
authorkaliko <kaliko@azylum.org>
Mon, 21 Dec 2020 16:40:41 +0000 (17:40 +0100)
committerkaliko <kaliko@azylum.org>
Mon, 21 Dec 2020 16:40:41 +0000 (17:40 +0100)
sima/lib/plugin.py
sima/lib/simadb.py
sima/lib/webserv.py
sima/plugins/internal/tags.py

index c38683af971e6aad172fe87cbfbe20750be7499b..37ce214aa24be30bffc7ea259e57040eb05d447b 100644 (file)
@@ -24,7 +24,7 @@ Plugin object to derive from
 import random
 
 from .track import Track
 import random
 
 from .track import Track
-from .meta import Album, Artist
+from .meta import Album, Artist, MetaContainer
 
 
 class Plugin:
 
 
 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.
 
         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,
         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)
                     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
         reorg = [art for art in not_queued_artist if art not in hist]
         reorg.extend(hist)
         return reorg
index 76b29cf4cbd3363b31139dd007328f455dcb4b7b..a3e55703641b480d1c237ac39b5137f70d2e46dd 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
 # -*- coding: utf-8 -*-
 #
-# Copyright (c) 2009-2013, 2019 kaliko <kaliko@azylum.org>
+# Copyright (c) 2009-2013, 2019-2020 kaliko <kaliko@azylum.org>
 # Copyright (c) 2009, Eric Casteleijn <thisfred@gmail.com>
 # Copyright (c) 2008 Rick van Hattem
 #
 # Copyright (c) 2009, Eric Casteleijn <thisfred@gmail.com>
 # Copyright (c) 2008 Rick van Hattem
 #
@@ -324,6 +324,8 @@ class SimaDB(object):
 
     def get_artists_history(self, artists, duration=__HIST_DURATION__):
         """
 
     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()
         """
         date = datetime.utcnow() - timedelta(hours=duration)
         connection = self.get_database_connection()
index 4b3e2989666db4c782a893dc378b4fc319ae0205..0770ea8ae0afe4b84d96e3df0cdd13923b412016 100644 (file)
@@ -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
             # 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',
                 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))
         # 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
         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:
         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'):
             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)
         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
             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:
             # find tracks not in history for artist
             track_candidate = self.filter_track(found)
             if track_candidate:
index e0d26d2f277f4ae3540f4f63f7211e09823f003a..76a2b7bf695364605ef6ca93a1434c003aaf8829 100644 (file)
@@ -29,7 +29,7 @@ from musicpd import CommandError
 
 # local import
 from ...lib.plugin import AdvancedPlugin
 
 # local import
 from ...lib.plugin import AdvancedPlugin
-from ...lib.meta import Artist
+from ...lib.meta import Artist, MetaContainer
 from ...utils.utils import PluginException
 
 
 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
         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)
         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)
         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
             trk = self.filter_track(tracks)
             if not trk:
                 continue