-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlist.go
60 lines (48 loc) · 1.33 KB
/
playlist.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
package distance
import (
"net/url"
"strconv"
)
// PlaylistState is the state of the server and its playlist.
type PlaylistState struct {
CurrentLevelIndex int
Playlist Playlist
}
// Playlist is the server's playlist.
type Playlist struct {
Total int
Start int
Count int
Levels []Level
}
// AllPlaylist fetches all playlists and automatically paginates.
func (c *Client) AllPlaylist() (*PlaylistState, error) {
var state PlaylistState
for {
// This loop is overkill, but we're doing it just in case.
s, err := c.Playlist(state.Playlist.Start, 10240)
if err != nil {
return nil, err
}
state.CurrentLevelIndex = s.CurrentLevelIndex
state.Playlist.Levels = append(state.Playlist.Levels, s.Playlist.Levels...)
last := state.Playlist.Levels[len(state.Playlist.Levels)-1]
state.Playlist.Start = last.Index + 1
if state.Playlist.Start >= s.Playlist.Total {
break
}
}
state.Playlist.Count = state.Playlist.Start
state.Playlist.Total = state.Playlist.Start
state.Playlist.Start = 0
return &state, nil
}
// Playlist fetches a single playlist page.
func (c *Client) Playlist(start, count int) (*PlaylistState, error) {
v := url.Values{
"Start": {strconv.Itoa(start)},
"Count": {strconv.Itoa(count)},
}
var p *PlaylistState
return p, c.getJSON(url.URL{Path: "/playlist", RawQuery: v.Encode()}, &p)
}