]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/tags.py
Tags plugin configuration check and test
[mpd-sima.git] / sima / plugins / internal / tags.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2020 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 random
26
27 # third parties components
28 from musicpd import CommandError
29
30 # local import
31 from ...lib.plugin import Plugin
32 from ...lib.track import Track
33 from ...utils.utils import PluginException
34
35 def forge_filter(cfg):
36     tags = set(cfg.keys()) & Tags.supported_tags
37     cfg_filter = cfg.get('filter', None)
38     mpd_filter = []
39     if cfg_filter:
40         mpd_filter.append(cfg_filter)
41     for tag in tags:
42         if not cfg[tag]:  # avoid empty tags entries in config
43             continue
44         if ',' in cfg[tag]:
45             patt = '|'.join(map(str.strip, cfg[tag].split(',')))
46             mpd_filter.append(f"({tag} =~ '({patt})')")
47         else:
48             mpd_filter.append(f"({tag} == '{cfg[tag].strip()}')")
49     mpd_filter = ' AND '.join(mpd_filter)
50     if 'AND' in mpd_filter:
51         mpd_filter = f'({mpd_filter})'
52     return mpd_filter
53
54
55 class Tags(Plugin):
56     """Add track based on tags content
57     """
58     supported_tags = {'comment', 'date', 'genre', 'label', 'originaldate'}
59
60     def __init__(self, daemon):
61         super().__init__(daemon)
62         self.daemon = daemon
63         self._control_conf()
64         self._setup_tagsneeded()
65         self.mpd_filter = forge_filter(self.plugin_conf)
66         self.log.debug('mpd filter: %s', self.mpd_filter)
67
68     def _control_conf(self):
69         sup_tags = Tags.supported_tags
70         config_tags = {k for k, v in self.plugin_conf.items()
71                        if (v and k not in ['filter', 'priority'])}
72         if not self.plugin_conf.get('filter', None) and \
73                 config_tags.isdisjoint(sup_tags):
74             self.log.error('Found no config for %s plugin! '
75                            'Need at least "filter" or a supported tag', self)
76             self.log.info('Supported Tags are : %s', ', '.join(sup_tags))
77             raise PluginException('plugin misconfiguration')
78         if config_tags.difference(sup_tags):
79             self.log.error('Found unsupported tag in config: %s',
80                            config_tags.difference(sup_tags))
81             raise PluginException('plugin misconfiguration')
82
83     def _setup_tagsneeded(self):
84         config_tags = {k for k, v in self.plugin_conf.items() if v}
85         self.log.debug('%s plugin needs the followinng metadata: %s',
86                 self, config_tags & Tags.supported_tags)
87         tags = config_tags & Tags.supported_tags
88         self.player.needed_tags |= tags
89
90     def _get_history(self):
91         """Constructs list of already played artists.
92         """
93         duration = self.daemon.config.getint('sima', 'history_duration')
94         tracks_from_db = self.daemon.sdb.get_history(duration=duration)
95         hist = [Track(file=tr[3], artist=tr[0]) for tr in tracks_from_db]
96         return hist
97
98     def start(self):
99         if (0, 21, 0) > tuple(map(int, self.player.mpd_version.split('.'))):
100             self.log.warning('MPD protocol version: %s < 0.21.0',
101                     self.player.mpd_version)
102             self.log.error('Need at least MPD 0.21 to use Tags plugin (filters required)')
103             self.player.disconnect()
104             raise PluginException('MPD >= 0.21 required')
105         # Check filter is valid
106         try:
107             self.player.find(self.plugin_conf['filter'])
108         except CommandError:
109             raise PluginException('Badly formated filter in tags plugin configuration: "%s"'
110                                   % self.plugin_conf['filter'])
111
112     def callback_need_track(self):
113         candidates = []
114         target = self.plugin_conf.getint('track_to_add')
115         tracks = self.player.find(self.mpd_filter)
116         random.shuffle(tracks)
117         history = self._get_history()
118         while tracks:
119             trk = tracks.pop()
120             if trk in self.player.queue or \
121                trk in candidates:
122                 self.log.debug('%s already queued', trk)
123                 continue
124             if trk in history:
125                 self.log.debug('%s in history', trk)
126                 continue
127             candidates.append(trk)
128             self.log.info('Tags candidate: {}'.format(trk))
129             if len(candidates) >= target:
130                 break
131         if not candidates:
132             self.log.info('Tags plugin failed to find some tracks')
133         return candidates
134
135 # VIM MODLINE
136 # vim: ai ts=4 sw=4 sts=4 expandtab