1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013-2015, 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/>.
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.
51 duration = self.main_conf.getint('sima', 'history_duration')
52 tracks_from_db = self.sdb.get_history(duration=duration)
53 artists = [tr[0] for tr in tracks_from_db]
56 def filtered_artist(self, artist):
60 If sensible random is set:
61 * not in recent history
64 if self.mode == 'sensible':
65 if self.sdb.get_bl_artist(artist, add_not=True):
66 self.log.debug('Random: Blacklisted "%s"', artist)
68 if artist in self.get_played_artist():
70 if artist in self.player.queue:
72 if artist in self.candidates:
76 def callback_need_track(self):
79 target = self.plugin_conf.getint('track_to_add')
80 artists = self.player.list('artist', '( artist != "")')
81 random.shuffle(artists)
83 if self.filtered_artist(art):
85 self.log.debug('Random art: %s', art)
86 trks = self.player.find_tracks(Artist(art))
88 trk = random.choice(trks)
89 self.candidates.append(trk)
90 self.log.info('Random plugin chose (%s): %s', self.mode, trk)
91 if len(self.candidates) >= target:
93 return self.candidates
96 # vim: ai ts=4 sw=4 sts=4 expandtab