forked from bitfocus/companion-module-spotify-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
288 lines (258 loc) · 7.58 KB
/
helpers.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { getMyDevices, setVolume } from './api/device.js'
import {
addItemToQueue,
getMyCurrentPlaybackState,
pause,
play,
seek,
setRepeat,
setShuffle,
skipToNext,
skipToPrevious,
transferMyPlayback,
} from './api/playback.js'
import { SpotifyInstanceBase } from './types.js'
// Limit the number of retries that we do
const MAX_ATTEMPTS = 5
export async function ChangeVolume(
instance: SpotifyInstanceBase,
deviceId: string,
absolute: boolean,
volumeOrDelta: number,
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
if (isNaN(volumeOrDelta)) {
instance.log('debug', `Invalid volume change: ${volumeOrDelta} isAbsolute=${absolute}`)
return
}
try {
const data = await getMyDevices(reqOptions)
const selectedDevice = data.body?.devices?.find((dev) => dev.id === deviceId)
// TODO - if dev.type == 'Tablet' || dev.type == 'Phone', then we can't do increments? or should that have been set the volume at all?
// Device doesn't look valid
if (!selectedDevice || typeof selectedDevice.volume_percent !== 'number') return
let newVolume = absolute ? volumeOrDelta : selectedDevice.volume_percent + volumeOrDelta
if (newVolume < 0) newVolume = 0
if (newVolume > 100) newVolume = 100
await setVolume(reqOptions, newVolume, { deviceId })
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return ChangeVolume(instance, deviceId, absolute, volumeOrDelta, attempt + 1)
} else {
throw err
}
}
}
export async function QueueItem(
instance: SpotifyInstanceBase,
deviceId: string,
context_uri: string,
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
await addItemToQueue(reqOptions, context_uri, { deviceId })
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return QueueItem(instance, deviceId, context_uri, attempt + 1)
} else {
throw err
}
}
}
export async function SkipSong(instance: SpotifyInstanceBase, deviceId: string, attempt = 0): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
await skipToNext(reqOptions, { deviceId })
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return SkipSong(instance, deviceId, attempt + 1)
} else {
throw err
}
}
}
export async function PreviousSong(instance: SpotifyInstanceBase, deviceId: string, attempt = 0): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
await skipToPrevious(reqOptions, { deviceId })
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return PreviousSong(instance, deviceId, attempt + 1)
} else {
throw err
}
}
}
export async function TransferPlayback(instance: SpotifyInstanceBase, deviceId: string, attempt = 0): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
await transferMyPlayback(reqOptions, [deviceId], {
play: true,
})
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return TransferPlayback(instance, deviceId, attempt + 1)
} else {
throw err
}
}
}
export async function ChangePlayState(
instance: SpotifyInstanceBase,
deviceId: string,
action: 'play' | 'pause' | 'toggle',
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
const data = await getMyCurrentPlaybackState(reqOptions)
if ((action === 'pause' || action === 'toggle') && data.body?.is_playing) {
await pause(reqOptions, { deviceId })
} else if ((action === 'play' || action === 'toggle') && !data.body?.is_playing) {
await play(reqOptions, { deviceId })
}
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return ChangePlayState(instance, deviceId, action, attempt + 1)
} else {
throw err
}
}
}
export async function ChangeRepeatState(
instance: SpotifyInstanceBase,
deviceId: string,
target: 'off' | 'track' | 'context',
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
// Check if already in desired state
const data = await getMyCurrentPlaybackState(reqOptions)
if (data.body?.repeat_state === target) return
await setRepeat(reqOptions, target)
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return ChangeRepeatState(instance, deviceId, target, attempt + 1)
} else {
throw err
}
}
}
export async function ChangeShuffleState(
instance: SpotifyInstanceBase,
deviceId: string,
target: boolean | 'toggle',
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
const data = await getMyCurrentPlaybackState(reqOptions)
if ((target === false || target === 'toggle') && data.body?.shuffle_state) {
await setShuffle(reqOptions, false, { deviceId })
} else if ((target === true || target === 'toggle') && !data.body?.shuffle_state) {
await setShuffle(reqOptions, true, { deviceId })
}
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return ChangeShuffleState(instance, deviceId, target, attempt + 1)
} else {
throw err
}
}
}
export async function SeekPosition(
instance: SpotifyInstanceBase,
deviceId: string,
positionMs: number,
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
await seek(reqOptions, positionMs)
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return SeekPosition(instance, deviceId, positionMs, attempt + 1)
} else {
throw err
}
}
}
export async function PlaySpecificList(
instance: SpotifyInstanceBase,
deviceId: string,
context_uri: string,
behavior: 'return' | 'resume' | 'force',
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
if (behavior !== 'force') {
const data = await getMyCurrentPlaybackState(reqOptions)
if (data.body?.context?.uri === context_uri) {
if (behavior == 'return' || (behavior == 'resume' && data.body.is_playing)) {
instance.log('warn', `Already playing that ${context_uri}`)
} else if (behavior == 'resume') {
await play(reqOptions, { deviceId })
}
return
}
}
await play(reqOptions, {
deviceId,
context_uri,
})
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return PlaySpecificList(instance, deviceId, context_uri, behavior, attempt + 1)
} else {
throw err
}
}
}
export async function PlaySpecificTracks(
instance: SpotifyInstanceBase,
deviceId: string,
uris: string[],
positionMs: number,
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
try {
await play(reqOptions, {
deviceId: deviceId,
uris: uris,
position_ms: positionMs,
})
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return PlaySpecificTracks(instance, deviceId, uris, positionMs, attempt + 1)
} else {
throw err
}
}
}