Skip to content

Commit

Permalink
Add support for Spotify playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwilsdon committed Mar 19, 2019
1 parent e62250f commit df8dd1c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Helper
{
const TRACK_HASH = "00032020";
const ALBUM_HASH = "0004206c";
const PLAYLIST_HASH = "0006006c";

/**
* Create a mode array from the mode text value.
Expand Down
2 changes: 1 addition & 1 deletion src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected function addUris(array $tracks, int $position = null)

$position += $numberOfTracks;

if ($data["NumTracksAdded"] != $numberOfTracks) {
if ($data["NumTracksAdded"] < $numberOfTracks) {
throw new SonosException("Failed to add all the tracks");
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Tracks/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function guessTrackClass(string $uri): string
{
$classes = [
Spotify::class,
SpotifyPlaylist::class,
Google::class,
GoogleUnlimited::class,
Deezer::class,
Expand Down
51 changes: 51 additions & 0 deletions src/Tracks/SpotifyPlaylist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace duncan3dc\Sonos\Tracks;

use duncan3dc\Sonos\Helper;

/**
* Representation of a Spotify playlist.
*/
class SpotifyPlaylist extends Track
{
const PREFIX = "x-rincon-cpcontainer:" . Helper::PLAYLIST_HASH;
const REGION_EU = "2311";
const REGION_US = "3079";

/**
* @var string $region The region code for the Spotify service (the default is EU).
*/
public static $region = self::REGION_EU;


/**
* Create a Spotify playlist object.
*
* @param string $uri The URI of the playlist or the full Spotify ID of the playlist
*/
public function __construct(string $uri)
{
# If this is a spotify playlist ID and not a URI then convert it to a URI now
if (substr($uri, 0, strlen(self::PREFIX)) !== self::PREFIX) {
$uri = self::PREFIX . urlencode($uri);
}

parent::__construct($uri);
}

/**
* Get the metadata xml for this playlist.
*
* @return string
*/
public function getMetaData(): string
{
$uri = substr($this->getUri(), strlen(self::PREFIX));

return Helper::createMetaDataXml(Helper::PLAYLIST_HASH . "{$uri}", "-1", [
"dc:title" => "",
"upnp:class" => "object.container.playlistContainer",
], static::$region);
}
}
16 changes: 16 additions & 0 deletions tests/Tracks/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use duncan3dc\Sonos\Tracks\Google;
use duncan3dc\Sonos\Tracks\GoogleUnlimited;
use duncan3dc\Sonos\Tracks\Spotify;
use duncan3dc\Sonos\Tracks\SpotifyPlaylist;
use duncan3dc\Sonos\Tracks\Stream;
use duncan3dc\Sonos\Tracks\Track;
use duncan3dc\SonosTests\MockTest;
Expand Down Expand Up @@ -44,6 +45,13 @@ public function testSpotifyTrackUri()
}


public function testSpotifyPlaylistUri()
{
$uri = "x-rincon-cpcontainer:0006006cspotify:user:123sdfd6:playlist:123sdfd6";
$track = $this->factory->createFromUri($uri);
$this->assertInstanceOf(SpotifyPlaylist::class, $track);
}

public function testDeezerTrackUri()
{
$uri = "x-sonos-http:tr:123sdfd6";
Expand Down Expand Up @@ -106,6 +114,14 @@ public function testSpotifyTrackXml()
}


public function testSpotifyPlaylistTrackXml()
{
$xml = $this->getXml("x-rincon-cpcontainer:0006006cspotify:user:123sdfd6:playlist:123sdfd6");
$track = $this->factory->createFromXml($xml);
$this->assertInstanceOf(SpotifyPlaylist::class, $track);
}


public function testDeezerTrackXml()
{
$xml = $this->getXml("x-sonos-http:tr:123sdfd6");
Expand Down

0 comments on commit df8dd1c

Please sign in to comment.