Stuck working with the Spotify API

Hi,

I’m trying to retrieve the URL for a track from Spotify. I can construct the URL I need, but I am stuck with how to actually use it.

Here is my template but how do I actually make use of it? Stumped!

curl --request GET --url 'https://api.spotify.com/v1/search?q={{ states.media_player.office_media.attributes.media_artist|default('')|replace(' ','%20') }}%2C%20{{ states.media_player.office_media.attributes.media_title |default('')|replace(' ','%20') |regex_replace('\([0-9]+\)')}}&type=artist%2Ctrack&limit=1' --header 'application/json' --header 'Accept: application/json' --header 'Authorization: Bearer {{ state_attr('sensor.spotify_token','access_token') }}' 

Because the URL changes for every song I can’t seem to make it a command_line or rest function. Maybe I’m overthinking it?

OK, I have got a rest sensor working (not my preferred option but…

Anyway … I am now struggling to parse the result. I want the path - tracks,items,artists,external_urls,spotify but no matter what I try I cannot seem to get the URL in my sensor :frowning:

Here is the json in case anyone has any ideas for me please

{
  "tracks" : {
    "href" : "https://api.spotify.com/v1/search?query=Bonded+By+Blood%2C+Immortal+Life&type=track&offset=0&limit=1",
    "items" : [ {
      "album" : {
        "album_type" : "compilation",
        "artists" : [ {
          "external_urls" : {
            "spotify" : "https://open.spotify.com/artist/0LyfQWJT6nXafLPZqxe9Of"
          },
          "href" : "https://api.spotify.com/v1/artists/0LyfQWJT6nXafLPZqxe9Of",
          "id" : "0LyfQWJT6nXafLPZqxe9Of",
          "name" : "Various Artists",
          "type" : "artist",
          "uri" : "spotify:artist:0LyfQWJT6nXafLPZqxe9Of"
        } ],
        "available_markets" : [ "AD", "AE", "AG", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BN", "BO", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CD", "CG", "CH", "CI", "CL", "CM", "CO",                                  "CR", "CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "ET", "FI", "FJ", "FM", "FR", "GA", "GB", "GD", "GE", "GH", "GM", "GN", "GQ", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL",                                  "IN", "IQ", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MN", "MO", "MR", "MT", "MU", "                                 MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PK", "PL", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "RW", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "S                                 T", "SV", "SZ", "TD", "TG", "TH", "TJ", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "VU", "WS", "XK", "ZA", "ZM", "ZW" ],
        "external_urls" : {
          "spotify" : "https://open.spotify.com/album/3Hew9i4xQriccSpvf4D6zd"
        },
        "href" : "https://api.spotify.com/v1/albums/3Hew9i4xQriccSpvf4D6zd",
        "id" : "3Hew9i4xQriccSpvf4D6zd",
        "images" : [ {
          "height" : 640,
          "url" : "https://i.scdn.co/image/ab67616d0000b27314cce1317eb0d790bc3496b9",
          "width" : 640
        }, {
          "height" : 300,
          "url" : "https://i.scdn.co/image/ab67616d00001e0214cce1317eb0d790bc3496b9",
          "width" : 300
        }, {
          "height" : 64,
          "url" : "https://i.scdn.co/image/ab67616d0000485114cce1317eb0d790bc3496b9",
          "width" : 64
        } ],
        "name" : "Thrashing Like A Maniac",
        "release_date" : "2008",
        "release_date_precision" : "year",
        "total_tracks" : 16,
        "type" : "album",
        "uri" : "spotify:album:3Hew9i4xQriccSpvf4D6zd"
      },
      "artists" : [ {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/3csFpGsY4rQQucEXwU0vaS"
        },
        "href" : "https://api.spotify.com/v1/artists/3csFpGsY4rQQucEXwU0vaS",
        "id" : "3csFpGsY4rQQucEXwU0vaS",
        "name" : "Bonded By Blood",
        "type" : "artist",
        "uri" : "spotify:artist:3csFpGsY4rQQucEXwU0vaS"
      } ],
      "available_markets" : [ "AD", "AE", "AG", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BN", "BO", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CD", "CG", "CH", "CI", "CL", "CM", "CO", "                                 CR", "CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "ET", "FI", "FJ", "FM", "FR", "GA", "GB", "GD", "GE", "GH", "GM", "GN", "GQ", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "I                                 N", "IQ", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MN", "MO", "MR", "MT", "MU", "MV                                 ", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PK", "PL", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "RW", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "ST"                                 , "SV", "SZ", "TD", "TG", "TH", "TJ", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "VU", "WS", "XK", "ZA", "ZM", "ZW" ],
      "disc_number" : 1,
      "duration_ms" : 175466,
      "explicit" : false,
      "external_ids" : {
        "isrc" : "GBBPB0703227"
      },
      "external_urls" : {
        "spotify" : "https://open.spotify.com/track/0Baa8BiycvDmQicvq8eVar"
      },
      "href" : "https://api.spotify.com/v1/tracks/0Baa8BiycvDmQicvq8eVar",
      "id" : "0Baa8BiycvDmQicvq8eVar",
      "is_local" : false,
      "name" : "Immortal Life",
      "popularity" : 29,
      "preview_url" : "https://p.scdn.co/mp3-preview/ac43ee69491c75d4aa46e61a83dcbc8f03357347?cid=19d3333b5c564c858a6a26fc402f6a2c",
      "track_number" : 1,
      "type" : "track",
      "uri" : "spotify:track:0Baa8BiycvDmQicvq8eVar"
    } ],
    "limit" : 1,
    "next" : "https://api.spotify.com/v1/search?query=Bonded+By+Blood%2C+Immortal+Life&type=track&offset=1&limit=1",
    "offset" : 0,
    "previous" : null,
    "total" : 7
  }
}

I figured it out. Discovered the correct way to use json_attribute_path.

Hi Neil,
Can I ask you if you can explain how I can extract the spotify info from spotify API on Home Assistant?
I need to extract this part of your json

        "href" : "https://api.spotify.com/v1/albums/3Hew9i4xQriccSpvf4D6zd",
        "id" : "3Hew9i4xQriccSpvf4D6zd",
        "images" : [ {
          "height" : 640,
          "url" : "https://i.scdn.co/image/ab67616d0000b27314cce1317eb0d790bc3496b9",
          "width" : 640

and send the “url” trough mqtt to other devices.
I hope that you have time to explain.
Thank you in advance.

I can post my solution and hopefully you can see what I did :

rest:
   - resource_template: https://api.spotify.com/v1/search?q={{ states('sensor.media_title_office')|default('')|replace(' ','%20')|replace("'",'%27') |replace(':','%3A') }}%2C%20{{ states('sensor.media_artist_office') |default('')|replace(' ','%20')|replace("'",'%27')|replace(':','%3A')|regex_replace('\([0-9]+\)')}}&type=track%2Cartist&limit=1
     headers:
       Authorization: Bearer {{ state_attr("sensor.spotify_token","access_token") }}
       Content-Type: application/json
       Accept: application/json
     scan_interval: 1800
     sensor:
       - name: spotify_url_rest
         unique_id: sur
         value_template: 'OK'
         json_attributes_path: "$.tracks.items[0].external_urls"
         json_attributes:
           - spotify
       - name: spotify_track_rest
         unique_id: str
         value_template: 'OK'
         json_attributes_path: "$.tracks.items[0]"
         json_attributes:
           - name
       - name: spotify_artist_rest
         unique_id: sar
         value_template: 'OK'
         json_attributes_path: "$.tracks.items[0].artists[0]"
         json_attributes:
           - name
       - name: spotify_album_rest
         unique_id: sal
         value_template: 'OK'
         json_attributes_path: "$.tracks.items[0].album"
         json_attributes:
           - name

Also use this tool to get the attributes path : https://jsonpathfinder.com/

2 Likes

Really thanks!
I’m sure that it can be useful for me.

Have a nice day!

1 Like

@Neil_Brownlee @Kroko

I have developed a new SpotifyPlus Home Assistant custom component integration that expands the built-in Spotify capabilities. It’s a simple install to the “custom_components” directory via a HACS UI custom repository installer.

It has built-in services to search Spotify, as well as retrieve information about the various media types. For example:

service: spotifyplus.search_playlists
data:
  entity_id: media_player.spotifyplus_john_s
  criteria: MercyMe
  include_external: audio
  limit_total: 75

Hope it helps!

1 Like

As a spotify lover I really want to say that thankyou so much for these helpful links it help me alot

1 Like

@hooram95
You’re welcome; glad you are enjoying the integration.

FYI … There is also a SpotifyPlus Card user-interface that allows you to control the player, manage your Spotify favorites, as well as search the Spotify catalog. You have to install the SpotifyPlus integration first though, and get it configured before you can use the SpotifyPlus Card.

Hope it helps!

1 Like

Thanks for sharing. This helps me

1 Like