]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/genre.py
264d8abfba25e79c5c17b9e7ee30911ca822fa80
[mpd-sima.git] / sima / plugins / internal / genre.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 Genre tag
22 """
23
24 # standard library import
25 from collections import Counter
26 from random import shuffle
27
28 # third parties components
29
30 # local import
31 from ...lib.plugin import AdvancedPlugin
32 from ...lib.meta import Artist, MetaContainer
33 from ...utils.utils import PluginException
34
35
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 != ''))"
40     return mpd_filter
41
42
43 class Genre(AdvancedPlugin):
44     """Add track based on tags content
45     """
46
47     def __init__(self, daemon):
48         super().__init__(daemon)
49         self._setup_tagsneeded()
50
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'}
55
56     def start(self):
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)
60             self.log.error(
61                 'Need at least MPD 0.21 to use Genre plugin (filters required)')
62             self.player.disconnect()
63             raise PluginException('MPD >= 0.21 required')
64
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)]
71         if not genres:
72             self.log.debug('No genre found in current track history')
73             return []
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()
78
79     def callback_need_track(self):
80         candidates = []
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()
84         if not genres:
85             self.log.warning('No genre tag set in current tracks!')
86             return []
87         self.log.info('Genre plugin looking for genre: %s', ' / '.join(genres))
88         artists = MetaContainer([])
89         for genre in genres:
90             mpd_filter = forge_filter(genre)
91             self.log.debug('mpd filter: %s', mpd_filter)
92             _ = self.player.list('artist', mpd_filter)
93             shuffle(_)
94             artists |= MetaContainer([Artist(name=a) for a in _])
95         if not artists:
96             self.log.info('Genre plugin found nothing to queue')
97             return []
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)
103             if not tracks:
104                 continue
105             trk = self.filter_track(tracks, candidates)
106             if not trk:
107                 continue
108             if queue_mode == 'track':
109                 self.log.info('Genre plugin chose: {}'.format(trk))
110                 candidates.append(trk)
111                 if len(candidates) == target:
112                     break
113             else:
114                 album = self.album_candidate(trk.Artist, unplayed=True)
115                 if not album:
116                     continue
117                 candidates.extend(self.player.find_tracks(album))
118                 if len({t.album for t in candidates}) == target:
119                     break
120         return candidates
121
122 # VIM MODLINE
123 # vim: ai ts=4 sw=4 sts=4 expandtab