]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/random.py
Update simadb API
[mpd-sima.git] / sima / plugins / internal / random.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013-2015, 2020-2021 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.mode = self.plugin_conf.get('flavour', None)
41         if self.mode not in ['pure', 'sensible']:
42             self.log.warning('Bad value for flavour, '
43                              '"%s" not in ["pure", "sensible"]', self.mode)
44             self.mode = 'pure'
45         self.log.debug('Random flavour: %s', self.mode)
46         self.candidates = []
47
48     def get_played_artist(self,):
49         """Constructs list of already played artists."""
50         duration = self.main_conf.getint('sima', 'history_duration')
51         artists = self.sdb.fetch_artists_history(duration=duration)
52         return artists
53
54     def filtered_artist(self, artist):
55         """Filters artists:
56          * not already queued
57
58         If sensible random is set:
59          * not in recent history
60          * not in blocklist
61         """
62         if self.mode == 'sensible':
63             if self.sdb.get_bl_artist(Artist(artist), add=False):
64                 self.log.debug('Random plugin: Blacklisted "%s"', artist)
65                 return True
66             if artist in self.get_played_artist():
67                 return True
68         if artist in self.player.queue:
69             return True
70         if artist in self.candidates:
71             return True
72         return False
73
74     def callback_need_track(self):
75         self.candidates = []
76         trks = []
77         target = self.plugin_conf.getint('track_to_add')
78         artists = self.player.list('artist', '( artist != "")')
79         random.shuffle(artists)
80         for art in artists:  # artists is a list of strings here
81             if self.filtered_artist(art):
82                 continue
83             self.log.debug('Random art: %s', art)
84             trks = self.player.find_tracks(Artist(art))
85             if trks:
86                 trk = random.choice(trks)
87                 self.candidates.append(trk)
88                 self.log.info('Random plugin chose (%s): %s', self.mode, trk)
89             if len(self.candidates) >= target:
90                 break
91         return self.candidates
92
93 # VIM MODLINE
94 # vim: ai ts=4 sw=4 sts=4 expandtab