]> kaliko git repositories - mpd-goodies.git/blob - bin/mrandom
18ea9c700d9312379247ed66650187fe4191f2fa
[mpd-goodies.git] / bin / mrandom
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009,2010,2012,2019,2023 kaliko <kaliko@azylum.org>
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20 import argparse
21 import sys
22
23 from os.path import basename
24 from random import randrange
25
26 import musicpd
27
28 VERSION = '0.1'
29
30
31 class MRand(musicpd.MPDClient):
32     script_info = dict({
33         'prog': basename(__file__),
34         'description': 'Add some, randomly selected, files to the queue.',
35         'epilog': 'Set MPD host/port in env. var'
36     })
37
38     def __init__(self):
39         """"""
40         musicpd.MPDClient.__init__(self)
41         self.args = self._get_args()
42         self.added = []
43         self._run()
44
45     def _get_args(self):
46         """"""
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()
53         return args
54
55     def get_random_id(self, n: int)->int:
56         """"""
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)
61         return cand
62
63     def _run(self):
64         """"""
65         self.connect()
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)
69             self.disconnect()
70             return
71         # add songs
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")}')
76             self.args.n-=1
77         self.disconnect()
78
79 # Script starts here
80 if __name__ == '__main__':
81     try:
82         MRand()
83     except KeyboardInterrupt:
84         sys.stdout.write('exit')
85
86 # VIM MODLINE
87 # vim: ai ts=4 sw=4 sts=4 expandtab