From: kaliko Date: Sun, 8 Aug 2010 12:12:49 +0000 (+0000) Subject: * Add wakeup script. X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=6729b0a3c9607c8ee6a8dd352ec601ad22cacaf2;p=mpd-goodies.git * Add wakeup script. --- diff --git a/wakeup b/wakeup new file mode 100755 index 0000000..7009081 --- /dev/null +++ b/wakeup @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright (c) 2009, 2010 Efrim {{{ +# +# 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 . +# +# }}} + + +import sys + +from random import choice + +from lib.mpdclass import MPDClass +from lib.startop import StartOpt + +NAME = 'wakeup' +VERSION = '0.1' +USAGE = 'USAGE: %prog --help | AlbumA AlbumB "The Album C" | -f file' + +WAKEUP_OPTS = list([ + { + 'sw': ['-f', '--file'], + 'type': 'str', + 'dest': 'file', + 'metavar': 'filename', + 'help': 'filemame to read album list from. An album name on each line'}, + ]) + + +class WakeUp(StartOpt, MPDClass): + """ + """ + script_info = dict({ + 'version': VERSION, + 'prog_name': NAME, + 'description': 'Queue and play an album chosen at random from a list. Albums list provided from CLI or text file.', + 'usage': USAGE, + }) + + def __init__(self): + """""" + StartOpt.__init__(self, self.__class__.script_info, WAKEUP_OPTS) + MPDClass.__init__(self) + self._get_arg() + self._run() + + def _get_arg(self): + """""" + if self.cli_options.file: + # TODO: handle encoding reading file! + print 'reading from file, assuming %s encoding' % self.localencoding + try: + with open(self.cli_options.file) as f: + self.albums_list = [line.rstrip('\n') for line in f.readlines()] + return True + except IOError, err: + print 'ERROR: "%s": %s' % (self.cli_options.file, err[1]) + sys.exit(1) + if self.cli_args: + print 'WARNING: ignoring "%s"' % self.cli_args + return True + if self.cli_args: + self.albums_list = self.cli_args + return True + self.parser.print_usage() + print "ERROR: Need at least an argument!" + sys.exit(1) + + def _run(self): + """""" + print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port) + self.mpdConnect() + album = choice(self.albums_list) + print 'Queueing "%s"' % album + tracks = self.client.find('album', self._u8_convert(album)) + if not tracks: + print 'No tracks found for "%s"' % album + self.client.disconnect() + sys.exit(1) + for track in tracks: + self.client.add(track.get('file')) + self.client.disconnect() + sys.exit(0) + + +# Script starts here +if __name__ == '__main__': + try: + WakeUp() + except KeyboardInterrupt: + sys.stdout.write('exit') + +# VIM MODLINE +# vim: ai ts=4 sw=4 sts=4 expandtab