1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2020 kaliko <kaliko@azylum.org>
4 # This file is part of sima
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.
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.
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/>.
21 Add titles based on tags
24 # standard library import
27 # third parties components
30 from ...lib.plugin import Plugin
31 from ...lib.track import Track
32 from ...utils.utils import PluginConfException
34 def forge_filter(cfg):
35 tags = set(cfg.keys()) & Tags.supported_tags
36 cfg_filter = cfg.get('filter', None)
39 mpd_filter.append(cfg_filter)
42 patt = '|'.join(cfg[tag].split(','))
43 mpd_filter.append(f"({tag} =~ '({patt})')")
45 mpd_filter.append(f"({tag} == '{cfg[tag].strip()}')")
46 mpd_filter = ' AND '.join(mpd_filter)
47 if 'AND' in mpd_filter:
48 mpd_filter = f'({mpd_filter})'
53 """Add track based on tags content
55 supported_tags = {'comment', 'date', 'genre', 'label', 'originaldate'}
57 def __init__(self, daemon):
58 super().__init__(daemon)
61 #self._control_server()
62 self._setup_tagsneeded()
63 self.mpd_filter = forge_filter(self.plugin_conf)
64 self.log.debug('mpd filter: %s', self.mpd_filter)
66 def _control_conf(self):
67 sup_tags = Tags.supported_tags
68 if not self.plugin_conf.get('filter', None) and \
69 self.plugin_conf.keys().isdisjoint(sup_tags):
71 'Found no config for %s plugin! Need at least "filter" or a supported tag' % self)
72 self.log.info('Supported Tags are : %s', ', '.join(sup_tags))
73 raise PluginConfException('plugin misconfiguration')
75 def _control_server(self):
77 # * control tags used are available
78 # * filters are available mpd version >= 0.21
81 def _setup_tagsneeded(self):
82 tags = set(self.plugin_conf.keys()) & Tags.supported_tags
83 self.player.needed_tags |= tags
85 def _get_history(self):
86 """Constructs list of already played artists.
88 duration = self.daemon.config.getint('sima', 'history_duration')
89 tracks_from_db = self.daemon.sdb.get_history(duration=duration)
90 hist = [Track(file=tr[3], artist=tr[0]) for tr in tracks_from_db]
93 def callback_need_track(self):
95 target = self.plugin_conf.getint('track_to_add')
96 tracks = self.player.find(self.mpd_filter)
97 random.shuffle(tracks)
98 history = self._get_history()
101 if trk in self.player.queue or \
103 self.log.debug('%s already queued', trk)
106 self.log.debug('%s in history', trk)
108 candidates.append(trk)
109 self.log.info('Tags candidate: {}'.format(trk))
110 if len(candidates) >= target:
113 self.log.info('Tags plugin failed to find some tracks')
117 # vim: ai ts=4 sw=4 sts=4 expandtab