Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: dedfa0fb6e
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 77 lines (61 sloc) 2.166 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 65 66 67 68 69 70 71 72 73 74 75 76
import os
import subprocess
import binhex
import binascii


class Spotify:
    def tell(self, verb):
        verbs = ('play', 'pause', 'skip')
        if not verb in verbs:
            raise Exception('Invalid verb %s' % verb)
        if verb == 'skip':
            verb = 'next track'
        self._tell(verb)

    def isPlaying(self):
        out = self._get('player state').strip()
        print out
        return out == 'playing'

    def getTrackName(self):
        return self._get('name of current track')

    def getTrackArtist(self):
        return self._get('artist of current track')

    def louder(self):
        current = self.getVolume()
        self.setVolume(current + 10)

    def quieter(self):
        current = self.getVolume()
        self.setVolume(current - 10)

    def getVolume(self):
        settings = self._exec('get volume settings')
        return int(settings.split(':')[1].split(',')[0])

    def setVolume(self, value):
        if (value > 100):
            raise Exception('invalid value %d' % (value))
        if (value < 0):
            raise Exception('too low value %d' % (value))

        self._exec('set volume output volume %d' % (value))

    def getArt(self):
        raw = self._get('artwork of current track')
        binary = binascii.a2b_hex(raw[11:-3])
        f = open('/tmp/whatever.tiff', 'w')
        f.write(binary)
        f.close()
        subprocess.Popen(('convert', '/tmp/whatever.tiff',
                          '/tmp/whatever.jpg')).wait()
        jpg = open('/tmp/whatever.jpg')
        ret = jpg.read(1000000)
        jpg.close()
        return ret

    def _get(self, thing):
        things = (
            'artwork of current track',
            'player state',
            'artist of current track',
            'name of current track',
            )
        return self._exec('tell application "Spotify" to set foo to %s' % (thing))

    def _tell(self, verb):
        self._exec('tell application "Spotify" to %s' % (verb))

    def _exec(self, script):
        args = ('osascript', '-e', script)
        sp = subprocess.Popen(args, stdout=subprocess.PIPE)
        return sp.stdout.readline()

Something went wrong with that request. Please try again.