@@ -115,7 +115,7 @@ protected function getMe():void{
115115 /**
116116 * fetch the artists the user is following
117117 */
118- protected function getFollowedArtists ():void {
118+ public function getFollowedArtists ():array {
119119 $ this ->artists = [];
120120
121121 $ params = [
@@ -155,12 +155,14 @@ protected function getFollowedArtists():void{
155155 while ($ params ['after ' ] !== '' );
156156
157157 $ this ->logger ->info (sprintf ('fetched %s artists ' , count ($ this ->artists )));
158+
159+ return $ this ->artists ;
158160 }
159161
160162 /**
161163 * fetch the releases for the followed artists
162164 */
163- protected function getArtistReleases ():void {
165+ public function getArtistReleases ():array {
164166 $ this ->albums = [];
165167
166168 foreach ($ this ->artists as $ artistID => $ artist ){
@@ -191,12 +193,66 @@ protected function getArtistReleases():void{
191193 usleep (self ::sleepTimer);
192194 }
193195
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 ;
194250 }
195251
196252 /**
197253 * create a new playlist
198254 */
199- protected function createPlaylist (string $ name , string $ description ):string {
255+ public function createPlaylist (string $ name , string $ description ):string {
200256
201257 $ createPlaylist = $ this ->spotify ->request (
202258 path : sprintf ('/v1/users/%s/playlists ' , $ this ->id ),
@@ -231,7 +287,7 @@ protected function createPlaylist(string $name, string $description):string{
231287 /**
232288 * add the tracks to the given playlist
233289 */
234- protected function addTracks (string $ playlistID , array $ trackIDs ):void {
290+ public function addTracks (string $ playlistID , array $ trackIDs ):static {
235291
236292 $ uris = array_chunk (
237293 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{
260316 $ this ->logger ->warning (sprintf ('error adding tracks: http/%s ' , $ playlistAddTracks ->getStatusCode ())); // idc
261317 }
262318
319+ return $ this ;
263320 }
264321
265322}
0 commit comments