]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/tags.py
More robust Tags plugin MPD filter configuration
[mpd-sima.git] / sima / plugins / internal / tags.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2020, 2021 kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
6 #  sima is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  sima is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #
20 """
21 Add titles based on tags
22 """
23
24 # standard library import
25 import logging
26 import random
27
28 # third parties components
29 from musicpd import CommandError
30
31 # local import
32 from ...lib.plugin import AdvancedPlugin
33 from ...lib.meta import Artist, MetaContainer
34 from ...utils.utils import PluginException
35
36
37 def control_config(tags_config):
38     log = logging.getLogger('sima')
39     sup_tags = Tags.supported_tags
40     config_tags = {k for k, v in tags_config.items()
41                    if (v and k in Tags.supported_tags)}
42     if not tags_config.get('filter', None) and \
43             config_tags.isdisjoint(sup_tags):
44         log.warning('Found no config for Tags plugin! '
45                     'Need at least "filter" or a supported tag')
46         log.info('Supported Tags are : %s', ', '.join(sup_tags))
47         return False
48     if config_tags.difference(sup_tags):
49         log.error('Found unsupported tag in config: %s',
50                   config_tags.difference(sup_tags))
51         return False
52     return True
53
54
55 def forge_filter(cfg, logger):
56     """forge_filter merges tags config and user defined MPD filter into a single
57     MPD filter"""
58     tags = set(cfg.keys()) & Tags.supported_tags
59     cfg_filter = cfg.get('filter', None)
60     # Remove external enclosing parentheses in user defined MPD filter, for
61     # instance  when there is more than one expression:
62     #     ((genre == 'rock' ) AND (date =~ '198.'))
63     # Even though it's a valid MPD filter, forge_filter will enclose it
64     # properly. We do not want to through a syntax error at users since it's a
65     # valid MPD filter, hence trying to transparently reformat the filter
66     if cfg_filter.startswith('((') and cfg_filter.endswith('))'):
67         logger.debug('Drop external enclosing parentheses in user filter: %s',
68                      cfg_filter[1:-1])
69         cfg['filter'] = cfg_filter[1:-1]
70         cfg_filter = cfg['filter']
71     mpd_filter = []
72     if cfg_filter:
73         mpd_filter.append(cfg_filter)
74     for tag in tags:
75         if not cfg[tag]:  # avoid empty tags entries in config
76             continue
77         if ',' in cfg[tag]:
78             patt = '|'.join(map(str.strip, cfg[tag].split(',')))
79             mpd_filter.append(f"({tag} =~ '({patt})')")
80         else:
81             mpd_filter.append(f"({tag} == '{cfg[tag].strip()}')")
82     mpd_filter = ' AND '.join(mpd_filter)
83     # Ensure there is at least an artist name
84     mpd_filter = f"({mpd_filter} AND (artist != ''))"
85     return mpd_filter
86
87
88 class Tags(AdvancedPlugin):
89     """Add track based on tags content
90     """
91     supported_tags = {'comment', 'date', 'genre', 'label', 'originaldate'}
92     # options = {'queue_mode', 'priority', 'filter', 'track_to_add',
93     #            'album_to_add'}
94
95     def __init__(self, daemon):
96         super().__init__(daemon)
97         self._control_conf()
98         self.mpd_filter = forge_filter(self.plugin_conf, self.log)
99         self._setup_tagsneeded()
100         self.log.debug('mpd filter: %s', self.mpd_filter)
101
102     def _control_conf(self):
103         if not control_config(self.plugin_conf):
104             raise PluginException('plugin misconfiguration')
105
106     def _setup_tagsneeded(self):
107         """Ensure needed tags are exposed by MPD"""
108         # At this point mpd_filter concatenetes {tags}+filter
109         config_tags = set()
110         for mpd_supp_tags in self.player.MPD_supported_tags:
111             if mpd_supp_tags.lower() in self.mpd_filter.lower():
112                 config_tags.add(mpd_supp_tags.lower())
113         self.log.debug('%s plugin needs the following metadata: %s',
114                        self, config_tags)
115         tags = config_tags & Tags.supported_tags
116         self.player.needed_tags |= tags
117
118     def start(self):
119         if (0, 21, 0) > tuple(map(int, self.player.mpd_version.split('.'))):
120             self.log.warning('MPD protocol version: %s < 0.21.0',
121                              self.player.mpd_version)
122             self.log.error(
123                 'Need at least MPD 0.21 to use Tags plugin (filters required)')
124             self.player.disconnect()
125             raise PluginException('MPD >= 0.21 required')
126         if not self.plugin_conf['filter']:
127             return
128         # Check filter is valid
129         try:
130             # Use window to limit response size
131             self.player.find(self.mpd_filter, "window", (0, 1))
132         except CommandError as err:
133             self.log.warning(err)
134             raise PluginException('Badly formated filter in tags plugin configuration: "%s"'
135                                   % self.plugin_conf['filter']) from err
136
137     def callback_need_track(self):
138         candidates = []
139         queue_mode = self.plugin_conf.get('queue_mode', 'track')
140         target = self.plugin_conf.getint(f'{queue_mode}_to_add')
141         # look for artists acording to filter
142         artists = [Artist(name=a) for a in self.player.list('artist', self.mpd_filter)]
143         random.shuffle(artists)
144         artists = MetaContainer(artists)
145         if not artists:
146             self.log.info('Tags plugin found nothing to queue')
147             return candidates
148         artists = self.get_reorg_artists_list(artists)
149         self.log.debug('Tags plugin found: %s', ' / '.join(map(str, artists)))
150         for artist in artists:
151             self.log.debug('looking for %s', artist)
152             tracks = self.player.find_tracks(artist)
153             if not tracks:
154                 continue
155             trk = self.filter_track(tracks, candidates)
156             if not trk:
157                 continue
158             if queue_mode == 'track':
159                 self.log.info('Tags plugin chose: %s', trk)
160                 candidates.append(trk)
161                 if len(candidates) == target:
162                     break
163             else:
164                 album = self.album_candidate(trk.Artist, unplayed=True)
165                 if not album:
166                     continue
167                 candidates.extend(self.player.find_tracks(album))
168                 if len({t.album for t in candidates}) == target:
169                     break
170         return candidates
171
172 # VIM MODLINE
173 # vim: ai ts=4 sw=4 sts=4 expandtab