[GH-ISSUE #2] code returns error messages everytime #2

Closed
opened 2026-02-27 19:22:56 +03:00 by kerem · 0 comments
Owner

Originally created by @tim1f1 on GitHub (Mar 22, 2025).
Original GitHub issue: https://github.com/FrostBreker/spotify-private-api/issues/2

Originally assigned to: @FrostBreker on GitHub.

For a week or two now I've been getting error messages whenever I try running codes using this script e.g.

package main

import (
    "fmt"
    "log"

    spotifyprivateapi "github.com/FrostBreker/spotify-private-api"
)

func main() {
    client := spotifyprivateapi.NewClient(spotifyprivateapi.Client{
        Debug: true,
    })

    // Replace with a valid Spotify track ID
    track, err := client.FetchTrack("7sFMOSnRBa6td4lZa1oiTI")
    if err != nil {
        log.Fatal(err)
    }

    // Print the album name
    albumName := track.Album.Name

    // Print the primary artist name
    artistName := "Unknown Artist"
    if len(track.Artists) > 0 {
        artistName = track.Artists[0].Name
    }

    fmt.Println("Artist:", artistName)
    fmt.Println("Album:", albumName)
}

returns

C:\Users\Timothy\GO>go run playcount.go

command-line-arguments

.\playcount.go:22:24: track.Album undefined (type responseTypes.TrackResponse has no field or method Album)
.\playcount.go:26:18: track.Artists undefined (type responseTypes.TrackResponse has no field or method Artists)
.\playcount.go:27:28: track.Artists undefined (type responseTypes.TrackResponse has no field or method Artists)

and

package main

import (
"bufio"
"encoding/csv"
"log"
"os"
"path/filepath"
"strconv"

"github.com/FrostBreker/spotify-private-api"
)

func main() {
client := spotifyprivateapi.NewClient(spotifyprivateapi.Client{
Debug: true,
})

inputFilePath := "carly.txt" #contains list of track IDs to fetch playcount for

// Open the text file containing track IDs
file, err := os.Open(inputFilePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()

// Create the output file name by replacing the .txt extension with .csv
outputFilePath := changeExtension(inputFilePath, ".csv")

// Create a CSV file to write the data
outputFile, err := os.Create(outputFilePath)
if err != nil {
log.Fatal(err)
}
defer outputFile.Close()

// Create a CSV writer
writer := csv.NewWriter(outputFile)
defer writer.Flush()

// Write CSV header
err = writer.Write([]string{"Track ID", "Track Title", "Artist", "Album", "Release Date", "Play Count"})
if err != nil {
log.Fatal(err)
}

// Scanner to read track IDs line by line
scanner := bufio.NewScanner(file)

// Fetch play count for each track
for scanner.Scan() {
trackID := scanner.Text()

  // Fetch track information
  trackResp, err := client.FetchTrack(trackID)
  if err != nil {
  	log.Printf("Error fetching track information for track %s: %v\n", trackID, err)
  	continue
  }

  // Extract track title, artist name, album name, and release date from the response
  trackTitle := trackResp.Data.TrackUnion.Name
  artistName := trackResp.Data.TrackUnion.FirstArtist.Items[0].Profile.Name
  albumName := trackResp.Data.TrackUnion.AlbumOfTrack.Name
  releaseDate := trackResp.Data.TrackUnion.AlbumOfTrack.Date.IsoString.Format("2006-01-02")

  // Fetch play count for the track
  playCount, err := client.FetchTrackPlayCount(trackID)
  if err != nil {
  	log.Printf("Error fetching play count for track %s: %v\n", trackID, err)
  	continue
  }

  // Write track ID, track title, artist, album, release date, and play count to CSV
  err = writer.Write([]string{trackID, trackTitle, artistName, albumName, releaseDate, strconv.Itoa(playCount.PlayCount)})
  if err != nil {
  	log.Fatal(err)
  }

}

if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}

// changeExtension replaces the extension of a file path with the new extension
func changeExtension(inputPath string, newExt string) string {
baseName := filepath.Base(inputPath)
ext := filepath.Ext(baseName)
return baseName[0:len(baseName)-len(ext)] + newExt
}

returns
...
C:\Users\Timothy\GO>go run carly.go
2025/03/22 17:29:49 [INFO] --> Successfully created client
2025/03/22 17:29:49 [INFO] --> Fetching track: 6AGklQLROYjvD8qxAPrk3b
panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
github.com/FrostBreker/spotify-private-api.FetchSpotifyClientToken()
C:/Users/Timothy/go/pkg/mod/github.com/!frost!breker/spotify-private-api@v1.0.9/helpers.go:143 +0x632
github.com/FrostBreker/spotify-private-api.GetSpotifyClientToken()
C:/Users/Timothy/go/pkg/mod/github.com/!frost!breker/spotify-private-api@v1.0.9/helpers.go:86 +0x4e
github.com/FrostBreker/spotify-private-api.SetHeadersForSpotifyRequest(0xc00009c280)
C:/Users/Timothy/go/pkg/mod/github.com/!frost!breker/spotify-private-api@v1.0.9/helpers.go:168 +0x148
github.com/FrostBreker/spotify-private-api.(*Client).FetchTrack(, {, _})
C:/Users/Timothy/go/pkg/mod/github.com/!frost!breker/spotify-private-api@v1.0.9/functions.go:28 +0xf6
main.main()
C:/Users/Timothy/GO/carly.go:56 +0x466
exit status 2

C:\Users\Timothy\GO>
...

Originally created by @tim1f1 on GitHub (Mar 22, 2025). Original GitHub issue: https://github.com/FrostBreker/spotify-private-api/issues/2 Originally assigned to: @FrostBreker on GitHub. For a week or two now I've been getting error messages whenever I try running codes using this script e.g. ``` package main import ( "fmt" "log" spotifyprivateapi "github.com/FrostBreker/spotify-private-api" ) func main() { client := spotifyprivateapi.NewClient(spotifyprivateapi.Client{ Debug: true, }) // Replace with a valid Spotify track ID track, err := client.FetchTrack("7sFMOSnRBa6td4lZa1oiTI") if err != nil { log.Fatal(err) } // Print the album name albumName := track.Album.Name // Print the primary artist name artistName := "Unknown Artist" if len(track.Artists) > 0 { artistName = track.Artists[0].Name } fmt.Println("Artist:", artistName) fmt.Println("Album:", albumName) } ``` returns C:\Users\Timothy\GO>go run playcount.go # command-line-arguments .\playcount.go:22:24: track.Album undefined (type responseTypes.TrackResponse has no field or method Album) .\playcount.go:26:18: track.Artists undefined (type responseTypes.TrackResponse has no field or method Artists) .\playcount.go:27:28: track.Artists undefined (type responseTypes.TrackResponse has no field or method Artists) and > package main > > import ( > "bufio" > "encoding/csv" > "log" > "os" > "path/filepath" > "strconv" > > "github.com/FrostBreker/spotify-private-api" > ) > > func main() { > client := spotifyprivateapi.NewClient(spotifyprivateapi.Client{ > Debug: true, > }) > > inputFilePath := "carly.txt" #contains list of track IDs to fetch playcount for > > // Open the text file containing track IDs > file, err := os.Open(inputFilePath) > if err != nil { > log.Fatal(err) > } > defer file.Close() > > // Create the output file name by replacing the .txt extension with .csv > outputFilePath := changeExtension(inputFilePath, ".csv") > > // Create a CSV file to write the data > outputFile, err := os.Create(outputFilePath) > if err != nil { > log.Fatal(err) > } > defer outputFile.Close() > > // Create a CSV writer > writer := csv.NewWriter(outputFile) > defer writer.Flush() > > // Write CSV header > err = writer.Write([]string{"Track ID", "Track Title", "Artist", "Album", "Release Date", "Play Count"}) > if err != nil { > log.Fatal(err) > } > > // Scanner to read track IDs line by line > scanner := bufio.NewScanner(file) > > // Fetch play count for each track > for scanner.Scan() { > trackID := scanner.Text() > > // Fetch track information > trackResp, err := client.FetchTrack(trackID) > if err != nil { > log.Printf("Error fetching track information for track %s: %v\n", trackID, err) > continue > } > > // Extract track title, artist name, album name, and release date from the response > trackTitle := trackResp.Data.TrackUnion.Name > artistName := trackResp.Data.TrackUnion.FirstArtist.Items[0].Profile.Name > albumName := trackResp.Data.TrackUnion.AlbumOfTrack.Name > releaseDate := trackResp.Data.TrackUnion.AlbumOfTrack.Date.IsoString.Format("2006-01-02") > > // Fetch play count for the track > playCount, err := client.FetchTrackPlayCount(trackID) > if err != nil { > log.Printf("Error fetching play count for track %s: %v\n", trackID, err) > continue > } > > // Write track ID, track title, artist, album, release date, and play count to CSV > err = writer.Write([]string{trackID, trackTitle, artistName, albumName, releaseDate, strconv.Itoa(playCount.PlayCount)}) > if err != nil { > log.Fatal(err) > } > } > > if err := scanner.Err(); err != nil { > log.Fatal(err) > } > } > > // changeExtension replaces the extension of a file path with the new extension > func changeExtension(inputPath string, newExt string) string { > baseName := filepath.Base(inputPath) > ext := filepath.Ext(baseName) > return baseName[0:len(baseName)-len(ext)] + newExt > } returns ... C:\Users\Timothy\GO>go run carly.go 2025/03/22 17:29:49 [INFO] --> Successfully created client 2025/03/22 17:29:49 [INFO] --> Fetching track: 6AGklQLROYjvD8qxAPrk3b panic: runtime error: index out of range [1] with length 1 goroutine 1 [running]: github.com/FrostBreker/spotify-private-api.FetchSpotifyClientToken() C:/Users/Timothy/go/pkg/mod/github.com/!frost!breker/spotify-private-api@v1.0.9/helpers.go:143 +0x632 github.com/FrostBreker/spotify-private-api.GetSpotifyClientToken() C:/Users/Timothy/go/pkg/mod/github.com/!frost!breker/spotify-private-api@v1.0.9/helpers.go:86 +0x4e github.com/FrostBreker/spotify-private-api.SetHeadersForSpotifyRequest(0xc00009c280) C:/Users/Timothy/go/pkg/mod/github.com/!frost!breker/spotify-private-api@v1.0.9/helpers.go:168 +0x148 github.com/FrostBreker/spotify-private-api.(*Client).FetchTrack(_, {_, _}) C:/Users/Timothy/go/pkg/mod/github.com/!frost!breker/spotify-private-api@v1.0.9/functions.go:28 +0xf6 main.main() C:/Users/Timothy/GO/carly.go:56 +0x466 exit status 2 C:\Users\Timothy\GO> ...
kerem 2026-02-27 19:22:56 +03:00
  • closed this issue
  • added the
    bug
    label
Sign in to join this conversation.
No labels
bug
bug
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-private-api#2
No description provided.