Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
✨ +playlist diff example
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Aug 30, 2023
1 parent 4d9dfb5 commit 3dc281a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
65 changes: 61 additions & 4 deletions examples/Providers/Spotify/SpotifyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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???
Expand Down Expand Up @@ -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;
}

}
46 changes: 46 additions & 0 deletions examples/Providers/Spotify/playlist-diff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* playlist-diff.php
*
* @created 31.08.2023
* @author smiley <[email protected]>
* @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);

0 comments on commit 3dc281a

Please sign in to comment.