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)
            self.wfile.write("unknown request (try [playpause|stop|next|previous]")


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