Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrote the Space MPD code. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 44 additions & 28 deletions spacempd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()