-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.go
115 lines (94 loc) · 3.62 KB
/
player.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package spotify
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/url"
"strconv"
)
type Playback struct {
IsPlaying bool `json:"is_playing"`
Item Item `json:"item"`
ProgressMs int `json:"progress_ms"`
RepeatState string `json:"repeat_state"`
ShuffleState bool `json:"shuffle_state"`
}
type Item struct {
Track
Show Show `json:"show"`
}
// GetPlayback gets information about the user's current playback state, including track or episode, progress, and active device.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-information-about-the-users-current-playback
func (a *API) GetPlayback() (*Playback, error) {
query := make(url.Values)
query.Add("additional_types", "episode")
playback := new(Playback)
err := a.get("v1", "/me/player", query, playback)
if err == io.EOF {
return nil, errors.New("Player command failed: No active device found")
}
return playback, err
}
// GetDevices gets information about a user's available devices.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-users-available-devices
func (a *API) GetDevices() ([]*Device, error) {
res := &struct {
Devices []*Device `json:"devices"`
}{}
err := a.get("v1", "/me/player/devices", nil, res)
return res.Devices, err
}
// Play starts a new context or resume current playback on the user's active device.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-start-a-users-playback
func (a *API) Play(contextURI string, uris ...string) error {
query := make(url.Values)
body := &struct {
ContextURIs string `json:"context_uri,omitempty"`
URIs []string `json:"uris,omitempty"`
}{
ContextURIs: contextURI,
URIs: uris,
}
data, err := json.Marshal(body)
if err != nil {
return err
}
return a.put("v1", "/me/player/play", query, bytes.NewReader(data))
}
// Pause pauses playback on the user's account.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-pause-a-users-playback
func (a *API) Pause() error {
return a.put("v1", "/me/player/pause", nil, nil)
}
// SkipToPreviousTrack skips to the previous track in the user's queue.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-skip-users-playback-to-previous-track
func (a *API) SkipToPreviousTrack() error {
return a.post("v1", "/me/player/previous", nil, nil, nil)
}
// SkipToNextTrack skips to the next track in the user's queue.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-skip-users-playback-to-next-track
func (a *API) SkipToNextTrack() error {
return a.post("v1", "/me/player/next", nil, nil, nil)
}
// Repeat sets the repeat mode for the user's playback. Options are repeat-track, repeat-context, and off.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-set-repeat-mode-on-users-playback
func (a *API) Repeat(state string) error {
query := make(url.Values)
query.Add("state", state)
return a.put("v1", "/me/player/repeat", query, nil)
}
// Shuffle toggles shuffle on or off for user's playback.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-toggle-shuffle-for-users-playback
func (a *API) Shuffle(state bool) error {
query := make(url.Values)
query.Add("state", strconv.FormatBool(state))
return a.put("v1", "/me/player/shuffle", query, nil)
}
// Queue adds an item to the end of the user's current playback queue.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-add-to-queue
func (a *API) Queue(uri string) error {
query := make(url.Values)
query.Add("uri", uri)
return a.post("v1", "/me/player/queue", query, nil, nil)
}