Skip to content

Commit

Permalink
new mpc button handler to add more music
Browse files Browse the repository at this point in the history
if the button is pressed while stopped, we look at the song titles in the playlist,
and search for albums with similar names. Very naive substring searches.

enqueues the first album we find containing one word from the last song played
  • Loading branch information
ryepup committed Sep 30, 2014
1 parent f149055 commit 1aab867
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions big-button/mpc-button.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import argparse
import subprocess
import os.path

log = logging.getLogger(__name__)

Expand All @@ -14,21 +15,55 @@ def status():
'playing': '[playing]' in res
}

def play():
subprocess.check_call(['mpc', '--host', mpd_host, 'play'])
def play(i=1):
subprocess.check_call(['mpc', '--host', mpd_host, 'play', str(i)])

def pause():
subprocess.check_call(['mpc', '--host', mpd_host, 'pause'])

def toggle():
subprocess.check_call(['mpc', '--host', mpd_host, 'toggle'])

def playlist():
return subprocess.check_output(['mpc', '--host', mpd_host, '-f',
'%title%', 'playlist'])

def album_search(q):
res = subprocess.check_output(['mpc', '--host', mpd_host, 'search',
'album', q])
return res.split('\n')[0]

def add_and_play(folder, i):
ps = subprocess.Popen(['mpc', '--host', mpd_host, 'ls', folder],
stdout=subprocess.PIPE)
subprocess.check_call(['mpc', '--host', mpd_host, 'add'], stdin=ps.stdout)
ps.wait()
play(i)

def enqueue_related():
words = []
current_playlist = playlist().split('\n')
for title in current_playlist:
for w in title.split():
words.append(w.lower())

words.reverse()
for q in words:
album = album_search(q)
if album:
folder= os.path.dirname(album)
add_and_play(folder, len(current_playlist))
return

def wait_for_button(f):
log.info('wait_for_button')
press_ms = int(f.readline().strip())
log.info('button pressed for %s ms', press_ms)
s = status()
if s['paused']:
play()
elif s['playing']:
pause()
if s['paused'] or s['playing']:
toggle()
else:
enqueue_related()


parser = argparse.ArgumentParser(description='mpd button controller')
Expand Down

0 comments on commit 1aab867

Please sign in to comment.