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
28 from musicpd import CommandError
31 from ...lib.plugin import AdvancedPlugin
32 from ...lib.meta import Artist
33 from ...utils.utils import PluginException
36 def forge_filter(cfg):
37 tags = set(cfg.keys()) & Tags.supported_tags
38 cfg_filter = cfg.get('filter', None)
41 mpd_filter.append(cfg_filter)
43 if not cfg[tag]: # avoid empty tags entries in config
46 patt = '|'.join(map(str.strip, cfg[tag].split(',')))
47 mpd_filter.append(f"({tag} =~ '({patt})')")
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})'
56 class Tags(AdvancedPlugin):
57 """Add track based on tags content
59 supported_tags = {'comment', 'date', 'genre', 'label', 'originaldate'}
60 options = {'queue_mode', 'priority', 'filter', 'track_to_add', 'album_to_add'}
62 def __init__(self, daemon):
63 super().__init__(daemon)
65 self.mpd_filter = forge_filter(self.plugin_conf)
66 self._setup_tagsneeded()
67 self.log.debug('mpd filter: %s', self.mpd_filter)
69 def _control_conf(self):
70 sup_tags = Tags.supported_tags
71 config_tags = {k for k, v in self.plugin_conf.items()
72 if (v and k not in Tags.options)}
73 if not self.plugin_conf.get('filter', None) and \
74 config_tags.isdisjoint(sup_tags):
75 self.log.error('Found no config for %s plugin! '
76 'Need at least "filter" or a supported tag', self)
77 self.log.info('Supported Tags are : %s', ', '.join(sup_tags))
78 raise PluginException('plugin misconfiguration')
79 if config_tags.difference(sup_tags):
80 self.log.error('Found unsupported tag in config: %s',
81 config_tags.difference(sup_tags))
82 raise PluginException('plugin misconfiguration')
84 def _setup_tagsneeded(self):
85 """Ensure needed tags are exposed by MPD"""
86 # At this point mpd_filter concatenetes {tags}+filter
88 for mpd_supp_tags in self.player.MPD_supported_tags:
89 if mpd_supp_tags.lower() in self.mpd_filter.lower():
90 config_tags.add(mpd_supp_tags.lower())
91 self.log.debug('%s plugin needs the following metadata: %s',
93 tags = config_tags & Tags.supported_tags
94 self.player.needed_tags |= tags
97 if (0, 21, 0) > tuple(map(int, self.player.mpd_version.split('.'))):
98 self.log.warning('MPD protocol version: %s < 0.21.0',
99 self.player.mpd_version)
101 'Need at least MPD 0.21 to use Tags plugin (filters required)')
102 self.player.disconnect()
103 raise PluginException('MPD >= 0.21 required')
104 # Check filter is valid
106 if self.plugin_conf['filter']:
107 # Use window to limit response size
108 self.player.find(self.plugin_conf['filter'], "window", (0, 1))
110 raise PluginException('Badly formated filter in tags plugin configuration: "%s"'
111 % self.plugin_conf['filter'])
113 def callback_need_track_(self):
115 queue_mode = self.plugin_conf.get('queue_mode', 'track')
116 target = self.plugin_conf.getint(f'{queue_mode}_to_add')
117 tracks = self.player.find(self.mpd_filter)
118 random.shuffle(tracks)
119 history = self.get_history()
122 if trk in self.player.queue or \
124 self.log.debug('%s already queued', trk)
127 self.log.debug('%s in history', trk)
129 candidates.append(trk)
130 self.log.info('Tags candidate: {}'.format(trk))
131 if len(candidates) >= target:
133 if queue_mode == 'track':
135 if queue_mode == 'album':
136 for trk in candidates:
137 self.log.info(trk.Artist)
138 _ = self.album_candidate(trk.Artist)
140 self.log.info('Tags plugin failed to find some tracks')
143 def callback_need_track(self):
145 queue_mode = self.plugin_conf.get('queue_mode', 'track')
146 target = self.plugin_conf.getint(f'{queue_mode}_to_add')
147 # look for artists acording to filter
148 artists = self.player.list('artist', self.mpd_filter)
149 random.shuffle(artists)
150 artists = self.get_reorg_artists_list(artists)
151 self.log.debug('Tags candidates: %s', ' / '.join(artists))
152 for artist in artists:
153 if artist in {t.Artist for t in self.player.queue}:
155 self.log.debug('looking for %s', artist)
156 trk = self.filter_track(self.player.find_tracks(Artist(name=artist)))
159 if queue_mode == 'track':
160 self.log.info('Tags candidate: {}'.format(trk))
161 candidates.append(trk)
162 if len(candidates) == target:
165 album = self.album_candidate(trk.Artist, unplayed=True)
168 candidates.extend(self.player.find_tracks(album))
169 if len({t.album for t in candidates}) == target:
174 # vim: ai ts=4 sw=4 sts=4 expandtab