]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/random.py
99e0599ff63f178a5fc37faf0b421771acd82dc8
[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         """
51         duration = self.main_conf.getint('sima', 'history_duration')
52         tracks_from_db = self.sdb.get_history(duration=duration)
53         artists = {tr[0] for tr in tracks_from_db}
54         return artists
55
56     def filtered_artist(self, artist):
57         """Filters artists:
58          * not already queued
59
60         If sensible random is set:
61          * not in recent history
62          * not blacklisted
63         """
64         if self.mode == 'sensible':
65             if self.sdb.get_bl_artist(artist, add_not=True):
66                 self.log.debug('Random plugin: Blacklisted "%s"', artist)
67                 return True
68             if artist in self.get_played_artist():
69                 return True
70         if artist in self.player.queue:
71             return True
72         if artist in self.candidates:
73             return True
74         return False
75
76     def callback_need_track(self):
77         self.candidates = []
78         trks = []
79         target = self.plugin_conf.getint('track_to_add')
80         artists = self.player.list('artist', '( artist != "")')
81         random.shuffle(artists)
82         for art in artists:
83             if self.filtered_artist(art):
84                 continue
85             self.log.debug('Random art: %s', art)
86             trks = self.player.find_tracks(Artist(art))
87             if trks:
88                 trk = random.choice(trks)
89                 self.candidates.append(trk)
90                 self.log.info('Random plugin chose (%s): %s', self.mode, trk)
91             if len(self.candidates) >= target:
92                 break
93         return self.candidates
94
95 # VIM MODLINE
96 # vim: ai ts=4 sw=4 sts=4 expandtab