Skip to content

Instantly share code, notes, and snippets.

@simonbru
Last active October 4, 2017 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbru/0ed6332b72d1f7f6ce95470d34bbd316 to your computer and use it in GitHub Desktop.
Save simonbru/0ed6332b72d1f7f6ce95470d34bbd316 to your computer and use it in GitHub Desktop.
mpd-web-remote
#!/usr/bin/env python3
import re
from subprocess import check_call, check_output
from bottle import get, post, run, redirect, request
def retrieve_volume():
output = check_output(['mpc', 'volume'])
match = re.search(r'([0-9]+)%', output.decode())
volume = match.group(1)
return volume
@get('/')
def root():
volume = retrieve_volume()
return """
<html>
<form method="post" action="/play">
<input type="submit" value="Play">
</form>
<form method="post" action="/stop">
<input type="submit" value="Stop">
</form>
<form method="post" action="/volume">
<input type="range" name="volume" min="0" max="100" value="{volume}">
<input type="submit" value="Set volume">
</form>
</html>
""".format(volume=volume)
@post('/play')
def play():
check_call(['mpc', 'play'])
redirect('/')
@post('/stop')
def stop():
check_call(['mpc', 'stop'])
redirect('/')
@post('/volume')
def set_volume():
volume = request.POST.get('volume', '0')
if not volume.isdigit():
return "WTF"
check_call(['mpc', 'volume', volume])
redirect('/')
run(host='::', port=8001, debug=True, reloader=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment