Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: dedfa0fb6e
Fetching contributors…

Cannot retrieve contributors at this time

file 64 lines (54 sloc) 1.861 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
import tornado.ioloop
import tornado.web
import subprocess
import socket
import urllib2

from spotify import Spotify

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        s = Spotify()
        verbs = ['skip']
        if s.isPlaying():
            verbs.append('pause')
        else:
            verbs.append('play')

        volume_filled = int(s.getVolume()/10)

        self.render('index.html',
                    verbs=verbs,
                    track_name=s.getTrackName(),
                    track_artist=s.getTrackArtist(),
                    volume_filled = volume_filled,
                    volume_empty = 10 - volume_filled)

class DoHandler(tornado.web.RequestHandler):
    def post(self):
        verb = self.get_argument('verb')
        s = Spotify()
        if verb == '+':
            s.louder()
        elif verb == '-':
            s.quieter()
        else:
            s.tell(verb)

        self.redirect('/')

class ArtHandler(tornado.web.RequestHandler):
    def get(self):
        s = Spotify()
        self.set_header('Content-Type', 'image/jpeg')
        self.write(s.getArt())

application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/do", DoHandler),
    (r"/art", ArtHandler),
    ])

if __name__ == "__main__":
    # note the local address and report it to the master so that iphone
    # clients can simply hit the master's URL, and it'll know where to
    # redirect them so they hit this server (whatever IP it happens to be on)
    local_addr = socket.gethostbyname(socket.gethostname())
    f = urllib2.urlopen("http://a.mkjon.es:9999/register", "addr=%s" % (local_addr))
    print "Local address: %s, reported remote address; %s" % (
        local_addr,
        f.readline())

    # now start up the app itself
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
Something went wrong with that request. Please try again.