zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.33k stars 284 forks source link

Local tracks `Track` object is empty. #192

Closed jtagcat closed 2 years ago

jtagcat commented 2 years ago

(hehe i had a panic in code!)

Spotify returns:

{
    "added_at" : "2020-07-27T12:51:13Z",
    "added_by" : {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/user/shawncrowder"
      },
      "href" : "https://api.spotify.com/v1/users/shawncrowder",
      "id" : "shawncrowder",
      "type" : "user",
      "uri" : "spotify:user:shawncrowder"
    },
    "is_local" : true,
    "track" : {
      "available_markets" : [ ],
      "disc_number" : 0,
      "duration_ms" : 307000,
      "explicit" : false,
      "external_ids" : { },
      "external_urls" : { },
      "href" : null,
      "id" : null,
      "is_local" : true,
      "name" : "Grey Street",
      "popularity" : 0,
      "preview_url" : null,
      "track_number" : 0,
      "type" : "track",
      "uri" : "spotify:local:Dave+Matthews+Band:Busted+Stuff:Grey+Street:307"
    },

Track is not populated at all: image

Expected: uri, etc is populated under t.Track.Track.SimpleTrack.

Code I tested with:

package main

import (
    "context"
    "log"
    "os"

    "github.com/zmb3/spotify/v2"
    spotifyauth "github.com/zmb3/spotify/v2/auth"
    "golang.org/x/oauth2/clientcredentials"
)

func spotifyInit(ctx context.Context) *spotify.Client {
    config := &clientcredentials.Config{
        ClientID:     os.Getenv("SPOTIFY_ID"),
        ClientSecret: os.Getenv("SPOTIFY_SECRET"),
        TokenURL:     spotifyauth.TokenURL,
    }
    token, err := config.Token(ctx)
    if err != nil {
        log.Fatalf("couldn't get token: %v", err)
    }
    httpClient := spotifyauth.New().Client(ctx, token)
    return spotify.New(httpClient)
}

func main() {
    ctx := context.Background()
    c := spotifyInit(ctx)
    var id spotify.ID = "0mi75nrf8Dy9gT3AX4Soza"

    _, err := c.GetPlaylistItems(ctx, id)
    if err != nil {
        log.Panicf("couldn't get playlist tracks for %q: %e", id, err)
    }
}
strideynet commented 2 years ago

Thanks for raising this, I'll take a look at this tomorrow. It looks like this is a bug related to local tracks, it's possible the Spotify API doesn't actually return any information for these so I will need to investigate.

strideynet commented 2 years ago

Identified and fixed a few bugs here, cutting a release shortly.