forked from zmb3/spotify
/
page.go
54 lines (47 loc) · 1.15 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"context"
"log"
"os"
"github.com/JunNishimura/spotify/v2"
spotifyauth "github.com/JunNishimura/spotify/v2/auth"
"golang.org/x/oauth2/clientcredentials"
)
func main() {
ctx := context.Background()
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)
client := spotify.New(httpClient)
// Public playlist owned by noah.stride:
// "Long playlist for testing pagination"
playlistID := "1ckDytqUi4BUYzs6HIhcAN"
if id := os.Getenv("SPOTIFY_PLAYLIST"); id != "" {
playlistID = id
}
tracks, err := client.GetPlaylistItems(
ctx,
spotify.ID(playlistID),
)
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.Items))
err = client.NextPage(ctx, tracks)
if err == spotify.ErrNoMorePages {
break
}
if err != nil {
log.Fatal(err)
}
}
}