#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) 2009,2010,2012,2019 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 import musicpd VERSION = '0.2' class Crop(musicpd.MPDClient): script_info = dict({ 'prog': basename(__file__), 'description': 'Keep tracks before currently played, removed others. Default is to keep 6 tracks.', 'epilog': 'Set MPD host/port in env. var' }) def __init__(self): """""" musicpd.MPDClient.__init__(self) self.nb_tracks = 6 self._get_args() 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='?', help='how many titles to crop') args = parser.parse_args() if args.n: self.nb_tracks = args.n def _run(self): """""" self.connect() current_pos = int(self.currentsong().get('pos', 0)) if current_pos <= self.nb_tracks: self.disconnect() sys.exit(0) print('Keeping %i tracks' % self.nb_tracks) while current_pos > self.nb_tracks: self.delete(0) current_pos = int(self.currentsong().get('pos', 0)) self.disconnect() sys.exit(0) # Script starts here if __name__ == '__main__': try: Crop() except KeyboardInterrupt: sys.stdout.write('exit') # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab