-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SpotifyBridge.php
371 lines (324 loc) · 11.6 KB
/
SpotifyBridge.php
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
<?php
class SpotifyBridge extends BridgeAbstract
{
const NAME = 'Spotify';
const URI = 'https://spotify.com/';
const DESCRIPTION = 'Fetches the latest items from one or more artists, playlists or podcasts';
const MAINTAINER = 'Paroleen';
const CACHE_TIMEOUT = 3600;
const PARAMETERS = [
'By Spotify URIs' => [
'clientid' => [
'name' => 'Client ID',
'type' => 'text',
'required' => true
],
'clientsecret' => [
'name' => 'Client secret',
'type' => 'text',
'required' => true
],
'country' => [
'name' => 'Country/Market',
'type' => 'text',
'required' => false,
'exampleValue' => 'US',
'defaultValue' => 'US'
],
'limit' => [
'name' => 'Limit',
'type' => 'number',
'required' => false,
'exampleValue' => 10,
'defaultValue' => 10
],
'spotifyuri' => [
'name' => 'Spotify URIs',
'type' => 'text',
'required' => true,
// spotify:playlist:37i9dQZF1DXcBWIGoYBM5M
// spotify:show:6ShFMYxeDNMo15COLObDvC
'exampleValue' => 'spotify:artist:4lianjyuR1tqf6oUX8kjrZ',
],
'albumtype' => [
'name' => 'Album type',
'type' => 'text',
'required' => false,
'exampleValue' => 'album,single,appears_on,compilation',
'defaultValue' => 'album,single'
]
],
'By Spotify Search' => [
'clientid' => [
'name' => 'Client ID',
'type' => 'text',
'required' => true
],
'clientsecret' => [
'name' => 'Client secret',
'type' => 'text',
'required' => true
],
'market' => [
'name' => 'Market',
'type' => 'text',
'required' => false,
'exampleValue' => 'US',
'defaultValue' => 'US'
],
'limit' => [
'name' => 'Limit',
'type' => 'number',
'required' => false,
'exampleValue' => 10,
'defaultValue' => 10
],
'query' => [
'name' => 'Search query',
'type' => 'text',
'required' => true,
'exampleValue' => 'artist:The Beatles',
],
'type' => [
'name' => 'Type',
'type' => 'text',
'required' => true,
'exampleValue' => 'album,episode',
'defaultValue' => 'album,episode'
]
],
];
private $uri = '';
private $name = '';
private $token = '';
public function collectData()
{
/**
* https://developer.spotify.com/documentation/web-api/concepts/rate-limits
*/
$cacheKey = 'spotify_rate_limit';
try {
$this->collectDataInternal();
} catch (HttpException $e) {
if ($e->getCode() === 429) {
$retryAfter = $e->response->getHeader('Retry-After') ?? (60 * 5);
$this->cache->set($cacheKey, true, $retryAfter);
throw new RateLimitException(sprintf('Rate limited by spotify, try again in %s seconds', $retryAfter));
}
throw $e;
}
}
private function collectDataInternal()
{
$this->fetchAccessToken();
if ($this->queriedContext === 'By Spotify URIs') {
$entries = $this->getEntriesFromURIs();
} else {
$entries = $this->getEntriesFromQuery();
}
usort($entries, function ($entry1, $entry2) {
return $this->getDate($entry2) <=> $this->getDate($entry1);
});
foreach ($entries as $entry) {
if (! isset($entry['type'])) {
$item = $this->getTrackData($entry);
} elseif ($entry['type'] === 'album') {
$item = $this->getAlbumData($entry);
} elseif ($entry['type'] === 'episode') {
$item = $this->getEpisodeData($entry);
} else {
throw new \Exception('Spotify URI not supported');
}
$this->items[] = $item;
if ($this->getInput('limit') > 0 && count($this->items) >= $this->getInput('limit')) {
break;
}
}
}
private function fetchAccessToken()
{
$cacheKey = sprintf('SpotifyBridge:%s:%s', $this->getInput('clientid'), $this->getInput('clientsecret'));
$token = $this->cache->get($cacheKey);
if ($token) {
$this->token = $token;
} else {
$basicAuth = base64_encode(sprintf('%s:%s', $this->getInput('clientid'), $this->getInput('clientsecret')));
$json = getContents('https://accounts.spotify.com/api/token', [
"Authorization: Basic $basicAuth",
], [
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
]);
$data = Json::decode($json);
$this->token = $data['access_token'];
$this->cache->set($cacheKey, $this->token, 3600);
}
}
private function getEntriesFromQuery()
{
$entries = [];
$types = [
'albums',
'episodes',
];
$query = [
'q' => $this->getInput('query'),
'type' => $this->getInput('type'),
'market' => $this->getInput('market'),
'limit' => 50,
];
$hasItems = true;
$offset = 0;
while ($hasItems && $offset < 1000) {
$hasItems = false;
$query['offset'] = $offset;
$json = getContents('https://api.spotify.com/v1/search?' . http_build_query($query), ['Authorization: Bearer ' . $this->token]);
$partial = Json::decode($json);
foreach ($types as $type) {
if (isset($partial[$type]['items'])) {
$entries = array_merge($entries, $partial[$type]['items']);
$hasItems = true;
}
}
$offset += 50;
}
return $entries;
}
private function getEntriesFromURIs()
{
$entries = [];
$uris = explode(',', $this->getInput('spotifyuri'));
foreach ($uris as $uri) {
$type = explode(':', $uri)[1];
$spotifyId = explode(':', $uri)[2];
$types = [
'artist' => 'album',
'playlist' => 'track',
'show' => 'episode',
];
if (!isset($types[$type])) {
throw new \Exception(sprintf('Unsupported Spotify URI: %s', $uri));
}
$entry_type = $types[$type];
$url = 'https://api.spotify.com/v1/' . $type . 's/' . $spotifyId . '/' . $entry_type . 's';
$query = [
'limit' => 50,
];
if ($type === 'artist') {
$query['country'] = $this->getInput('country');
$query['include_groups'] = $this->getInput('albumtype');
} else {
$query['market'] = $this->getInput('country');
}
$offset = 0;
while (true) {
$query['offset'] = $offset;
$json = getContents($url . '?' . http_build_query($query), ['Authorization: Bearer ' . $this->token]);
$partial = Json::decode($json);
if (empty($partial['items'])) {
break;
}
$entries = array_merge($entries, $partial['items']);
$offset += 50;
}
}
return $entries;
}
private function getAlbumData($album)
{
$item = [];
$item['title'] = $album['name'];
$item['uri'] = $album['external_urls']['spotify'];
$item['timestamp'] = $this->getDate($album);
$item['author'] = $album['artists'][0]['name'];
$item['categories'] = [$album['album_type']];
$item['content'] = '<img style="width: 256px" src="' . $album['images'][0]['url'] . '">';
if ($album['total_tracks'] > 1) {
$item['content'] .= '<p>Total tracks: ' . $album['total_tracks'] . '</p>';
}
return $item;
}
private function getTrackData($track)
{
$item = [];
$item['title'] = $track['track']['name'];
$item['uri'] = $track['track']['external_urls']['spotify'];
$item['timestamp'] = $this->getDate($track);
$item['author'] = $track['track']['artists'][0]['name'];
$item['categories'] = ['track'];
$item['content'] = '<img style="width: 256px" src="' . $track['track']['album']['images'][0]['url'] . '">';
return $item;
}
private function getEpisodeData($episode)
{
$item = [];
$item['title'] = $episode['name'];
$item['uri'] = $episode['external_urls']['spotify'];
$item['timestamp'] = $this->getDate($episode);
$item['content'] = '<img style="width: 256px" src="' . $episode['images'][0]['url'] . '">';
if (isset($episode['description'])) {
$item['content'] = $item['content'] . '<p>' . $episode['description'] . '</p>';
}
if (isset($episode['audio_preview_url'])) {
$item['content'] = $item['content'] . '<audio controls src="' . $episode['audio_preview_url'] . '"></audio>';
}
return $item;
}
private function getDate($entry)
{
if (isset($entry['type'])) {
$type = 'release_date';
} else {
$type = 'added_at';
}
$date = $entry[$type];
if (strlen($date) == 4) {
$date .= '-01-01';
} elseif (strlen($date) == 7) {
$date .= '-01';
}
if (strlen($date) > 10) {
return DateTime::createFromFormat('Y-m-d\TH:i:s\Z', $date)->getTimestamp();
}
return DateTime::createFromFormat('Y-m-d', $date)->getTimestamp();
}
public function getURI()
{
if (empty($this->uri)) {
$this->getFirstEntry();
}
return $this->uri;
}
public function getName()
{
if (empty($this->name)) {
$this->getFirstEntry();
}
return $this->name;
}
private function getFirstEntry()
{
$spotifyUri = $this->getInput('spotifyuri');
if (!is_null($spotifyUri) && strpos($spotifyUri, ',') === false) {
$uris = explode(',', $spotifyUri);
$firstUri = $uris[0];
$type = explode(':', $firstUri)[1];
$spotifyId = explode(':', $firstUri)[2];
$uri = 'https://api.spotify.com/v1/' . $type . 's/' . $spotifyId;
$query = [];
if ($type === 'show') {
$query['market'] = $this->getInput('country');
}
$json = getContents($uri . '?' . http_build_query($query), ['Authorization: Bearer ' . $this->token]);
$item = Json::decode($json);
$this->uri = $item['external_urls']['spotify'];
$this->name = $item['name'] . ' - Spotify';
} else {
$this->uri = parent::getURI();
$this->name = parent::getName();
}
}
public function getIcon()
{
return 'https://www.scdn.co/i/_global/favicon.png';
}
}