]> kaliko git repositories - mpd-sima.git/blobdiff - sima/plugins/internal/tags.py
Fixed issues in reorg and album_candidate
[mpd-sima.git] / sima / plugins / internal / tags.py
index cd1a4fe3bbdf91660c8be23a2c15d5e304bd4b29..ad326ef3a1f3f587c3ba6c89332a847aaa072426 100644 (file)
@@ -25,11 +25,13 @@ Add titles based on tags
 import random
 
 # third parties components
+from musicpd import CommandError
 
 # local import
-from ...lib.plugin import Plugin
-from ...lib.track import Track
-from ...utils.utils import PluginConfException
+from ...lib.plugin import AdvancedPlugin
+from ...lib.meta import Artist
+from ...utils.utils import PluginException
+
 
 def forge_filter(cfg):
     tags = set(cfg.keys()) & Tags.supported_tags
@@ -38,8 +40,10 @@ def forge_filter(cfg):
     if cfg_filter:
         mpd_filter.append(cfg_filter)
     for tag in tags:
+        if not cfg[tag]:  # avoid empty tags entries in config
+            continue
         if ',' in cfg[tag]:
-            patt = '|'.join(cfg[tag].split(','))
+            patt = '|'.join(map(str.strip, cfg[tag].split(',')))
             mpd_filter.append(f"({tag} =~ '({patt})')")
         else:
             mpd_filter.append(f"({tag} == '{cfg[tag].strip()}')")
@@ -49,68 +53,90 @@ def forge_filter(cfg):
     return mpd_filter
 
 
-class Tags(Plugin):
+class Tags(AdvancedPlugin):
     """Add track based on tags content
     """
     supported_tags = {'comment', 'date', 'genre', 'label', 'originaldate'}
+    options = {'queue_mode', 'priority', 'filter', 'track_to_add', 'album_to_add'}
 
     def __init__(self, daemon):
         super().__init__(daemon)
-        self.daemon = daemon
         self._control_conf()
-        #self._control_server()
-        self._setup_tagsneeded()
         self.mpd_filter = forge_filter(self.plugin_conf)
+        self._setup_tagsneeded()
         self.log.debug('mpd filter: %s', self.mpd_filter)
 
     def _control_conf(self):
         sup_tags = Tags.supported_tags
+        config_tags = {k for k, v in self.plugin_conf.items()
+                       if (v and k not in Tags.options)}
         if not self.plugin_conf.get('filter', None) and \
-                self.plugin_conf.keys().isdisjoint(sup_tags):
-            self.log.error(
-                'Found no config for %s plugin! Need at least "filter" or a supported tag' % self)
+                config_tags.isdisjoint(sup_tags):
+            self.log.error('Found no config for %s plugin! '
+                           'Need at least "filter" or a supported tag', self)
             self.log.info('Supported Tags are : %s', ', '.join(sup_tags))
-            raise PluginConfException('plugin misconfiguration')
-
-    def _control_server(self):
-        #TODO:
-        # * control tags used are available
-        # * filters are available mpd version >= 0.21
-        raise NotImplemented
+            raise PluginException('plugin misconfiguration')
+        if config_tags.difference(sup_tags):
+            self.log.error('Found unsupported tag in config: %s',
+                           config_tags.difference(sup_tags))
+            raise PluginException('plugin misconfiguration')
 
     def _setup_tagsneeded(self):
-        tags = set(self.plugin_conf.keys()) & Tags.supported_tags
+        """Ensure needed tags are exposed by MPD"""
+        # At this point mpd_filter concatenetes {tags}+filter
+        config_tags = set()
+        for mpd_supp_tags in self.player.MPD_supported_tags:
+            if mpd_supp_tags.lower() in self.mpd_filter.lower():
+                config_tags.add(mpd_supp_tags.lower())
+        self.log.debug('%s plugin needs the following metadata: %s',
+                       self, config_tags)
+        tags = config_tags & Tags.supported_tags
         self.player.needed_tags |= tags
 
-    def _get_history(self):
-        """Constructs list of already played artists.
-        """
-        duration = self.daemon.config.getint('sima', 'history_duration')
-        tracks_from_db = self.daemon.sdb.get_history(duration=duration)
-        hist = [Track(file=tr[3], artist=tr[0]) for tr in tracks_from_db]
-        return hist
+    def start(self):
+        if (0, 21, 0) > tuple(map(int, self.player.mpd_version.split('.'))):
+            self.log.warning('MPD protocol version: %s < 0.21.0',
+                             self.player.mpd_version)
+            self.log.error(
+                'Need at least MPD 0.21 to use Tags plugin (filters required)')
+            self.player.disconnect()
+            raise PluginException('MPD >= 0.21 required')
+        # Check filter is valid
+        try:
+            if self.plugin_conf['filter']:
+                # Use window to limit response size
+                self.player.find(self.plugin_conf['filter'], "window", (0, 1))
+        except CommandError:
+            raise PluginException('Badly formated filter in tags plugin configuration: "%s"'
+                                  % self.plugin_conf['filter'])
 
     def callback_need_track(self):
         candidates = []
-        target = self.plugin_conf.getint('track_to_add')
-        tracks = self.player.find(self.mpd_filter)
-        random.shuffle(tracks)
-        history = self._get_history()
-        while tracks:
-            trk = tracks.pop()
-            if trk in self.player.queue or \
-               trk in candidates:
-                self.log.debug('%s already queued', trk)
-                continue
-            if trk in history:
-                self.log.debug('%s in history', trk)
+        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 = self.get_reorg_artists_list(artists)
+        self.log.debug('Tags artists found: %s', ' / '.join(artists))
+        for artist in artists:
+            self.log.debug('looking for %s', artist)
+            tracks = self.player.find_tracks(Artist(name=artist))
+            trk = self.filter_track(tracks)
+            if not trk:
                 continue
-            candidates.append(trk)
-            self.log.info('Tags candidate: {}'.format(trk))
-            if len(candidates) >= target:
-                break
-        if not candidates:
-            self.log.info('Tags plugin failed to find some tracks')
+            if queue_mode == 'track':
+                self.log.info('Tags plugin chose: {}'.format(trk))
+                candidates.append(trk)
+                if len(candidates) == target:
+                    break
+            else:
+                album = self.album_candidate(trk.Artist, unplayed=True)
+                if not album:
+                    continue
+                candidates.extend(self.player.find_tracks(album))
+                if len({t.album for t in candidates}) == target:
+                    break
         return candidates
 
 # VIM MODLINE