]> kaliko git repositories - mpd-goodies.git/blob - bin/mtime
Fixed mtime prog name
[mpd-goodies.git] / bin / mtime
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20 import argparse
21 import sys
22
23 from datetime import timedelta
24 from os.path import basename
25
26 import musicpd
27
28 VERSION = '0.2'
29
30
31 class Mtime(musicpd.MPDClient):
32     script_info = dict({
33         'prog': basename(__file__),
34         'description': 'Print current playlist duration.',
35         'epilog': 'Set MPD host/port in env. var'
36     })
37
38     def __init__(self):
39         musicpd.MPDClient.__init__(self)
40         self.args = self._get_args()
41         self._run()
42
43     def _get_args(self):
44         parser = argparse.ArgumentParser(**self.__class__.script_info)
45         parser.add_argument('--version', action='version',
46                             version='v%s' % VERSION)
47         parser.add_argument('-r', '--remaining', action='store_true', dest='remaining',
48                             help='print remaining play time (only relevant when paused or playing).',)
49         parser.add_argument('-H', '--human', action='store_true', dest='human',
50                             help='print duration in human readable format.',)
51         args = parser.parse_args()
52         return args
53
54     def _print_time(self, duration):
55         if self.args.human:
56             print(timedelta(seconds=duration))
57         else:
58             print(duration)
59
60     def _run(self):
61         self.connect()
62         status = self.status()
63         plinfo = self.playlistinfo()
64         total_time = 0
65         if self.args.remaining:
66             if status.get('state') in ['play', 'pause']:
67                 total_time = sum(int(trk.get('time')) for trk in plinfo if int(
68                     trk.get('pos')) > int(status.get('song')))
69                 # add remaining time from current song
70                 curr_elapsed = status.get('time').split(':')
71                 total_time += int(curr_elapsed[1]) - int(curr_elapsed[0])
72         else:
73             total_time = sum(int(trk.get('time')) for trk in plinfo)
74         self._print_time(total_time)
75         self.disconnect()
76         sys.exit(0)
77
78
79 # Script starts here
80 if __name__ == '__main__':
81     try:
82         Mtime()
83     except KeyboardInterrupt:
84         sys.stdout.write('exit')
85
86 # VIM MODLINE
87 # vim: ai ts=4 sw=4 sts=4 expandtab