2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
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.
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.
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/>.
23 from datetime import timedelta
30 class Mtime(musicpd.MPDClient):
33 'description': 'Print current playlist duration.',
34 'epilog': 'Set MPD host/port in env. var'
38 musicpd.MPDClient.__init__(self)
39 self.args = self._get_args()
43 parser = argparse.ArgumentParser(**self.__class__.script_info)
44 parser.add_argument('--version', action='version',
45 version='v%s' % VERSION)
46 parser.add_argument('-r', '--remaining', action='store_true', dest='remaining',
47 help='print remaining play time (only relevant when paused or playing).',)
48 parser.add_argument('-H', '--human', action='store_true', dest='human',
49 help='print duration in human readable format.',)
50 args = parser.parse_args()
53 def _print_time(self, duration):
55 print(timedelta(seconds=duration))
61 status = self.status()
62 plinfo = self.playlistinfo()
64 if self.args.remaining:
65 if status.get('state') in ['play', 'pause']:
66 total_time = sum(int(trk.get('time')) for trk in plinfo if int(
67 trk.get('pos')) > int(status.get('song')))
68 # add remaining time from current song
69 curr_elapsed = status.get('time').split(':')
70 total_time += int(curr_elapsed[1]) - int(curr_elapsed[0])
72 total_time = sum(int(trk.get('time')) for trk in plinfo)
73 self._print_time(total_time)
79 if __name__ == '__main__':
82 except KeyboardInterrupt:
83 sys.stdout.write('exit')
86 # vim: ai ts=4 sw=4 sts=4 expandtab