-
Notifications
You must be signed in to change notification settings - Fork 28
/
SpotifyClient.cpp
500 lines (436 loc) · 14.3 KB
/
SpotifyClient.cpp
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/**The MIT License (MIT)
Copyright (c) 2018 by ThingPulse Ltd., https://thingpulse.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
#include "SpotifyClient.h"
SpotifyClient::SpotifyClient(String clientId, String clientSecret, String redirectUri, WiFiClientSecure *wifiClient) {
this->clientId = clientId;
this->clientSecret = clientSecret;
this->redirectUri = redirectUri;
this->wifiClient = wifiClient;
}
uint16_t SpotifyClient::update(SpotifyData *data, SpotifyAuth *auth) {
this->data = data;
level = 0;
isDataCall = true;
currentParent = "";
JsonStreamingParser parser;
parser.setListener(this);
String host = "api.spotify.com";
const int port = 443;
String url = "/v1/me/player/currently-playing";
if (!wifiClient->connect(host.c_str(), port)) {
Serial.println("connection failed");
return 0;
}
Serial.print("Requesting URL: ");
String request = "GET " + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + auth->accessToken + "\r\n" +
"Connection: close\r\n\r\n";
// This will send the request to the server
Serial.println(request);
wifiClient->print(request);
int retryCounter = 0;
while (!wifiClient->available()) {
executeCallback();
retryCounter++;
if (retryCounter > 10) {
return 0;
}
delay(10);
}
uint16_t bufLen = 1024;
unsigned char buf[bufLen];
boolean isBody = false;
char c = ' ';
int size = 0;
wifiClient->setNoDelay(false);
uint16_t httpCode = 0;
while (wifiClient->connected() || wifiClient->available()) {
while ((size = wifiClient->available()) > 0) {
if (isBody) {
uint16_t len = _min(bufLen, size);
c = wifiClient->readBytes(buf, len);
for (uint16_t i = 0; i < len; i++) {
parser.parse(buf[i]);
// Serial.print((char)buf[i]);
}
} else {
String line = wifiClient->readStringUntil('\r');
Serial.println(line);
if (line.startsWith("HTTP/1.")) {
httpCode = line.substring(9, line.indexOf(' ', 9)).toInt();
Serial.printf("HTTP Code: %d\n", httpCode);
}
if (line == "\r" || line == "\n" || line == "") {
Serial.println("Body starts now");
isBody = true;
}
}
executeCallback();
}
}
if (httpCode == 200) {
this->data->isPlayerActive = true;
} else if (httpCode == 204) {
this->data->isPlayerActive = false;
}
// client.flush();
// client.stop();
this->data = nullptr;
return httpCode;
}
uint16_t SpotifyClient::playerCommand(SpotifyAuth *auth, String method, String command) {
level = 0;
isDataCall = true;
currentParent = "";
JsonStreamingParser parser;
parser.setListener(this);
String host = "api.spotify.com";
const int port = 443;
String url = "/v1/me/player/" + command;
if (!wifiClient->connect(host.c_str(), port)) {
Serial.println("connection failed");
return 0;
}
Serial.print("Requesting URL: ");
String request = method + " " + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + auth->accessToken + "\r\n" +
"Content-Length: 0\r\n" +
"Connection: close\r\n\r\n";
// This will send the request to the server
Serial.println(request);
wifiClient->print(request);
int retryCounter = 0;
while (!wifiClient->available()) {
executeCallback();
retryCounter++;
if (retryCounter > 10) {
return 0;
}
delay(10);
}
uint16_t bufLen = 1024;
unsigned char buf[bufLen];
boolean isBody = false;
char c = ' ';
int size = 0;
wifiClient->setNoDelay(false);
uint16_t httpCode = 0;
while (wifiClient->connected() || wifiClient->available()) {
while ((size = wifiClient->available()) > 0) {
if (isBody) {
uint16_t len = _min(bufLen, size);
c = wifiClient->readBytes(buf, len);
for (uint16_t i = 0; i < len; i++) {
parser.parse(buf[i]);
// Serial.print((char)buf[i]);
}
} else {
String line = wifiClient->readStringUntil('\r');
Serial.println(line);
if (line.startsWith("HTTP/1.")) {
httpCode = line.substring(9, line.indexOf(' ', 9)).toInt();
Serial.printf("HTTP Code: %d\n", httpCode);
}
if (line == "\r" || line == "\n" || line == "") {
Serial.println("Body starts now");
isBody = true;
}
}
}
executeCallback();
}
return httpCode;
}
void SpotifyClient::getToken(SpotifyAuth *auth, String grantType, String code) {
this->auth = auth;
isDataCall = false;
JsonStreamingParser parser;
parser.setListener(this);
// https://accounts.spotify.com/api/token
const char *host = "accounts.spotify.com";
const int port = 443;
String url = "/api/token";
if (!wifiClient->connect(host, port)) {
Serial.printf("Connection to %s:%d failed; returning.\n", host, port);
wifiClient->stop();
return;
}
Serial.print("Requesting URL: ");
String codeParam = "code";
if (grantType == "refresh_token") {
codeParam = "refresh_token";
}
String authorizationRaw = clientId + ":" + clientSecret;
String authorization = base64::encode(authorizationRaw, false);
// This will send the request to the server
String content = "grant_type=" + grantType + "&" + codeParam + "=" + code + "&redirect_uri=" + redirectUri;
String request = String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Basic " + authorization + "\r\n" +
"Content-Length: " + String(content.length()) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Connection: close\r\n\r\n" +
content;
Serial.println(request);
wifiClient->print(request);
char c;
boolean isBody = false;
unsigned long timeoutMillis = 10000UL;
unsigned long startMillis = millis();
while (wifiClient->connected() || wifiClient->available()) {
if (wifiClient->available()) {
if ((millis() - startMillis) > timeoutMillis) {
Serial.printf("HTTP timeout after %ds.\n", (timeoutMillis/1000));
wifiClient->stop();
ESP.restart();
}
c = wifiClient->read();
Serial.print(c);
if (c == '{' || c == '[') {
isBody = true;
}
if (isBody) {
parser.parse(c);
}
}
executeCallback();
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
wifiClient->stop();
this->data = nullptr;
}
String SpotifyClient::startConfigPortal(const String mDnsName) {
String oneWayCode = "";
Serial.println("Starting config portal");
server.on("/", [this]() {
Serial.println("Serving resource '/'");
Serial.println("Sending HTTP 302 to Spotify");
server.sendHeader(
"Location",
String("https://accounts.spotify.com/authorize/?client_id=" +
this->clientId +
"&response_type=code&redirect_uri=" + this->redirectUri +
"&scope=user-read-private%20user-read-currently-playing%20user-"
"read-playback-state%20user-modify-playback-state"),
true);
server.send(302, "text/plain", "");
});
server.on("/callback/", [this, &oneWayCode]() {
if (!server.hasArg("code")) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
oneWayCode = server.arg("code");
Serial.printf("Code: %s\n", oneWayCode.c_str());
String message = "<html><head></head><body>Succesfully authentiated this device with Spotify. Restart your device now</body></html>";
server.send(200, "text/html", message);
});
server.begin();
Serial.println("HTTP server started");
if (!MDNS.begin(mDnsName)) {
Serial.println("Error setting up MDNS responder!");
}
Serial.println("MDNS responder started");
Serial.println("Open browser at http://" + mDnsName + ".local");
while (oneWayCode == "") {
MDNS.update();
server.handleClient();
yield();
}
Serial.println("Stopping HTTP server");
server.stop();
Serial.println("Stopping MDNS responder");
MDNS.close();
return oneWayCode;
}
void SpotifyClient::whitespace(char c) {
Serial.println("whitespace");
}
void SpotifyClient::startDocument() {
Serial.println("start document");
level = 0;
}
void SpotifyClient::key(String key) {
currentKey = String(key);
rootPath[level] = key;
}
String SpotifyClient::getRootPath() {
String path = "";
for (uint8_t i = 1; i <= level; i++) {
String currentLevel = rootPath[i];
if (currentLevel == "") {
break;
}
if (i > 1) {
path += ".";
}
path += currentLevel;
}
return path;
}
void SpotifyClient::value(String value) {
if (isDataCall) {
String rootPath = this->getRootPath();
// Serial.printf("%s = %s\n", rootPath.c_str(), value.c_str());
// Serial.printf("%s = %s\n", currentKey.c_str(), value.c_str());
// progress_ms = 37516 uint32_t progressMs;
if (currentKey == "progress_ms") {
data->progressMs = value.toInt();
}
// duration_ms = 259120 uint32_t durationMs;
if (currentKey == "duration_ms") {
data->durationMs = value.toInt();
}
// name = Lost in My MindString name;
if (currentKey == "name") {
data->title = value;
}
// is_playing = true boolean isPlaying;
if (currentKey == "is_playing") {
data->isPlaying = (value == "true" ? true : false);
}
if (currentKey == "height") {
currentImageHeight = value.toInt();
}
if (currentKey == "url") {
Serial.printf("HREF: %s = %s", rootPath.c_str(), value.c_str());
if (rootPath == "item.album.images.url") {
if (currentImageHeight == 640) {
data->image640Href = value;
}
if (currentImageHeight > 250 && currentImageHeight < 350) {
data->image300Href = value;
}
if (currentImageHeight == 64) {
data->image64Href = value;
}
}
}
if (rootPath == "item.album.artists.name") {
data->artistName = value;
}
} else {
Serial.printf("\n%s=%s\n", currentKey.c_str(), value.c_str());
// "access_token": "XXX" String accessToken;
if (currentKey == "access_token") {
auth->accessToken = value;
}
// "token_type":"Bearer", String tokenType;
if (currentKey == "token_type") {
auth->tokenType = value;
}
// "expires_in":3600, uint16_t expiresIn;
if (currentKey == "expires_in") {
auth->expiresIn = value.toInt();
}
// "refresh_token":"XX", String refreshToken;
if (currentKey == "refresh_token") {
auth->refreshToken = value;
}
// "scope":"user-modify-playback-state user-read-playback-state user-read-currently-playing user-read-private String scope;
if (currentKey == "scope") {
auth->scope = value;
}
}
}
void SpotifyClient::endArray() {}
void SpotifyClient::startObject() {
// Serial.println("Starting new object: " + currentKey);
currentParent = currentKey;
// rootPath[level] = currentKey;
level++;
// level++;*/
}
void SpotifyClient::endObject() {
// rootPath[level] = "";
level--;
currentParent = "";
}
void SpotifyClient::endDocument() {}
void SpotifyClient::startArray() {}
void SpotifyClient::executeCallback() {
if (drawingCallback != nullptr) {
(*this->drawingCallback)();
}
}
void SpotifyClient::downloadFile(String url, String filename) {
Serial.println("Downloading " + url + " and saving as " + filename);
// wait for WiFi connection
// TODO - decide if there's a different action for first call or subsequent
// calls boolean isFirstCall = true;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
// configure server and url
http.begin(*wifiClient, url);
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
if (httpCode > 0) {
// SPIFFS.remove(filename);
fs::File f = SPIFFS.open(filename, "w+");
if (!f) {
Serial.println("file open failed");
return;
}
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
// get lenght of document (is -1 when Server sends no Content-Length
// header)
int total = http.getSize();
int len = total;
// progressCallback(filename, 0,total, true);
// create buffer for read
uint8_t buff[128] = {0};
// get tcp stream
WiFiClient *stream = http.getStreamPtr();
// read all data from server
while (http.connected() && (len > 0 || len == -1)) {
// get available data size
size_t size = stream->available();
if (size) {
// read up to 128 byte
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
// write it to Serial
f.write(buff, c);
if (len > 0) {
len -= c;
}
// progressCallback(filename, total - len,total, false);
// isFirstCall = false;
executeCallback();
}
delay(1);
}
Serial.println();
Serial.print("[HTTP] connection closed or file end.\n");
}
f.close();
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}