-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.py
40 lines (28 loc) · 1.13 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
# -*- coding: UTF-8 -*-
import httplib
import urllib
import json
class Spotify:
basicAuth='XXX'
refreshToken='XXX'
@staticmethod
def currentlyPlaying(accessToken):
connection = httplib.HTTPSConnection('api.spotify.com')
connection.request('GET', '/v1/me/player/currently-playing', headers={'Authorization': 'Bearer ' + accessToken})
response = connection.getresponse()
responseData = response.read()
connection.close()
if response.status == 200 and responseData != '':
return json.loads(responseData, 'utf-8')
else:
return {'is_playing': False, 'token_expired': response.status == 401}
@staticmethod
def getAccessToken():
headers = {'Authorization': 'Basic ' + Spotify.basicAuth, 'Accept': '*/*', 'Content-Type': 'application/x-www-form-urlencoded'}
params = urllib.urlencode({'grant_type': 'refresh_token', 'refresh_token': Spotify.refreshToken})
connection = httplib.HTTPSConnection('accounts.spotify.com')
connection.request('POST', '/api/token', params, headers)
response = connection.getresponse()
data = json.loads(response.read(), 'utf-8')
connection.close()
return data['access_token']