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

Commit 3dc281a

Browse files
committed
✨ +playlist diff example
1 parent 4d9dfb5 commit 3dc281a

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

examples/Providers/Spotify/SpotifyClient.php

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* playlist-diff.php
4+
*
5+
* @created 31.08.2023
6+
* @author smiley <[email protected]>
7+
* @copyright 2023 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\OAuthExamples\Providers\Spotify;
12+
13+
use function array_diff;
14+
use function array_keys;
15+
use function sprintf;
16+
17+
/**
18+
* @var \chillerlan\OAuth\Providers\Spotify $spotify
19+
* @var \Psr\Http\Message\RequestFactoryInterface $requestFactory
20+
* @var \Psr\Log\LoggerInterface $logger
21+
* @var string $CFGDIR
22+
*/
23+
require_once __DIR__.'/spotify-common.php';
24+
25+
$client = new class ($spotify, $logger) extends SpotifyClient{
26+
27+
public function playlistDiff(string $playlistID1, string $playlistID2, bool $createAsPlaylist = false):array{
28+
$p1 = array_keys($this->getPlaylist($playlistID1));
29+
$p2 = array_keys($this->getPlaylist($playlistID2));
30+
31+
$diff = array_diff($p1, $p2);
32+
33+
if($createAsPlaylist){
34+
$playlistID = $this->createPlaylist(
35+
'playlist diff',
36+
sprintf('diff between playlists "spotify:playlist:%s" and "spotify:playlist:%s"', $playlistID1, $playlistID2)
37+
);
38+
$this->addTracks($playlistID, $diff);
39+
}
40+
41+
return $diff;
42+
}
43+
44+
};
45+
46+
$client->playlistDiff('37i9dQZF1DX4UtSsGT1Sbe', '37i9dQZF1DXb57FjYWz00c', true);

0 commit comments

Comments
 (0)