]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/random.py
Get rid of inefficient log string formatting
[mpd-sima.git] / sima / plugins / internal / random.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013-2015, 2020 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     """
37
38     def __init__(self, daemon):
39         super().__init__(daemon)
40         self.daemon = daemon
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                              '"%s" not in ["pure", "sensible"]', self.mode)
45             self.mode = 'pure'
46         self.log.debug('Random flavour: %s', self.mode)
47         self.candidates = []
48
49     def get_played_artist(self,):
50         """Constructs list of already played artists.
51         """
52         duration = self.daemon.config.getint('sima', 'history_duration')
53         tracks_from_db = self.daemon.sdb.get_history(duration=duration)
54         artists = [tr[0] for tr in tracks_from_db]
55         return set(artists)
56
57     def filtered_artist(self, artist):
58         """Filters artists:
59          * not already queued
60
61         If sensible random is set:
62          * not in recent history
63          * not blacklisted
64         """
65         if self.mode == 'sensible':
66             if self.daemon.sdb.get_bl_artist(artist, add_not=True):
67                 self.log.debug('Random: Blacklisted "%s"', artist)
68                 return True
69             if artist in self.get_played_artist():
70                 return True
71         if artist in self.player.queue:
72             return True
73         if artist in self.candidates:
74             return True
75         return False
76
77     def callback_need_track(self):
78         self.candidates = []
79         trks = []
80         target = self.plugin_conf.getint('track_to_add')
81         artists = self.player.list('artist')
82         random.shuffle(artists)
83         for art in artists:
84             if self.filtered_artist(art):
85                 continue
86             self.log.debug('Random art: %s', art)
87             trks = self.player.find_tracks(Artist(art))
88             if trks:
89                 trk = random.choice(trks)
90                 self.candidates.append(trk)
91                 self.log.info('Random candidate (%s): %s', self.mode, trk)
92             if len(self.candidates) >= target:
93                 break
94         return self.candidates
95
96 # VIM MODLINE
97 # vim: ai ts=4 sw=4 sts=4 expandtab