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