forked from zmb3/spotify
/
search.go
46 lines (40 loc) · 1005 Bytes
/
search.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
package main
import (
"context"
"fmt"
"log"
"os"
"golang.org/x/oauth2/clientcredentials"
"github.com/zmb3/spotify"
)
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)
// search for playlists and albums containing "holiday"
results, err := client.Search("holiday", spotify.SearchTypePlaylist|spotify.SearchTypeAlbum)
if err != nil {
log.Fatal(err)
}
// handle album results
if results.Albums != nil {
fmt.Println("Albums:")
for _, item := range results.Albums.Albums {
fmt.Println(" ", item.Name)
}
}
// handle playlist results
if results.Playlists != nil {
fmt.Println("Playlists:")
for _, item := range results.Playlists.Playlists {
fmt.Println(" ", item.Name)
}
}
}