diff --git a/spacempd.py b/spacempd.py index 19861f5..e22182f 100644 --- a/spacempd.py +++ b/spacempd.py @@ -9,6 +9,23 @@ PORT = '6600' PASSWORD = 'password goes here' +mpd_commands = { + i: show_state for i in ['playing', 'state'], + i: lambda client, action: do_action(client, action, j) for i, j in zip( + ['play', 'pause', 'stop', 'next'], [True, False, False, True]), +} + +# Just use replacement identifiers, as they are close to lazy evaluation. +mpd_messages = { + 'is_play': 'Now playing: {artist} - {title}', + 'is_pause': 'Music is currently paused ({artist})', + 'is_stop': 'No music is playing', + 'play': 'Pressing play on mpd...', + 'pause': 'Pausing mpd...', + 'stop': 'Stopping mpd...', + 'next': 'Moving to next song on mpd...', +} + @willie.module.commands('mpd') def mpd(bot, trigger): rulenum = trigger.group(2) @@ -31,39 +48,38 @@ def mpd(bot, trigger): client.disconnect() sys.exit(2) - mpdcommand = str(rulenum) + # Dispatch the MPD command to a handler, if there is one. + mpd_command = str(rulenum) - if ((mpdcommand == 'playing') or (mpdcommand == 'state')) : - currentsong = client.currentsong() - currentstatus = client.status() + if mpd_command in mpd_commands: + mpd_commands[mpd_command](client, mpd_command) + else: + bot.say('Invalid mpd command.') - if currentstatus['state'] == 'play': - bot.say('Now playing: ' + currentsong['artist'] + ' - ' + currentsong['title']) - elif currentstatus['state'] == 'pause': - bot.say('Music is currently paused (' + currentsong['artist'] + ')') - elif currentstatus['state'] == 'stop': - bot.say('No music is playing') +# Shows the current state of MPD. +def show_state(client, action): + current_song = client.currentsong() + current_state = client.status()['state'] - elif mpdcommand == 'play': - bot.say('Pressing play on mpd...') - client.play() - currentsong = client.currentsong() - bot.say('Now playing: ' + currentsong['artist'] + ' - ' + currentsong['title']) + if 'is_' + current_state in mpd_messages: + bot.say(mpd_messages['is_' + current_state].format( + artist=current_song['artist'], title=current_song['title'])) - elif mpdcommand == 'pause': - bot.say('Pausing mpd...') - client.pause() +# Shows the current song. +def show_current_song(client): + current_song = client.currentsong() + bot.say(mpd_messages['is_playing'].format(artist=current_song['artist'], + title=current_song['title'])) - elif mpdcommand == 'stop': - bot.say('Stopping mpd...') - client.stop() +# Performs a client action, shows the associated message and, if desired, shows +# the current song. +def do_action(client, action, show_song=False): + if action in mpd_messages: + bot.say(mpd_messages[action]) - elif mpdcommand == 'next': - bot.say('Moving to next song on mpd...') - client.next() - currentsong = client.currentsong() - bot.say('Now playing: ' + currentsong['artist'] + ' - ' + currentsong['title']) + if getattr(client, action): + getattr(client, action)() - else: - bot.say('invalid mpd command') + if show_song: + show_current_song()