#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2009, 2010, 2012 Kaliko Jack # # 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 sys from datetime import timedelta from lib.goodies import Goodie NAME = 'mtime' VERSION = '0.1' NALBUM_OPTS = list([ { 'sw': ['-r', '--remaining'], 'action': 'store_true', 'dest': 'remaining', 'metavar': 'r', 'help': 'print remaining play time (only relevant when paused or playing).'}, { 'sw': ['-H', '--human'], 'action': 'store_true', 'dest': 'human', 'metavar': 'h', 'help': 'print duration in human readable format.'}, ]) class Nalbum(Goodie): """ """ script_info = dict({ 'version': VERSION, 'prog_name': NAME, 'description': 'Print current playlist duration.', }) def __init__(self): """""" Goodie.__init__(self, self.__class__.script_info, extra_options=NALBUM_OPTS) self._run() def _print_time(self, duration): if self.cli_options.human: print(timedelta(seconds=duration)) else: print(duration) def _run(self): """""" self.mpdConnect() status = self.client.status() plinfo = self.client.playlistinfo() total_time = 0 if self.cli_options.remaining: if status.get('state') in ['play', 'pause']: total_time = sum(int(trk.get('time')) for trk in plinfo if int(trk.get('pos')) > int(status.get('song'))) # add remaining time from current song curr_elapsed = status.get('time').split(':') total_time += int(curr_elapsed[1]) - int(curr_elapsed[0]) else: total_time = sum(int(trk.get('time')) for trk in plinfo) self._print_time(total_time) self.client.disconnect() sys.exit(0) # Script starts here if __name__ == '__main__': try: Nalbum() except KeyboardInterrupt: sys.stdout.write('exit') # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab