~dricottone/nspotify

nspotify/events.go -rw-r--r-- 4.2 KiB
1064d33fDominic Ricottone Noted one more TODO 7 months ago
                                                                                
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main

// Manager for events.

import (
	"context"
	"fmt"
	"strings"

	log "github.com/sirupsen/logrus"
	"github.com/zmb3/spotify/v2"
)

// Types of player events to expect.
type EventType int

const (
	// Player event requesting that playback begin with a specific URI.
	PlayURI EventType = 0

	// Player event requesting that a specific URI be enqueued for future
	// playback.
	QueueURI EventType = 1

	// Player event requesting that playback play (resume).
	Play EventType = 2

	// Player event requesting that playback pause.
	Pause EventType = 3

	// Player event requesting that playback toggle (play/pause).
	Toggle EventType = 4

	// Player event requesting that the previous song play.
	PlayPrevious EventType = 5

	// Player event requesting that the next song play.
	PlayNext EventType = 6
)

// Convert a player event to a printable (debug-able) string.
func debugEvent(ev EventType) string {
	switch ev {
	case PlayURI:
		return "PlayURI"

	case QueueURI:
		return "QueueURI"

	case Play:
		return "Play"

	case Pause:
		return "Pause"

	case Toggle:
		return "Toggle"

	case PlayPrevious:
		return "PlayPrevious"

	case PlayNext:
		return "PlayNext"
	}
	
	return fmt.Sprintf("%d", ev)
}

// A player event.
type Event struct {
	Type EventType
	URI  spotify.URI
}

// Creates an `Event` of type `PlayURI`.
func RequestPlayURI(uri spotify.URI) *Event {
	return &Event{
		Type: PlayURI,
		URI: uri,
	}
}

// Creates an `Event` of type `QueueURI`.
func RequestQueueURI(uri spotify.URI) *Event {
	return &Event{
		Type: QueueURI,
		URI: uri,
	}
}

// Creates an `Event` of type `Play`.
func RequestPlay() *Event {
	return &Event{
		Type: Play,
		URI: spotify.URI(""),
	}
}

// Creates an `Event` of type `Pause`.
func RequestPause() *Event {
	return &Event{
		Type: Pause,
		URI: spotify.URI(""),
	}
}

// Creates an `Event` of type `Toggle`.
func RequestToggle() *Event {
	return &Event{
		Type: Toggle,
		URI: spotify.URI(""),
	}
}

// Creates an `Event` of type `PlayPrevious`.
func RequestPlayPrevious() *Event {
	return &Event{
		Type: PlayPrevious,
		URI: spotify.URI(""),
	}
}

// Creates an `Event` of type `PlayNext`.
func RequestPlayNext() *Event {
	return &Event{
		Type: PlayNext,
		URI: spotify.URI(""),
	}
}

// Reusable handler for an `Event` of type `Play`.
func handlePlayEvent(ctx context.Context, cli *spotify.Client) {
	err := cli.Play(ctx)
	if err != nil {
		log.WithError(err).Error("request to play failed")
	}
}

// Reusable handler for an `Event` of type `Pause`.
func handlePauseEvent(ctx context.Context, cli *spotify.Client) {
	err := cli.Pause(ctx)
	if err != nil {
		log.WithError(err).Error("request to pause failed")
	}
}

// Manager for player events.
func EventsManager(ctx context.Context, cli *spotify.Client, ch <-chan *Event) {
	sdev, ok := ctx.Value("device").(string)
	dev := spotify.ID(sdev)
	if !ok || (dev == "") {
		log.Errorf("invalid device: %s", sdev)
	}

	for ev := range ch {
		switch ev.Type {
		case PlayURI:
			opts := &spotify.PlayOptions{
				DeviceID: &dev,
				URIs: []spotify.URI{ev.URI},
			}
			err := cli.PlayOpt(ctx, opts)
			if err != nil {
				log.WithError(err).Error("request to play URI failed")
			}
	
		case QueueURI:
			suri := string(ev.URI)
			sid := strings.Replace(suri, "spotify:track:", "", 1)
			id := spotify.ID(sid)
	
			err := cli.QueueSong(ctx, id)
			if err != nil {
				log.WithError(err).Error("request to queue URI failed")
			}
	
		case Play:
			handlePlayEvent(ctx, cli)
	
		case Pause:
			handlePauseEvent(ctx, cli)
	
		case Toggle:
			status, err := cli.PlayerCurrentlyPlaying(ctx)
			if err != nil {
				log.WithError(err).Error("request to determine playback status, so assuming that status is playing")
				handlePauseEvent(ctx, cli)
			} else {
				if status.Playing {
					handlePauseEvent(ctx, cli)
				} else {
					handlePlayEvent(ctx, cli)
				}
			}

		case PlayNext:
			err := cli.Next(ctx)
			if err != nil {
				log.WithError(err).Error("request to play next failed")
			}
	
		case PlayPrevious:
			err := cli.Previous(ctx)
			if err != nil {
				log.WithError(err).Error("request to play previous failed")
			}
	
		default:
			log.Errorf("unhandled event: %s", debugEvent(ev.Type))
		}
	}

	log.Trace("no more events, terminating events manager")
}