-
Notifications
You must be signed in to change notification settings - Fork 0
/
pymotify.py
89 lines (71 loc) · 2.72 KB
/
pymotify.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
import requests
from time import sleep
class PymotifyException(Exception):
pass
class Pymotify:
URL = 'http://127.0.0.1:{0}{1}'
def __init__(self):
self.session = requests.session()
self.csrf_token = self.oauth_token = self.port = None
# handshake
try:
self._find_port()
self._get_csrf_token()
self._get_oauth_token()
except PymotifyException as err:
print('Error', err)
# finds the port that Spotify Web Helper is using
def _find_port(self):
possible_ports = range(4370, 4390)
for port in possible_ports:
try:
self.session.get(Pymotify.URL.format(port, '/simplecsrf/token.json'), timeout=0.05)
except requests.Timeout or requests.ConnectionError:
continue
self.port = port
return port
raise PymotifyException('Have not found Web Helper port: check if Spotify is on? Try restarting it.')
# general method for local calls to Web Helper
def _local_call(self, uri, req_params={}):
headers = dict(Origin='https://open.spotify.com')
params = {
'oauth': self.oauth_token,
'csrf': self.csrf_token
}
params.update(req_params)
try:
res = self.session.get(Pymotify.URL.format(self.port, uri), headers=headers, params=params)
except:
raise PymotifyException('Connection failed')
try:
res_json = res.json()
except ValueError as err:
raise PymotifyException('Unable to decode JSON result: {}'.format(err))
if res_json.get('error'):
raise PymotifyException('Response error:', int(res_json.get('error').get('type', '0')))
return res_json
# obtains CSRF token from Web Helper
def _get_csrf_token(self):
res = self._local_call('/simplecsrf/token.json')
self.csrf_token = res.get('token')
# obtains OAuth token from open.spotify.com
def _get_oauth_token(self):
res = self.session.get('http://open.spotify.com/token')
self.oauth_token = res.json().get('t')
# pause
def pause(self):
return self._local_call('/remote/pause.json', req_params={'pause': 'true'})
# unpause
def unpause(self):
return self._local_call('/remote/pause.json', req_params={'pause': 'false'})
# play
def play(self, uri):
return self._local_call('/remote/play.json', req_params={'uri': uri, 'context': uri})
if __name__ == '__main__':
a = Pymotify()
# Plays Hot Chip's Flutes, sleeps 10s, pauses, waits 3s and then unpauses
a.play('spotify:track:65NKfdLTAnL8ubOho5jCXA')
sleep(10)
a.pause()
sleep(3)
a.unpause()