]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/randomfallback.py
Add album mode configuration example.
[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     TODO: refactor, this plugin does not look good to me.
36           callback_need_track_fb/get_trk articulation is not elegant at all
37     """
38
39     def __init__(self, daemon):
40         super().__init__(daemon)
41         self.daemon = daemon
42         if not self.plugin_conf:
43             return
44         self.mode = self.plugin_conf.get('flavour', None)
45         if self.mode not in ['pure', 'sensible']:
46             self.log.warning('Bad value for flavour, '
47                     '"{}" not in ["pure", "sensible"]'.format(self.mode))
48             self.mode = 'pure'
49
50     def get_played_artist(self,):
51         """Constructs list of already played artists.
52         """
53         duration = self.daemon.config.getint('sima', 'history_duration')
54         tracks_from_db = self.daemon.sdb.get_history(duration=duration)
55         # Construct Track() objects list from database history
56         artists = [tr[-1] for tr in tracks_from_db]
57         return set(artists)
58
59     def callback_need_track_fb(self):
60         trks = list()
61         target = self.plugin_conf.getint('track_to_add')
62         limit = 0
63         while len(trks) < target:
64             trk = self.get_trk()
65             if trk:
66                 trks.append(trk)
67             else:
68                 limit += 1
69                 if limit > 3:
70                     return trks
71         return trks
72
73     def get_trk(self):
74         """Get a single track according to random flavour
75         """
76         trk = None
77         art = None
78         artists = list(self.player.artists)
79         if self.mode == 'sensible':
80             played_art = self.get_played_artist()
81             while artists:
82                 art = random.choice(artists)
83                 if art not in played_art:
84                     break
85                 artists.pop(art)
86         elif self.mode == 'pure':
87             art = random.choice(artists)
88         if art is None:
89             return None
90         self.log.debug('Random art: {}'.format(art))
91         trks = self.player.find_track(art)
92         if trks:
93             trk = random.choice(trks)
94             self.log.info('random fallback ({}): {}'.format(self.mode, trk))
95         return trk
96
97
98
99 # VIM MODLINE
100 # vim: ai ts=4 sw=4 sts=4 expandtab