1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2020-2021 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 Genre tag
24 # standard library import
25 from collections import Counter
26 from random import shuffle
28 # third parties components
31 from ...lib.plugin import AdvancedPlugin
32 from ...lib.meta import Artist, MetaContainer
33 from ...utils.utils import PluginException
36 def forge_filter(genre):
37 mpd_filter = f"(Genre == '{genre.strip()}')"
38 # Ensure there is at least an artist name
39 mpd_filter = f"({mpd_filter} AND (artist != ''))"
43 class Genre(AdvancedPlugin):
44 """Add track based on tags content
47 def __init__(self, daemon):
48 super().__init__(daemon)
49 self._setup_tagsneeded()
51 def _setup_tagsneeded(self):
52 """Ensure needed tags are exposed by MPD"""
53 self.log.debug('%s plugin needs the following metadata: Genre', self)
54 self.player.needed_tags |= {'genre'}
57 if (0, 21, 0) > tuple(map(int, self.player.mpd_version.split('.'))):
58 self.log.warning('MPD protocol version: %s < 0.21.0',
59 self.player.mpd_version)
61 'Need at least MPD 0.21 to use Genre plugin (filters required)')
62 self.player.disconnect()
63 raise PluginException('MPD >= 0.21 required')
65 def fetch_genres(self):
66 """Fetches ,at most, nb-depth genre from history,
67 and returns the nbgenres most present"""
68 depth = 10 # nb of genre to fetch from history for analysis
69 nbgenres = 2 # nb of genre to return
70 genres = [g[0] for g in self.sdb.fetch_genres_history(limit=depth)]
72 self.log.debug('No genre found in current track history')
74 genres_analysis = Counter(genres)
75 if genres_analysis.most_common():
76 self.log.debug('Most common genres: %s', genres_analysis.most_common())
77 return dict(genres_analysis.most_common(nbgenres)).keys()
79 def callback_need_track(self):
81 queue_mode = self.plugin_conf.get('queue_mode', 'track')
82 target = self.plugin_conf.getint(f'{queue_mode}_to_add')
83 genres = self.fetch_genres()
85 self.log.warning('No genre tag set in current tracks!')
87 self.log.info('Genre plugin looking for genre: %s', ' / '.join(genres))
88 artists = MetaContainer([])
90 mpd_filter = forge_filter(genre)
91 self.log.debug('mpd filter: %s', mpd_filter)
92 _ = self.player.list('artist', mpd_filter)
94 artists |= MetaContainer([Artist(name=a) for a in _])
96 self.log.info('Genre plugin found nothing to queue')
98 artists = self.get_reorg_artists_list(artists)
99 self.log.debug('Genre plugin found: %s…', ' / '.join(map(str, artists[:4])))
100 for artist in artists:
101 self.log.debug('looking for %s', artist)
102 tracks = self.player.find_tracks(artist)
105 trk = self.filter_track(tracks, candidates)
108 if queue_mode == 'track':
109 self.log.info('Genre plugin chose: %s', trk)
110 candidates.append(trk)
111 if len(candidates) == target:
114 album = self.album_candidate(trk.Artist, unplayed=True)
117 candidates.extend(self.player.find_tracks(album))
118 if len({t.album for t in candidates}) == target:
123 # vim: ai ts=4 sw=4 sts=4 expandtab