@@ -115,7 +115,7 @@ protected function getMe():void{
115
115
/**
116
116
* fetch the artists the user is following
117
117
*/
118
- protected function getFollowedArtists ():void {
118
+ public function getFollowedArtists ():array {
119
119
$ this ->artists = [];
120
120
121
121
$ params = [
@@ -155,12 +155,14 @@ protected function getFollowedArtists():void{
155
155
while ($ params ['after ' ] !== '' );
156
156
157
157
$ this ->logger ->info (sprintf ('fetched %s artists ' , count ($ this ->artists )));
158
+
159
+ return $ this ->artists ;
158
160
}
159
161
160
162
/**
161
163
* fetch the releases for the followed artists
162
164
*/
163
- protected function getArtistReleases ():void {
165
+ public function getArtistReleases ():array {
164
166
$ this ->albums = [];
165
167
166
168
foreach ($ this ->artists as $ artistID => $ artist ){
@@ -191,12 +193,66 @@ protected function getArtistReleases():void{
191
193
usleep (self ::sleepTimer);
192
194
}
193
195
196
+ return $ this ->albums ;
197
+ }
198
+
199
+ /**
200
+ * get the tracks from the given playlist
201
+ */
202
+ public function getPlaylist (string $ playlistID ):array {
203
+
204
+ $ params = [
205
+ 'fields ' => 'total,limit,offset,items(track(id,name,album(id,name),artists(id,name))) ' ,
206
+ 'market ' => $ this ->market ,
207
+ 'offset ' => 0 ,
208
+ 'limit ' => 100 ,
209
+ ];
210
+
211
+ $ playlist = [];
212
+ $ retry = 0 ;
213
+
214
+ do {
215
+ $ response = $ this ->spotify ->request (sprintf ('/v1/playlists/%s/tracks ' , $ playlistID ), $ params );
216
+
217
+ if ($ retry > 3 ){
218
+ throw new RuntimeException ('error while retrieving playlist ' );
219
+ }
220
+
221
+ if ($ response ->getStatusCode () !== 200 ){
222
+ $ this ->logger ->warning (sprintf ('playlist endpoint http/%s ' , $ response ->getStatusCode ()));
223
+
224
+ $ retry ++;
225
+
226
+ continue ;
227
+ }
228
+
229
+ $ json = MessageUtil::decodeJSON ($ response );
230
+
231
+ if (!isset ($ json ->items )){
232
+ $ this ->logger ->warning ('empty playlist response ' );
233
+
234
+ $ retry ++;
235
+
236
+ continue ;
237
+ }
238
+
239
+ foreach ($ json ->items as $ item ){
240
+ $ playlist [$ item ->track ->id ] = $ item ->track ;
241
+ }
242
+
243
+ $ params ['offset ' ] += 100 ;
244
+ $ retry = 0 ;
245
+
246
+ }
247
+ while ($ params ['offset ' ] <= $ json ->total );
248
+
249
+ return $ playlist ;
194
250
}
195
251
196
252
/**
197
253
* create a new playlist
198
254
*/
199
- protected function createPlaylist (string $ name , string $ description ):string {
255
+ public function createPlaylist (string $ name , string $ description ):string {
200
256
201
257
$ createPlaylist = $ this ->spotify ->request (
202
258
path : sprintf ('/v1/users/%s/playlists ' , $ this ->id ),
@@ -231,7 +287,7 @@ protected function createPlaylist(string $name, string $description):string{
231
287
/**
232
288
* add the tracks to the given playlist
233
289
*/
234
- protected function addTracks (string $ playlistID , array $ trackIDs ):void {
290
+ public function addTracks (string $ playlistID , array $ trackIDs ):static {
235
291
236
292
$ uris = array_chunk (
237
293
array_map (fn (string $ t ):string => 'spotify:track: ' .$ t , array_values ($ trackIDs )), // why not just ids???
@@ -260,6 +316,7 @@ protected function addTracks(string $playlistID, array $trackIDs):void{
260
316
$ this ->logger ->warning (sprintf ('error adding tracks: http/%s ' , $ playlistAddTracks ->getStatusCode ())); // idc
261
317
}
262
318
319
+ return $ this ;
263
320
}
264
321
265
322
}
0 commit comments