From: kaliko Date: Sat, 8 Apr 2023 13:14:34 +0000 (+0200) Subject: Add mrandom X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=87c3a401428cb9e2ee42214573fbe69a30c58f3c;p=mpd-goodies.git Add mrandom --- diff --git a/bin/mrandom b/bin/mrandom new file mode 100755 index 0000000..18ea9c7 --- /dev/null +++ b/bin/mrandom @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Copyright (c) 2009,2010,2012,2019,2023 kaliko +# +# 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 . +# + +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