]> kaliko git repositories - mpd-goodies.git/blobdiff - bin/mtime
Massive refactoring
[mpd-goodies.git] / bin / mtime
diff --git a/bin/mtime b/bin/mtime
new file mode 100755 (executable)
index 0000000..24f1538
--- /dev/null
+++ b/bin/mtime
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
+#
+#   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 <http://www.gnu.org/licenses/>.
+#
+
+import argparse
+import sys
+
+from datetime import timedelta
+
+import musicpd
+
+VERSION = '0.2'
+
+
+class Mtime(musicpd.MPDClient):
+    script_info = dict({
+        'prog': 'mtime',
+        'description': 'Print current playlist duration.',
+        'epilog': 'Set MPD host/port in env. var'
+    })
+
+    def __init__(self):
+        musicpd.MPDClient.__init__(self)
+        self.args = 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('-r', '--remaining', action='store_true', dest='remaining',
+                            help='print remaining play time (only relevant when paused or playing).',)
+        parser.add_argument('-H', '--human', action='store_true', dest='human',
+                            help='print duration in human readable format.',)
+        args = parser.parse_args()
+        return args
+
+    def _print_time(self, duration):
+        if self.args.human:
+            print(timedelta(seconds=duration))
+        else:
+            print(duration)
+
+    def _run(self):
+        self.connect()
+        status = self.status()
+        plinfo = self.playlistinfo()
+        total_time = 0
+        if self.args.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.disconnect()
+        sys.exit(0)
+
+
+# Script starts here
+if __name__ == '__main__':
+    try:
+        Mtime()
+    except KeyboardInterrupt:
+        sys.stdout.write('exit')
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab