-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
96 lines (79 loc) · 2.47 KB
/
queue.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
package cidergo
import (
"encoding/json"
"slices"
)
// AddItemToQueue adds and item to the queue, using its ID (for example, Song.PlayParams.ID). ItemType has to be specified!
// last indicates whether the item is added to the end (false) or start (true) of the queue
func AddItemToQueue(itemType ItemType, id string, last bool) error {
data := map[string]string{
"type": string(itemType),
"id": id,
}
jsonData, _ := json.Marshal(data)
if last {
return postRequest("play-later", jsonData)
} else {
return postRequest("play-next", jsonData)
}
}
// MoveQueueItem moves an item in the queue from one index to another
func MoveQueueItem(startIndex int, destinationIndex int) error {
data := map[string]int{
"startIndex": startIndex,
"destinationIndex": destinationIndex,
}
jsonData, _ := json.Marshal(data)
return postRequest("/queue/move-to-position", jsonData)
}
// DeleteQueueItem deletes a single item from the queue using its index
func DeleteQueueItem(index int) error {
data := map[string]int{"index": index}
jsonData, _ := json.Marshal(data)
return postRequest("/queue/remove-by-index", jsonData)
}
// ClearQueue deletes all items from the queue
func ClearQueue() error {
return postRequestNoJson("queue/clear-queue")
}
// GetFullQueue returns the entire queue with all the relevant metadata.
// THE CURRENTLY PLAYING QueueItem IS NOT NECESSARILY AT INDEX 0 (queue can contain part of the history)
func GetFullQueue() ([]QueueItem, error) {
jsonData, err := jsonRequest("queue")
if err != nil {
return []QueueItem{}, err
}
var data []QueueItem
err = json.Unmarshal(jsonData, &data)
if err != nil {
return []QueueItem{}, err
}
return data, nil
}
// GetSongQueue returns all of the songs in the queue, without the other metadata that GetFullQueue returns.
// THE CURRENTLY PLAYING Song IS NOT NECESSARILY AT INDEX 0 (queue can contain part of the history)
func GetSongQueue() ([]Song, error) {
queueItems, err := GetFullQueue()
if err != nil {
return []Song{}, err
}
var songs []Song
for _, queueItem := range queueItems {
songs = append(songs, queueItem.Song)
}
return songs, nil
}
// GetCurrentQueueIndex returns the index of the currently playing item in the queue
func GetCurrentQueueIndex() (int, error) {
currentSong, err := CurrentSong()
if err != nil {
return -1, err
}
songs, err := GetSongQueue()
if err != nil {
return -1, err
}
return slices.IndexFunc(songs, func(song Song) bool {
return song.URL == currentSong.URL
}), nil
}