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