-
Notifications
You must be signed in to change notification settings - Fork 29
/
SpotifyClient.h
185 lines (153 loc) · 5.74 KB
/
SpotifyClient.h
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
/**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.
*/
#pragma once
#include <Arduino.h>
#include <JsonListener.h>
#include <JsonStreamingParser.h>
#include <ESP8266WebServer.h>
#include <WiFiClientSecure.h>
#include <FS.h>
#include <base64.h>
typedef void (*DrawingCallback)();
typedef struct SpotifyAuth {
// "access_token": "XXX"
String accessToken;
// "token_type":"Bearer",
String tokenType;
// "expires_in":3600,
uint16_t expiresIn;
// "refresh_token":"XX",
String refreshToken;
// "scope":"user-modify-playback-state user-read-playback-state user-read-currently-playing user-read-private
String scope;
} SpotifyAuth;
typedef struct SpotifyData {
// timestamp = 1527690461156
// progress_ms = 37516
uint32_t progressMs;
// is_playing = true
boolean isPlaying;
boolean isPlayerActive;
// Starting new object: item
// Starting new object: album
// album_type = album
// Starting new object: artists
// Starting new object: external_urls
// spotify = https://open.spotify.com/artist/0n94vC3S9c3mb2HyNAOcjg
// href = https://api.spotify.com/v1/artists/0n94vC3S9c3mb2HyNAOcjg
// id = 0n94vC3S9c3mb2HyNAOcjg
// name = The Head and the Heart item.album.artists.name
String artistName;
// type = artist
// uri = spotify:artist:0n94vC3S9c3mb2HyNAOcjg
// Starting new object: external_urls
// spotify = https://open.spotify.com/album/0xWfhCMYmaiCXtLOuyPoLF
// href = https://api.spotify.com/v1/albums/0xWfhCMYmaiCXtLOuyPoLF
String image640Href;
String image300Href;
String image64Href;
// id = 0xWfhCMYmaiCXtLOuyPoLF
// Starting new object: images
// height = 639
// url = https://i.scdn.co/image/c08c25d478dc3a645c04abc000602201ce1634d8
// width = 630
// Starting new object: width
// height = 300
// url = https://i.scdn.co/image/6b0e2580ede853b1173198693301f0df1979b5cf
// width = 296
// Starting new object: width
// height = 64
// url = https://i.scdn.co/image/7d20358db1b1502a09a05f860ba9d3641e552f8d
// width = 63
// name = The Head and the Heart
// release_date = 2010
// release_date_precision = year
// type = album
// uri = spotify:album:0xWfhCMYmaiCXtLOuyPoLF
// Starting new object: artists
// Starting new object: external_urls
// spotify = https://open.spotify.com/artist/0n94vC3S9c3mb2HyNAOcjg
// href = https://api.spotify.com/v1/artists/0n94vC3S9c3mb2HyNAOcjg
// id = 0n94vC3S9c3mb2HyNAOcjg
// name = The Head and the Heart
// type = artist
// uri = spotify:artist:0n94vC3S9c3mb2HyNAOcjg
// disc_number = 1
// duration_ms = 259120
uint32_t durationMs;
// explicit = false
// Starting new object: external_ids
// isrc = USSUB1191507
// Starting new object: external_urls
// spotify = https://open.spotify.com/track/3gvAGvbMCRvVDDp8ZaIPV5
// href = https://api.spotify.com/v1/tracks/3gvAGvbMCRvVDDp8ZaIPV5
// id = 3gvAGvbMCRvVDDp8ZaIPV5
// is_local = false
// name = Lost in My Mind
String title;
// popularity = 68
// preview_url = null
// track_number = 7
// type = track
// uri = spotify:track:3gvAGvbMCRvVDDp8ZaIPV5
// Starting new object: context
// Starting new object: external_urls
// spotify = https://open.spotify.com/user/spotify/playlist/37i9dQZF1DWUNIrSzKgQbP
// href = https://api.spotify.com/v1/users/spotify/playlists/37i9dQZF1DWUNIrSzKgQbP
// type = playlist
// uri = spotify:user:spotify:playlist:37i9dQZF1DWUNIrSzKgQbP
} SpotifyData;
class SpotifyClient: public JsonListener {
private:
String currentKey;
String currentParent;
SpotifyData *data;
SpotifyAuth *auth;
bool isDataCall;
String rootPath[10];
uint8_t level = 0;
uint16_t currentImageHeight;
DrawingCallback *drawingCallback;
String clientId;
String clientSecret;
String redirectUri;
WiFiClientSecure *wifiClient;
ESP8266WebServer server;
String getRootPath();
void executeCallback();
public:
SpotifyClient(String clientId, String clientSecret, String redirectUri, WiFiClientSecure *wifiClient);
uint16_t update(SpotifyData *data, SpotifyAuth *auth);
uint16_t playerCommand(SpotifyAuth *auth, String method, String command);
void getToken(SpotifyAuth *auth, String grantType, String code);
void downloadFile(String url, String filename);
void setDrawingCallback(DrawingCallback *drawingCallback) {
this->drawingCallback = drawingCallback;
}
String startConfigPortal(const String mDnsName);
virtual void whitespace(char c);
virtual void startDocument();
virtual void key(String key);
virtual void value(String value);
virtual void endArray();
virtual void endObject();
virtual void endDocument();
virtual void startArray();
virtual void startObject();
};