1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013, 2014 Jack 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 Fetching similar artists from last.fm web services
24 # standard library import
27 # third parties components
30 from ...lib.plugin import Plugin
31 from ...lib.track import Track
34 class RandomFallBack(Plugin):
35 """Add random track as fallback"""
37 def __init__(self, daemon):
38 super().__init__(daemon)
40 if not self.plugin_conf:
42 self.mode = self.plugin_conf.get('flavour', None)
43 if self.mode not in ['pure', 'sensible']:
44 self.log.warning('Bad value for flavour, '
45 '"{}" not in ["pure", "sensible"]'.format(self.mode))
48 def get_played_artist(self,):
49 """Constructs list of already played artists.
51 duration = self.daemon.config.getint('sima', 'history_duration')
52 tracks_from_db = self.daemon.sdb.get_history(duration=duration)
53 # Construct Track() objects list from database history
54 artists = [ tr[-1] for tr in tracks_from_db ]
57 def callback_need_track_fb(self):
59 target = self.plugin_conf.getint('track_to_add')
60 while len(trks) < target:
61 trks.append(self.get_trk())
65 artists = list(self.player.artists)
66 if self.mode == 'sensitive':
67 played_art = self.get_played_artist()
69 art = random.choice(artists)
70 if art not in played_art:
72 elif self.mode == 'pure':
73 art = random.choice(artists)
74 self.log.debug('Random art: {}'.format(art))
75 trk = random.choice(self.player.find_track(art))
76 self.log.info('random fallback ({}): {}'.format(self.mode, trk))
82 # vim: ai ts=4 sw=4 sts=4 expandtab