]> kaliko git repositories - mpd-goodies.git/blob - src/mtime
* Add mtime command
[mpd-goodies.git] / src / mtime
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009, 2010, 2012 Kaliko Jack <kaliko.jack@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
21 import sys
22
23
24 from datetime import timedelta
25
26
27 from lib.goodies import Goodie
28
29
30 NAME = 'mtime'
31 VERSION = '0.1'
32
33 NALBUM_OPTS = list([
34     {
35         'sw': ['-r', '--remaining'],
36         'action': 'store_true',
37         'dest': 'remaining',
38         'metavar': 'r',
39         'help': 'print remaining play time (only relevant when paused or playing).'},
40     {
41         'sw': ['-H', '--human'],
42         'action': 'store_true',
43         'dest': 'human',
44         'metavar': 'h',
45         'help': 'print duration in human readable format.'},
46     ])
47
48
49 class Nalbum(Goodie):
50     """
51     """
52     script_info = dict({
53         'version': VERSION,
54         'prog_name': NAME,
55         'description': 'Print current playlist duration.',
56         })
57
58     def __init__(self):
59         """"""
60         Goodie.__init__(self, self.__class__.script_info,
61                 extra_options=NALBUM_OPTS)
62         self._run()
63
64     def _print_time(self, duration):
65         if self.cli_options.human:
66             print(timedelta(seconds=duration))
67         else:
68             print(duration)
69
70     def _run(self):
71         """"""
72         self.mpdConnect()
73         status = self.client.status()
74         plinfo = self.client.playlistinfo()
75         total_time = 0
76         if self.cli_options.remaining:
77             if status.get('state') in ['play', 'pause']:
78                 total_time = sum(int(trk.get('time')) for trk in plinfo if int(trk.get('pos')) > int(status.get('song')))
79                 # add remaining time from current song
80                 curr_elapsed = status.get('time').split(':')
81                 total_time += int(curr_elapsed[1]) - int(curr_elapsed[0])
82         else:
83             total_time = sum(int(trk.get('time')) for trk in plinfo)
84         self._print_time(total_time)
85         self.client.disconnect()
86         sys.exit(0)
87
88
89 # Script starts here
90 if __name__ == '__main__':
91     try:
92         Nalbum()
93     except KeyboardInterrupt:
94         sys.stdout.write('exit')
95
96 # VIM MODLINE
97 # vim: ai ts=4 sw=4 sts=4 expandtab