Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Spotify playlists #101

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a "known source" for these hashes @duncan3dc? It seems like a whole host of different ones work for playlists;

0004006c
0006006c
10050024
10060a6c

The playlist hash I used here is from this issue I found, however the Sonos desktop application seems to use 10060a6c with my Sonos 🤔.

Copy link
Owner

@duncan3dc duncan3dc Mar 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No they're complete black magic. For Sonos resources (local media library) they're pretty reliable, but as soon as you move to streaming services there's a range in use with no obvious pattern (as you've discovered).

I've made several unsuccessful attempts to figure this out in the past to support Spotify albums. Last time I checked none of the libraries for other languages handled this successfully/reliably, but you might wanna check out their recent versions to see if they've cracked it.

Alternatively I've started a new library (sonos-cloud) which uses the recently published public API for Sonos. This is obviously a lot more reliable than reverse engineering their SOAP calls, but I've not built much with it yet to know if it supports your use case here


/**
* Create a mode array from the mode text value.
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:";
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 . Helper::PLAYLIST_HASH . urlencode($uri);
Copy link
Author

@jackwilsdon jackwilsdon Mar 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oddly enough the playlist hash is actually required as part of EnqueuedURI, which seems to be different when compared to the other sources (which only need it as part of the item ID in EnqueuedURIMetaData).

}

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($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