Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 77 lines (61 sloc) 2.166 kB
f441520 @mkjones first commit - it should work!
authored
1 import os
2 import subprocess
3 import binhex
dedfa0f @mkjones add (ghetto?) support for album art
authored
4 import binascii
f441520 @mkjones first commit - it should work!
authored
5
6
7 class Spotify:
8 def tell(self, verb):
9 verbs = ('play', 'pause', 'skip')
10 if not verb in verbs:
11 raise Exception('Invalid verb %s' % verb)
12 if verb == 'skip':
13 verb = 'next track'
14 self._tell(verb)
15
16 def isPlaying(self):
17 out = self._get('player state').strip()
18 print out
19 return out == 'playing'
20
21 def getTrackName(self):
22 return self._get('name of current track')
23
24 def getTrackArtist(self):
25 return self._get('artist of current track')
26
b8a5d60 @mkjones add ghetto volume up / down buttons
authored
27 def louder(self):
28 current = self.getVolume()
29 self.setVolume(current + 10)
30
31 def quieter(self):
32 current = self.getVolume()
33 self.setVolume(current - 10)
34
35 def getVolume(self):
36 settings = self._exec('get volume settings')
37 return int(settings.split(':')[1].split(',')[0])
38
39 def setVolume(self, value):
40 if (value > 100):
41 raise Exception('invalid value %d' % (value))
42 if (value < 0):
43 raise Exception('too low value %d' % (value))
44
45 self._exec('set volume output volume %d' % (value))
46
f441520 @mkjones first commit - it should work!
authored
47 def getArt(self):
48 raw = self._get('artwork of current track')
dedfa0f @mkjones add (ghetto?) support for album art
authored
49 binary = binascii.a2b_hex(raw[11:-3])
50 f = open('/tmp/whatever.tiff', 'w')
51 f.write(binary)
52 f.close()
53 subprocess.Popen(('convert', '/tmp/whatever.tiff',
54 '/tmp/whatever.jpg')).wait()
55 jpg = open('/tmp/whatever.jpg')
56 ret = jpg.read(1000000)
57 jpg.close()
58 return ret
f441520 @mkjones first commit - it should work!
authored
59
60 def _get(self, thing):
61 things = (
62 'artwork of current track',
63 'player state',
64 'artist of current track',
65 'name of current track',
66 )
b8a5d60 @mkjones add ghetto volume up / down buttons
authored
67 return self._exec('tell application "Spotify" to set foo to %s' % (thing))
68
69 def _tell(self, verb):
70 self._exec('tell application "Spotify" to %s' % (verb))
71
72 def _exec(self, script):
73 args = ('osascript', '-e', script)
f441520 @mkjones first commit - it should work!
authored
74 sp = subprocess.Popen(args, stdout=subprocess.PIPE)
75 return sp.stdout.readline()
76
Something went wrong with that request. Please try again.