forked from zmb3/spotify
/
page.go
41 lines (35 loc) · 845 Bytes
/
page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"context"
"log"
"os"
"github.com/zmb3/spotify"
"golang.org/x/oauth2/clientcredentials"
)
func main() {
config := &clientcredentials.Config{
ClientID: os.Getenv("SPOTIFY_ID"),
ClientSecret: os.Getenv("SPOTIFY_SECRET"),
TokenURL: spotify.TokenURL,
}
token, err := config.Token(context.Background())
if err != nil {
log.Fatalf("couldn't get token: %v", err)
}
client := spotify.Authenticator{}.NewClient(token)
tracks, err := client.GetPlaylistTracks("37i9dQZF1DWWzVPEmatsUB")
if err != nil {
log.Fatal(err)
}
log.Printf("Playlist has %d total tracks", tracks.Total)
for page := 1; ; page++ {
log.Printf(" Page %d has %d tracks", page, len(tracks.Tracks))
err = client.NextPage(tracks)
if err == spotify.ErrNoMorePages {
break
}
if err != nil {
log.Fatal(err)
}
}
}