]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/randomfallback.py
More robust randomfallback
[mpd-sima.git] / sima / plugins / internal / randomfallback.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013, 2014 Jack 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 Fetching similar artists from last.fm web services
22 """
23
24 # standard library import
25 import random
26
27 # third parties components
28
29 # local import
30 from ...lib.plugin import Plugin
31
32
33 class RandomFallBack(Plugin):
34     """Add random track as fallback"""
35
36     def __init__(self, daemon):
37         super().__init__(daemon)
38         self.daemon = daemon
39         if not self.plugin_conf:
40             return
41         self.mode = self.plugin_conf.get('flavour', None)
42         if self.mode not in ['pure', 'sensible']:
43             self.log.warning('Bad value for flavour, '
44                     '"{}" not in ["pure", "sensible"]'.format(self.mode))
45             self.mode = 'pure'
46
47     def get_played_artist(self,):
48         """Constructs list of already played artists.
49         """
50         duration = self.daemon.config.getint('sima', 'history_duration')
51         tracks_from_db = self.daemon.sdb.get_history(duration=duration)
52         # Construct Track() objects list from database history
53         artists = [tr[-1] for tr in tracks_from_db]
54         return set(artists)
55
56     def callback_need_track_fb(self):
57         trks = list()
58         target = self.plugin_conf.getint('track_to_add')
59         while len(trks) < target:
60             trk = self.get_trk()
61             if trk:
62                 trks.append(trk)
63         return trks
64
65     def get_trk(self):
66         """Get a single track acording to random flavour
67         """
68         trk = None
69         artists = list(self.player.artists)
70         if self.mode == 'sensitive':
71             played_art = self.get_played_artist()
72             while artists:
73                 art = random.choice(artists)
74                 if art not in played_art:
75                     break
76                 artists.pop(art)
77         elif self.mode == 'pure':
78             art = random.choice(artists)
79         self.log.debug('Random art: {}'.format(art))
80         trks = self.player.find_track(art)
81         if trks:
82             trk = random.choice(trks)
83             self.log.info('random fallback ({}): {}'.format(self.mode, trk))
84         return trk
85
86
87
88 # VIM MODLINE
89 # vim: ai ts=4 sw=4 sts=4 expandtab