]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/tags.py
Add album queue mode to Tags plugin
[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, AdvancedLookUp
32 from ...lib.meta import Artist, Album
33 from ...utils.utils import PluginException
34
35
36 def forge_filter(cfg):
37     tags = set(cfg.keys()) & Tags.supported_tags
38     cfg_filter = cfg.get('filter', None)
39     mpd_filter = []
40     if cfg_filter:
41         mpd_filter.append(cfg_filter)
42     for tag in tags:
43         if not cfg[tag]:  # avoid empty tags entries in config
44             continue
45         if ',' in cfg[tag]:
46             patt = '|'.join(map(str.strip, cfg[tag].split(',')))
47             mpd_filter.append(f"({tag} =~ '({patt})')")
48         else:
49             mpd_filter.append(f"({tag} == '{cfg[tag].strip()}')")
50     mpd_filter = ' AND '.join(mpd_filter)
51     if 'AND' in mpd_filter:
52         mpd_filter = f'({mpd_filter})'
53     return mpd_filter
54
55
56 class Tags(Plugin, AdvancedLookUp):
57     """Add track based on tags content
58     """
59     supported_tags = {'comment', 'date', 'genre', 'label', 'originaldate'}
60     options = {'queue_mode', 'priority', 'filter', 'track_to_add', 'album_to_add'}
61
62     def __init__(self, daemon):
63         super().__init__(daemon)
64         self.daemon = daemon
65         self._control_conf()
66         self.mpd_filter = forge_filter(self.plugin_conf)
67         self._setup_tagsneeded()
68         self.log.debug('mpd filter: %s', self.mpd_filter)
69
70     def _control_conf(self):
71         sup_tags = Tags.supported_tags
72         config_tags = {k for k, v in self.plugin_conf.items()
73                        if (v and k not in Tags.options)}
74         if not self.plugin_conf.get('filter', None) and \
75                 config_tags.isdisjoint(sup_tags):
76             self.log.error('Found no config for %s plugin! '
77                            'Need at least "filter" or a supported tag', self)
78             self.log.info('Supported Tags are : %s', ', '.join(sup_tags))
79             raise PluginException('plugin misconfiguration')
80         if config_tags.difference(sup_tags):
81             self.log.error('Found unsupported tag in config: %s',
82                            config_tags.difference(sup_tags))
83             raise PluginException('plugin misconfiguration')
84
85     def _setup_tagsneeded(self):
86         """Ensure needed tags are exposed by MPD"""
87         # At this point mpd_filter concatenetes {tags}+filter
88         config_tags = set()
89         for mpd_supp_tags in self.player.MPD_supported_tags:
90             if mpd_supp_tags.lower() in self.mpd_filter.lower():
91                 config_tags.add(mpd_supp_tags.lower())
92         self.log.debug('%s plugin needs the following metadata: %s',
93                        self, config_tags)
94         tags = config_tags & Tags.supported_tags
95         self.player.needed_tags |= tags
96
97     def start(self):
98         if (0, 21, 0) > tuple(map(int, self.player.mpd_version.split('.'))):
99             self.log.warning('MPD protocol version: %s < 0.21.0',
100                              self.player.mpd_version)
101             self.log.error(
102                 '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             if self.plugin_conf['filter']:
108                 # Use window to limit response size
109                 self.player.find(self.plugin_conf['filter'], "window", (0, 1))
110         except CommandError:
111             raise PluginException('Badly formated filter in tags plugin configuration: "%s"'
112                                   % self.plugin_conf['filter'])
113
114     def callback_need_track_(self):
115         candidates = []
116         queue_mode = self.plugin_conf.get('queue_mode', 'track')
117         target = self.plugin_conf.getint(f'{queue_mode}_to_add')
118         tracks = self.player.find(self.mpd_filter)
119         random.shuffle(tracks)
120         history = self.get_history()
121         while tracks:
122             trk = tracks.pop()
123             if trk in self.player.queue or \
124                trk in candidates:
125                 self.log.debug('%s already queued', trk)
126                 continue
127             if trk in history:
128                 self.log.debug('%s in history', trk)
129                 continue
130             candidates.append(trk)
131             self.log.info('Tags candidate: {}'.format(trk))
132             if len(candidates) >= target:
133                 break
134         if queue_mode == 'track':
135             return candidates
136         if queue_mode == 'album':
137             for trk in candidates:
138                 self.log.info(trk.Artist)
139                 _ = self.album_candidate(trk.Artist)
140         if not candidates:
141             self.log.info('Tags plugin failed to find some tracks')
142         return candidates
143
144     def callback_need_track(self):
145         candidates = []
146         queue_mode = self.plugin_conf.get('queue_mode', 'track')
147         target = self.plugin_conf.getint(f'{queue_mode}_to_add')
148         # look for artists acording to filter
149         artists = self.player.list('artist', self.mpd_filter)
150         random.shuffle(artists)
151         artists = self.get_reorg_artists_list(artists)
152         self.log.debug('Tags candidates: %s', ' / '.join(artists))
153         for artist in artists:
154             if artist in {t.Artist for t in self.player.queue}:
155                 continue
156             self.log.debug('looking for %s', artist)
157             trk = self.filter_track(self.player.find_tracks(Artist(name=artist)))
158             if not trk:
159                 continue
160             if queue_mode == 'track':
161                 self.log.info('Tags candidate: {}'.format(trk))
162                 candidates.append(trk)
163                 if len(candidates) == target:
164                     break
165             else:
166                 album = self.album_candidate(trk.Artist, unplayed=True)
167                 if not album:
168                     continue
169                 candidates.extend(self.player.find_tracks(album))
170                 if len({t.album for t in candidates}) == target:
171                     break
172         return candidates
173
174 # VIM MODLINE
175 # vim: ai ts=4 sw=4 sts=4 expandtab