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