]> kaliko git repositories - mpd-goodies.git/commitdiff
Add mrandom
authorkaliko <kaliko@azylum.org>
Sat, 8 Apr 2023 13:14:34 +0000 (15:14 +0200)
committerkaliko <kaliko@azylum.org>
Sat, 8 Apr 2023 13:14:34 +0000 (15:14 +0200)
bin/mrandom [new file with mode: 0755]

diff --git a/bin/mrandom b/bin/mrandom
new file mode 100755 (executable)
index 0000000..18ea9c7
--- /dev/null
@@ -0,0 +1,87 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2009,2010,2012,2019,2023 kaliko <kaliko@azylum.org>
+#
+#   This program is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import argparse
+import sys
+
+from os.path import basename
+from random import randrange
+
+import musicpd
+
+VERSION = '0.1'
+
+
+class MRand(musicpd.MPDClient):
+    script_info = dict({
+        'prog': basename(__file__),
+        'description': 'Add some, randomly selected, files to the queue.',
+        'epilog': 'Set MPD host/port in env. var'
+    })
+
+    def __init__(self):
+        """"""
+        musicpd.MPDClient.__init__(self)
+        self.args = self._get_args()
+        self.added = []
+        self._run()
+
+    def _get_args(self):
+        """"""
+        parser = argparse.ArgumentParser(**self.__class__.script_info)
+        parser.add_argument('--version', action='version',
+                            version='v%s' % VERSION)
+        parser.add_argument('n', type=int, nargs='?', default=5,
+                            help='how many items to add (defaults to 5)')
+        args = parser.parse_args()
+        return args
+
+    def get_random_id(self, n: int)->int:
+        """"""
+        cand = randrange(1, n)  # songs_cnt excluded
+        if cand in self.added:
+            return self.get_random_id(n)
+        self.added.append(cand)
+        return cand
+
+    def _run(self):
+        """"""
+        self.connect()
+        songs_cnt = int(self.stats().get('songs'))
+        if songs_cnt <= self.args.n:
+            print('Not enough songs in libary to run', file=sys.stderr)
+            self.disconnect()
+            return
+        # add songs
+        while self.args.n > 0:
+            cand = self.get_random_id(songs_cnt)
+            song = self.find('(file != "")', 'window', f'{cand}:{cand+1}')[0]
+            print(f'Add {song.get("file")}')
+            self.args.n-=1
+        self.disconnect()
+
+# Script starts here
+if __name__ == '__main__':
+    try:
+        MRand()
+    except KeyboardInterrupt:
+        sys.stdout.write('exit')
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab