-
Notifications
You must be signed in to change notification settings - Fork 15
/
spotify.py
384 lines (315 loc) · 12.6 KB
/
spotify.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#
# Use the Spotify Web Connect API using credentials auth code and tokens.
#
import json
import requests
import time
import urlparse
import sys
from myopenhab import openhab
from myopenhab import mapValues
from myopenhab import getJSONValue
# API Gateway
ACCOUNT_URL = 'https://accounts.spotify.com/api/token'
API_ROOT_URL = 'https://api.spotify.com/v1/me/player/'
REDIRECT_URI = 'http://openhabianpi.local:8080/static/spotify-auth.html'
VOL_INCREMENT = 10
class spotify(object):
"""
A wrapper for the Spotify Web Connect API
https://developer.spotify.com/web-api/web-api-connect-endpoint-reference/
"""
def __init__(self):
self.debug = True
self.oh = openhab()
self.client_id = self.oh.getState('spotify_client_id')
self.client_secret = self.oh.getState('spotify_client_secret')
self.access_token = self.oh.getState('spotify_access_token')
self.refresh_token = self.oh.getState('spotify_refresh_token')
self.token_issued = self.oh.getState('spotify_token_issued')
self.token_expiry = self.oh.getState('spotify_token_expiry')
if(self.token_expiry == "NULL"):
self.refreshCredentials()
if (self.access_token == "NULL"):
self.generateCredentials()
else:
if (time.time() > float(self.token_expiry)):
self.refreshCredentials()
def generateCredentials(self):
"""
Generate auth and refresh token for the very first time.
"""
# Send OAuth payload to get access_token
payload = { 'code':self.oh.getState('spotify_auth_code'), 'client_id':self.client_id, 'client_secret':self.client_secret, 'redirect_uri':REDIRECT_URI, 'grant_type':'authorization_code' }
print "-- Calling Token Service for the first time"
try:
r = requests.post(ACCOUNT_URL, data=payload, allow_redirects=False)
if (self.debug): print r.headers
if (self.debug): print r.json()
resp = r.json()
if(r.status_code == 200):
access_token = resp['access_token']
refresh_token = resp['refresh_token']
expires_in = resp['expires_in']
# Set and Save the access token
self.access_token = access_token
self.refresh_token = refresh_token
self.token_expiry = time.time() + float(expires_in)
self.token_issued = time.strftime("%Y-%m-%dT%H:%M:%S")
self.saveCredentials()
except:
print " -> Error getting token:" + str(sys.exc_info()[1])
def refreshCredentials(self):
"""
If previous auth token expired, get a new one with refresh token.
"""
# Send OAuth payload to get access_token
payload = { 'refresh_token':self.refresh_token, 'client_id':self.client_id, 'client_secret':self.client_secret, 'redirect_uri':REDIRECT_URI, 'grant_type':'refresh_token' }
print "-- Calling Token Refresh Service"
try:
r = requests.post(ACCOUNT_URL, data=payload, allow_redirects=False)
if (self.debug): print r.headers
if (self.debug): print r.json()
resp = r.json()
if(r.status_code == 200):
access_token = resp['access_token']
expires_in = resp['expires_in']
if('refresh_token' in resp):
refresh_token = resp['refresh_token']
self.refresh_token = refresh_token
# Set and Save the access token
self.access_token = access_token
self.token_expiry = time.time() + float(expires_in)
self.token_issued = time.strftime("%Y-%m-%dT%H:%M:%S")
self.saveCredentials()
except:
print " -> Error refreshing token:" + str(sys.exc_info()[1])
def saveCredentials(self):
"""
Save current tokens to the openhab.
"""
self.oh.sendCommand('spotify_access_token',self.access_token)
self.oh.sendCommand('spotify_refresh_token',self.refresh_token)
self.oh.sendCommand('spotify_token_expiry',self.token_expiry)
self.oh.sendCommand('spotify_token_issued',self.token_issued)
def call(self, path, mode=None, payload=None):
"""
Call the API at the given path.
"""
if (time.time() > self.token_expiry):
self.refreshCredentials()
headers = {"Authorization": "Bearer " + self.access_token, "Content-Type": "application/json" }
if mode == "POST":
r = requests.post(API_ROOT_URL + path, headers=headers, data=payload)
if(r.status_code < 200 and r.status_code > 299):
print "Response Code = " + str(r.status_code)
print r.content
return r.status_code
elif mode == "PUT":
r = requests.put(API_ROOT_URL + path, headers=headers, data=payload)
if(r.status_code < 200 and r.status_code > 299):
print "Response Code = " + str(r.status_code)
print r.content
return r.status_code
else:
r = requests.get(API_ROOT_URL + path, headers=headers)
if(r.status_code < 200 and r.status_code > 299):
print "Response Code = " + str(r.status_code)
print r.content
return r.json()
def update(self):
"""
Get a current player state.
"""
print "-- Calling Service: Update"
try:
resp = self.call("")
if (self.debug): print resp
if ('item' in resp):
self.oh.sendCommand('spotify_current_track', getJSONValue(resp, ['item','name']))
self.oh.sendCommand('spotify_current_artist', getJSONValue(resp, ['item', 'artists', 0, 'name']))
self.oh.sendCommand('spotify_current_cover', getJSONValue(resp, ['item', 'album', 'images', 1, 'url']))
self.oh.sendCommand('spotify_current_duration', getJSONValue(resp, ['item', 'duration_ms']))
self.oh.sendCommand('spotify_current_progress', getJSONValue(resp, ['progress_ms']))
self.oh.sendCommand('spotify_current_playing', mapValues(getJSONValue(resp, ['is_playing']), { 'True': 'ON', 'False': 'OFF' }))
self.oh.sendCommand('spotify_current_device', getJSONValue(resp, ['device', 'name']))
self.oh.sendCommand('spotify_current_volume', getJSONValue(resp, ['device', 'volume_percent']))
self.oh.sendCommand('spotify_current_context_uri', getJSONValue(resp, ['context', 'uri']))
self.oh.sendCommand('spotify_current_device_id', getJSONValue(resp, ['device', 'id']))
duration = getJSONValue(resp, ['item', 'duration_ms'])
progress = getJSONValue(resp, ['progress_ms'])
if(duration is not None and progress is not None):
progress_percent = round(float(progress) / float(duration) * 100,2)
else:
progress_percent = 0
self.oh.sendCommand('spotify_current_progress_percent', progress_percent)
print " -> Success"
else:
print " -> Item node missing from response :("
except:
print " -> Failure: ", sys.exc_info()[0]
resp = ""
return resp
def getDevices(self):
"""
Get List of Devices
"""
print "-- Calling Service: Get Devices"
try:
resp = self.call("devices")
resp = json.dumps(resp["devices"])
self.oh.sendCommand('spotify_device_list',resp)
if (self.debug): print resp
except:
print " -> Device List Failure: ", sys.exc_info()[0]
resp = ""
return resp
def transferPlayback(self):
"""
Transfer Playback from one device to another
"""
print "-- Calling Service: Transfer Playback"
deviceID = self.oh.getState('spotify_current_device_id')
payload = json.dumps({ 'device_ids': [ deviceID ] })
print payload
try:
resp = self.call("","PUT", payload = payload)
if (self.debug): print resp
self.update()
except:
print " -> Transfer Playback Failure: ", sys.exc_info()[0]
resp = ""
return resp
def volumeUp(self):
"""
Volume up by 10%
"""
print "-- Calling Service: Volume Up"
try:
vol = int(self.oh.getState('spotify_current_volume'))
vol = int(round(vol/10)*10 + VOL_INCREMENT)
if(vol>100):
vol = 100
print " -> Volume To:" + str(vol)
resp = self.call("volume?volume_percent=" + str(vol),"PUT" )
self.oh.sendCommand('spotify_current_volume',vol)
if (self.debug): print resp
except:
print " -> VolumeUp Failure: ", sys.exc_info()[0]
resp = ""
return resp
def volumeDown(self):
"""
Volume down by 10%
"""
print "-- Calling Service: Volume Down"
try:
vol = int(self.oh.getState('spotify_current_volume'))
vol = int(round(vol/10)*10 - VOL_INCREMENT)
if(vol<0):
vol = 0
print "Volume To:" + str(vol)
resp = self.call("volume?volume_percent=" + str(vol),"PUT" )
self.oh.sendCommand('spotify_current_volume',vol)
if (self.debug): print resp
except:
print " -> VolumeDown Failure: ", sys.exc_info()[0]
resp = ""
return resp
def pause(self):
"""
Pause player
"""
print "-- Calling Service: Pause"
try:
resp = self.call("pause","PUT")
self.oh.sendCommand('spotify_current_playing',"OFF")
if (self.debug): print resp
except:
print " -> Pause Failure: ", sys.exc_info()[0]
resp = ""
return resp
def play(self, context_uri = None):
"""
Resume player
"""
print "-- Calling Service: Play"
deviceID = self.oh.getState('spotify_current_device_id')
if (deviceID == ''):
command = "play"
else:
command = "play?device_id=" + deviceID
if (context_uri is None):
payload = {}
else:
payload = json.dumps({ 'context_uri': context_uri })
try:
resp = self.call(command,"PUT", payload = payload)
if (self.debug): print resp
self.update()
except:
print " -> Play Failure: ", sys.exc_info()[0]
resp = ""
return resp
def previous(self):
"""
Skip to previous track
"""
print "-- Calling Service: Previous"
try:
resp = self.call("previous","POST")
if (self.debug): print resp
self.update()
except:
print " -> Previous Failure: ", sys.exc_info()[0]
resp = ""
return resp
def next(self):
"""
Skip to next track
"""
print "-- Calling Service: Next"
try:
resp = self.call("next","POST")
if (self.debug): print resp
self.update()
except:
print " -> Next Failure: ", sys.exc_info()[0]
resp = ""
return resp
def updateConnectionDateTime(self):
self.oh.sendCommand('spotify_lastConnectionDateTime',time.strftime("%Y-%m-%dT%H:%M:%S+0000",time.gmtime(time.time())))
def main():
t1 = time.time()
c = spotify()
args = sys.argv
if(len(args) == 1):
c.update()
else:
if(args[1] == "get_devices"):
c.getDevices()
if(args[1] == "transfer_playback"):
c.transferPlayback()
if(args[1] == "volume_up"):
c.volumeUp()
if(args[1] == "volume_down"):
c.volumeDown()
if(args[1] == "play"):
if(len(args)>2):
a = ""
for x in range(2, len(args)):
a = a + args[x] + " "
c.play(a.strip())
else:
c.play()
if(args[1] == "pause"):
c.pause()
if(args[1] == "previous"):
c.previous()
if(args[1] == "next"):
c.next()
c.updateConnectionDateTime()
t2 = time.time()
print "Done in " + str(t2-t1) + " seconds"
if __name__ == '__main__':
main()