From 3dc281a8b863600dc19e65e3716e5dbc02a7f520 Mon Sep 17 00:00:00 2001 From: smiley Date: Thu, 31 Aug 2023 01:55:58 +0200 Subject: [PATCH] :sparkles: +playlist diff example --- examples/Providers/Spotify/SpotifyClient.php | 65 ++++++++++++++++++-- examples/Providers/Spotify/playlist-diff.php | 46 ++++++++++++++ 2 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 examples/Providers/Spotify/playlist-diff.php diff --git a/examples/Providers/Spotify/SpotifyClient.php b/examples/Providers/Spotify/SpotifyClient.php index 97f5bbd..5214376 100644 --- a/examples/Providers/Spotify/SpotifyClient.php +++ b/examples/Providers/Spotify/SpotifyClient.php @@ -115,7 +115,7 @@ protected function getMe():void{ /** * fetch the artists the user is following */ - protected function getFollowedArtists():void{ + public function getFollowedArtists():array{ $this->artists = []; $params = [ @@ -155,12 +155,14 @@ protected function getFollowedArtists():void{ while($params['after'] !== ''); $this->logger->info(sprintf('fetched %s artists', count($this->artists))); + + return $this->artists; } /** * fetch the releases for the followed artists */ - protected function getArtistReleases():void{ + public function getArtistReleases():array{ $this->albums = []; foreach($this->artists as $artistID => $artist){ @@ -191,12 +193,66 @@ protected function getArtistReleases():void{ usleep(self::sleepTimer); } + return $this->albums; + } + + /** + * get the tracks from the given playlist + */ + public function getPlaylist(string $playlistID):array{ + + $params = [ + 'fields' => 'total,limit,offset,items(track(id,name,album(id,name),artists(id,name)))', + 'market' => $this->market, + 'offset' => 0, + 'limit' => 100, + ]; + + $playlist = []; + $retry = 0; + + do{ + $response = $this->spotify->request(sprintf('/v1/playlists/%s/tracks', $playlistID), $params); + + if($retry > 3){ + throw new RuntimeException('error while retrieving playlist'); + } + + if($response->getStatusCode() !== 200){ + $this->logger->warning(sprintf('playlist endpoint http/%s', $response->getStatusCode())); + + $retry++; + + continue; + } + + $json = MessageUtil::decodeJSON($response); + + if(!isset($json->items)){ + $this->logger->warning('empty playlist response'); + + $retry++; + + continue; + } + + foreach($json->items as $item){ + $playlist[$item->track->id] = $item->track; + } + + $params['offset'] += 100; + $retry = 0; + + } + while($params['offset'] <= $json->total); + + return $playlist; } /** * create a new playlist */ - protected function createPlaylist(string $name, string $description):string{ + public function createPlaylist(string $name, string $description):string{ $createPlaylist = $this->spotify->request( path : sprintf('/v1/users/%s/playlists', $this->id), @@ -231,7 +287,7 @@ protected function createPlaylist(string $name, string $description):string{ /** * add the tracks to the given playlist */ - protected function addTracks(string $playlistID, array $trackIDs):void{ + public function addTracks(string $playlistID, array $trackIDs):static{ $uris = array_chunk( 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{ $this->logger->warning(sprintf('error adding tracks: http/%s', $playlistAddTracks->getStatusCode())); // idc } + return $this; } } diff --git a/examples/Providers/Spotify/playlist-diff.php b/examples/Providers/Spotify/playlist-diff.php new file mode 100644 index 0000000..010013e --- /dev/null +++ b/examples/Providers/Spotify/playlist-diff.php @@ -0,0 +1,46 @@ + + * @copyright 2023 smiley + * @license MIT + */ + +namespace chillerlan\OAuthExamples\Providers\Spotify; + +use function array_diff; +use function array_keys; +use function sprintf; + +/** + * @var \chillerlan\OAuth\Providers\Spotify $spotify + * @var \Psr\Http\Message\RequestFactoryInterface $requestFactory + * @var \Psr\Log\LoggerInterface $logger + * @var string $CFGDIR + */ +require_once __DIR__.'/spotify-common.php'; + +$client = new class ($spotify, $logger) extends SpotifyClient{ + + public function playlistDiff(string $playlistID1, string $playlistID2, bool $createAsPlaylist = false):array{ + $p1 = array_keys($this->getPlaylist($playlistID1)); + $p2 = array_keys($this->getPlaylist($playlistID2)); + + $diff = array_diff($p1, $p2); + + if($createAsPlaylist){ + $playlistID = $this->createPlaylist( + 'playlist diff', + sprintf('diff between playlists "spotify:playlist:%s" and "spotify:playlist:%s"', $playlistID1, $playlistID2) + ); + $this->addTracks($playlistID, $diff); + } + + return $diff; + } + +}; + +$client->playlistDiff('37i9dQZF1DX4UtSsGT1Sbe', '37i9dQZF1DXb57FjYWz00c', true);