-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_credentials.py
executable file
·357 lines (288 loc) · 10.7 KB
/
spotify_credentials.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
import sys
if sys.implementation.name == 'micropython':
# noinspection PyUnresolvedReferences
import usocket as socket
# noinspection PyUnresolvedReferences
import urequests as requests
# noinspection PyUnresolvedReferences
import ujson as json
class JSONDecodeError(Exception):
pass
# noinspection PyShadowingBuiltins
class FileNotFoundError(Exception):
pass
else:
import socket
import requests
import json
from json import JSONDecodeError
INITIAL_RESPONSE_TEMPLATE = """\
HTTP/1.0 200 OK
Content-Type: text/html
<h1>Authenticate with Spotify</h1>
1) Go to <a target="_blank" href="https://developer.spotify.com/dashboard/applications">Spotify for Developers</a> and "Create an app"<br>
2) Edit Settings on the app, add "{redirect_uri}" as a Redirect URI and Save<br>
3) Enter Client ID below, submit and then allow the scopes for the app.<br><br>
<form action="/auth-request" method="post">
client_id: <input type="text" name="client_id" size="34" value="{default_client_id}"><br><br>
client_secret: <input type="text" name="client_secret" size="34" value="{default_client_secret}"><br><br>
<input type="submit" value="Submit">
</form>
"""
SELECT_DEVICE_TEMPLATE = """\
HTTP/1.0 200 OK
Content-Type: text/html
<h1>Select device</h1>
<form action="/select-device" method="post">
{device_list}
<input type="submit" value="Submit">
</form>
"""
AUTH_REDIRECT_TEMPLATE = """\
HTTP/1.0 302 Found
Location: {url}
"""
NOT_FOUND = """\
HTTP/1.0 404 NOT FOUND
"""
DONE_RESPONSE = """\
HTTP/1.0 200 OK
Content-Type: text/html
OK
"""
def setup_authorization_code_flow(default_client_id='', default_client_secret='', default_device_id=''):
micropython_optimize = sys.implementation.name == 'micropython'
s = socket.socket()
# Binding to all interfaces - server will be accessible to other hosts!
ai = socket.getaddrinfo("0.0.0.0", 8080)
addr = ai[0][-1]
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print("Listening, connect your browser to http://{myip}:8080/".format(myip=myip()))
redirect_uri = None
client_id = None
client_secret = None
credentials = None
device_selected = False
while not device_selected:
client_sock, _ = s.accept()
if micropython_optimize:
client_stream = client_sock
else:
client_stream = client_sock.makefile("rwb")
req = client_stream.readline().decode()
content_length = None
while True:
h = client_stream.readline().decode()
if h.startswith("Host: "):
host = h[6:-2]
redirect_uri = 'http://{host}/auth-response/'.format(host=host)
if h.startswith("Content-Length: "):
content_length = int(h[16:-2])
if h == "" or h == "\r\n":
break
def write_response(resp):
client_stream.write(resp.encode())
client_stream.close()
if not micropython_optimize:
client_sock.close()
if req.startswith("GET / "):
write_response(INITIAL_RESPONSE_TEMPLATE.format(
redirect_uri=redirect_uri,
default_client_id=default_client_id,
default_client_secret=default_client_secret,
))
elif req.startswith("POST /auth-request"):
authorization_endpoint = 'https://accounts.spotify.com/authorize'
form_values = parse_qs(client_stream.read(content_length).decode())
client_id = form_values['client_id'][0]
client_secret = form_values['client_secret'][0]
params = dict(
client_id=client_id,
response_type='code',
redirect_uri=redirect_uri,
scope='user-read-playback-state user-modify-playback-state',
)
url = "{path}?{query}".format(path=authorization_endpoint, query=urlencode(params))
write_response(AUTH_REDIRECT_TEMPLATE.format(url=url))
elif req.startswith("GET /auth-response"):
authorization_code = parse_qs(req[4:-11].split('?')[1])['code'][0]
credentials = get_access_tokens(authorization_code, redirect_uri, client_id, client_secret)
device_list = get_available_devices(credentials)
device_list_html = []
for device in device_list:
checked = 'checked' if device['id'] == default_device_id else ''
device_list_html.append("""<input type="radio" name="device_id" value="{id}" {checked}> {name}<br>""".format(checked=checked, **device))
write_response(SELECT_DEVICE_TEMPLATE.format(device_list=''.join(device_list_html)))
elif req.startswith("POST /select-device"):
device_selected = True
response = client_stream.read(content_length).decode()
credentials['device_id'] = parse_qs(response)['device_id'][0]
write_response(DONE_RESPONSE)
else:
write_response(NOT_FOUND)
return credentials
def get_access_tokens(authorization_code, redirect_uri, client_id, client_secret):
params = dict(
grant_type="authorization_code",
code=authorization_code,
redirect_uri=redirect_uri,
client_id=client_id,
client_secret=client_secret,
)
access_token_endpoint = "https://accounts.spotify.com/api/token"
response = requests.post(
access_token_endpoint,
headers={'Content-Type': 'application/x-www-form-urlencoded'},
data=urlencode(params),
)
tokens = response.json()
return dict(
access_token=tokens['access_token'],
refresh_token=tokens['refresh_token'],
client_id=client_id,
client_secret=client_secret,
)
def get_available_devices(credentials):
devices_endpoint = "https://api.spotify.com/v1/me/player/devices"
return requests.get(
devices_endpoint,
headers={'Authorization': "Bearer {access_token}".format(**credentials)},
).json()['devices']
def refresh_access_token(credentials):
access_token_endpoint = "https://accounts.spotify.com/api/token"
params = dict(
grant_type="refresh_token",
refresh_token=credentials['refresh_token'],
client_id=credentials['client_id'],
client_secret=credentials['client_secret'],
)
response = requests.post(
access_token_endpoint,
headers={'Content-Type': 'application/x-www-form-urlencoded'},
data=urlencode(params),
)
if response.status_code != 200:
raise RefreshAccessTokenError
tokens = response.json()
credentials['access_token'] = tokens['access_token']
if 'refresh_token' in tokens:
credentials['refresh_token'] = tokens['refresh_token']
store_credentials(credentials)
class RefreshAccessTokenError(Exception):
pass
def get_credentials(setup=False):
credentials = load_credentials()
if credentials:
if not setup:
try:
refresh_access_token(credentials)
except RefreshAccessTokenError:
setup = True
if credentials.get('invalide') == 'true':
setup = True
if setup:
credentials = setup_authorization_code_flow(
default_client_id=credentials['client_id'],
default_client_secret=credentials['client_secret'],
default_device_id=credentials['device_id'],
)
store_credentials(credentials)
else:
credentials = setup_authorization_code_flow()
store_credentials(credentials)
return credentials
def load_credentials():
try:
with open('credentials.json') as credentials_file:
credentials = json.loads(credentials_file.read())
assert credentials['refresh_token']
assert credentials['client_id']
assert credentials['client_secret']
assert credentials['device_id']
except (OSError, ValueError, FileNotFoundError, JSONDecodeError, KeyError, AssertionError):
credentials = None
return credentials
def store_credentials(credentials):
with open('credentials.json', 'w') as credentials_file:
credentials_file.write(json.dumps(credentials))
def invalidate_credentials():
credentials = load_credentials()
if credentials is not None:
credentials['invalide'] = 'true'
store_credentials(credentials)
def myip():
if sys.implementation.name == 'micropython':
try:
import network
return network.WLAN(network.STA_IF).ifconfig()[0]
except ImportError:
return "<my host>"
else:
return (([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0]
def parse_qs(qs):
parsed_result = {}
pairs = parse_qsl(qs)
for name, value in pairs:
if name in parsed_result:
parsed_result[name].append(value)
else:
parsed_result[name] = [value]
return parsed_result
def parse_qsl(qs):
pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
r = []
for name_value in pairs:
if not name_value:
continue
nv = name_value.split('=', 1)
if len(nv) != 2:
continue
if len(nv[1]):
name = nv[0].replace('+', ' ')
name = unquote(name)
value = nv[1].replace('+', ' ')
value = unquote(value)
r.append((name, value))
return r
def quote(s):
always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789' '_.-')
res = []
for c in s:
if c in always_safe:
res.append(c)
continue
res.append('%%%x' % ord(c))
return ''.join(res)
def quote_plus(s):
s = quote(s)
if ' ' in s:
s = s.replace(' ', '+')
return s
def unquote(s):
res = s.split('%')
for i in range(1, len(res)):
item = res[i]
try:
res[i] = chr(int(item[:2], 16)) + item[2:]
except ValueError:
res[i] = '%' + item
return "".join(res)
def unquote_plus(s):
s = s.replace('+', ' ')
return unquote(s)
def urlencode(query):
if isinstance(query, dict):
query = query.items()
li = []
for k, v in query:
if not isinstance(v, list):
v = [v]
for value in v:
k = quote_plus(str(k))
v = quote_plus(str(value))
li.append(k + '=' + v)
return '&'.join(li)