0x79 - a blog

Work in progress.

Remote-Controll Spotify using REST and DBUS

In a previous article we used DBUS to send control commands to the spotify client.

To remotely control spotify via the web, i wrote a little HTTP server which takes GET requests and "forwards" them via DBUS to the player. The program is written in python (see below) and takes requests on the following paths:

  • /playpause
  • /stop
  • /next
  • /previous

Next step is an android app to call our little webservice :)

import SimpleHTTPServer
import SocketServer
import argparse
import subprocess

parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, default=9000)
args = parser.parse_args()


class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    cmd = "/usr/bin/dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 "

    def do_GET(self):
        if self.path.endswith("playpause"):
            subprocess.call(self.cmd + "org.mpris.MediaPlayer2.Player.PlayPause", shell=True)
        elif self.path.endswith("stop"):
            subprocess.call(self.cmd + "org.mpris.MediaPlayer2.Player.Stop", shell=True)
        elif self.path.endswith("next"):
            subprocess.call(self.cmd + "org.mpris.MediaPlayer2.Player.Next", shell=True)
        elif self.path.endswith("previous"):
            subprocess.call(self.cmd + "org.mpris.MediaPlayer2.Player.Previous", shell=True)
        else:
            self.wfile.write("unknown request (try [playpause|stop|next|previous]")
            self.send_respose(404)
            return

        self.wfile.write("OK")
        self.send_response(200)


httpd = SocketServer.TCPServer(("", args.port), ServerHandler)
print "listening on " + str(args.port)
httpd.serve_forever()