]> kaliko git repositories - mpd-goodies.git/blobdiff - bin/wakeup
Massive refactoring
[mpd-goodies.git] / bin / wakeup
diff --git a/bin/wakeup b/bin/wakeup
new file mode 100755 (executable)
index 0000000..f938473
--- /dev/null
@@ -0,0 +1,85 @@
+#!/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 random import choice
+from os.path import basename
+
+import musicpd
+
+
+VERSION = '0.2'
+
+
+class WakeUp(musicpd.MPDClient):
+    script_info = dict({
+        'prog': basename(__file__),
+        'description': 'Queues and plays an album chosen at random from a list. Albums list provided from CLI or text file.',
+        'epilog': 'Set MPD host/port in env. var'
+    })
+
+    def __init__(self):
+        musicpd.MPDClient.__init__(self)
+        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('-f', type=argparse.FileType('r'), dest='albumf',
+                            help='text file, one album per line', nargs='?')
+        parser.add_argument('album', type=str,
+                            nargs='*', help='album name')
+        args = parser.parse_args()
+        if args.albumf:
+            args.album.extend([line.rstrip(' \n')
+                               for line in args.albumf.readlines()])
+        if not args.album:
+            # parser.print_help()
+            print(
+                'No album names found on CLI, run "{} -h" for usage'.format(sys.argv[0]))
+            sys.exit(1)
+        return args.album
+
+    def _run(self):
+        albums_list = self._get_args()
+        album = choice(albums_list)
+        print('Queueing "%s"' % album)
+        self.connect()
+        tracks = self.search('album', album)
+        if not tracks:
+            print('No tracks found for "%s"' % album)
+            self.disconnect()
+            sys.exit(1)
+        self.searchadd('album', album)
+        self.disconnect()
+        sys.exit(0)
+
+
+# Script starts here
+if __name__ == '__main__':
+    try:
+        WakeUp()
+    except KeyboardInterrupt:
+        print('exit')
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab