1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013-2015, 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/>.
24 # standard library import
27 # third parties components
30 from ...lib.plugin import Plugin
31 from ...lib.meta import Artist
38 def __init__(self, daemon):
39 super().__init__(daemon)
40 self.mode = self.plugin_conf.get('flavour', None)
41 if self.mode not in ['pure', 'sensible']:
42 self.log.warning('Bad value for flavour, '
43 '"%s" not in ["pure", "sensible"]', self.mode)
45 self.log.debug('Random flavour: %s', self.mode)
48 def get_played_artist(self,):
49 """Constructs list of already played artists."""
50 duration = self.main_conf.getint('sima', 'history_duration')
51 artists = self.sdb.fetch_artists_history(duration=duration)
54 def filtered_artist(self, artist):
58 If sensible random is set:
59 * not in recent history
62 if self.mode == 'sensible':
63 if self.sdb.get_bl_artist(Artist(artist), add=False):
64 self.log.debug('Random plugin: Blacklisted "%s"', artist)
66 if artist in self.get_played_artist():
68 if artist in self.player.queue:
70 if artist in self.candidates:
74 def callback_need_track(self):
77 target = self.plugin_conf.getint('track_to_add')
78 artists = self.player.list('artist', '( artist != "")')
79 random.shuffle(artists)
80 for art in artists: # artists is a list of strings here
81 if self.filtered_artist(art):
83 self.log.debug('Random art: %s', art)
84 trks = self.player.find_tracks(Artist(art))
86 trk = random.choice(trks)
87 self.candidates.append(trk)
88 self.log.info('Random plugin chose (%s): %s', self.mode, trk)
89 if len(self.candidates) >= target:
91 return self.candidates
94 # vim: ai ts=4 sw=4 sts=4 expandtab