-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_api.go
63 lines (48 loc) · 1.43 KB
/
spotify_api.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
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"log"
"net/http"
"github.com/zmb3/spotify"
)
var (
auth = spotify.NewAuthenticator("http://localhost:8080/callback", spotify.ScopeUserReadPlaybackState, spotify.ScopeUserModifyPlaybackState, spotify.ScopePlaylistReadPrivate)
state = "abc123"
ch = make(chan *spotify.Client)
)
func spotifyAuthentication(w http.ResponseWriter, r *http.Request) {
token, err := auth.Token(state, r)
if err != nil {
http.Error(w, "could not get spotify oauth token", http.StatusForbidden)
log.Fatal(err)
}
spotifyClient := auth.NewClient(token)
fmt.Fprintf(w, "Login Completed")
ch <- &spotifyClient
}
func getUserPlayerState(client *spotify.Client) (*spotify.PlayerState, error) {
playerState, err := client.PlayerState()
if err != nil {
return nil, err
}
return playerState, nil
}
func loadTracksForPlaylist(client *spotify.Client, playlistID spotify.ID) ([]spotify.FullTrack, error) {
playlistTracks, err := client.GetPlaylistTracks(playlistID)
if err != nil {
return nil, err
}
var tracks []spotify.FullTrack
for _, item := range playlistTracks.Tracks {
track := item.Track
tracks = append(tracks, track)
}
return tracks, nil
}
func initializePlaylists(client *spotify.Client) ([]spotify.SimplePlaylist, error) {
playlists, err := client.CurrentUsersPlaylists()
if err != nil {
return nil, err
}
return playlists.Playlists, nil
}