2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009,2010,2012,2019,2023 kaliko <kaliko@azylum.org>
6 # This program 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.
11 # This program 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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 from os.path import basename
24 from random import randrange
31 class MRand(musicpd.MPDClient):
33 'prog': basename(__file__),
34 'description': 'Add some, randomly selected, files to the queue.',
35 'epilog': 'Set MPD host/port in env. var'
40 musicpd.MPDClient.__init__(self)
41 self.args = self._get_args()
47 parser = argparse.ArgumentParser(**self.__class__.script_info)
48 parser.add_argument('--version', action='version',
49 version='v%s' % VERSION)
50 parser.add_argument('n', type=int, nargs='?', default=5,
51 help='how many items to add (defaults to 5)')
52 args = parser.parse_args()
55 def get_random_id(self, n: int)->int:
57 cand = randrange(1, n) # songs_cnt excluded
58 if cand in self.added:
59 return self.get_random_id(n)
60 self.added.append(cand)
66 songs_cnt = int(self.stats().get('songs'))
67 if songs_cnt <= self.args.n:
68 print('Not enough songs in libary to run', file=sys.stderr)
72 while self.args.n > 0:
73 cand = self.get_random_id(songs_cnt)
74 song = self.find('(file != "")', 'window', f'{cand}:{cand+1}')[0]
75 print(f'Add {song.get("file")}')
80 if __name__ == '__main__':
83 except KeyboardInterrupt:
84 sys.stdout.write('exit')
87 # vim: ai ts=4 sw=4 sts=4 expandtab