forked from natkinson2020/MMM-Spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Spotify.js
449 lines (419 loc) · 14 KB
/
MMM-Spotify.js
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//
// Module : MMM-Spotify
//
Module.register("MMM-Spotify", {
defaults: {
style: "default", // "default", "mini" available.
control: "default", //"default", "hidden" available
updateInterval: 1000,
allowDevices: [],
iconify: "https://code.iconify.design/1/1.0.0-rc7/iconify.min.js",
//iconify: null,
//When you use this module with `MMM-CalendarExt` or any other `iconify` used modules together, Set this null.
onStart: null,
//If you want to play something on start; set like this.
/*
onStart: {
deviceName: "Web Player (Chrome)", //if null, current(last) activated device will be.
spotifyUri : "spotify:playlist:37i9dQZF1DX9EM98aZosoy", //when search is set, sportifyUri will be ignored.
search: {
type: "artist, track", // `artist`, track`, `album`, `playlist` available
keyword: "michael+jackson",
random:true,
}
}
*/
},
getScripts: function() {
r = []
if (this.config.iconify) {
r.push(this.config.iconify)
}
return r
},
getStyles: function() {
return ["MMM-Spotify.css"]
},
start: function() {
this.currentPlayback = null
},
notificationReceived: function(noti, payload, sender) {
if (noti === "DOM_OBJECTS_CREATED") {
this.sendSocketNotification("INIT", this.config)
//console.log(this.config)
//this.loadExternalScript(this.config.iconify)
this.onStart()
}
switch(noti) {
case "SPOTIFY_SEARCH":
var pl = {
query: {
q: payload.query,
type: payload.type,
},
condition: {
random:payload.random,
autoplay:true,
}
}
this.sendSocketNotification("SEARCH_AND_PLAY", pl)
break
case "SPOTIFY_PLAY":
this.sendSocketNotification("PLAY", payload)
break
case "SPOTIFY_PAUSE":
this.sendSocketNotification("PAUSE")
break
case "SPOTIFY_NEXT":
this.sendSocketNotification("NEXT")
break
case "SPOTIFY_PREVIOUS":
this.sendSocketNotification("PREVIOUS")
break
case "SPOTIFY_VOLUME":
this.sendSocketNotification("VOLUME", payload)
break
case "SPOTIFY_TRANSFER":
this.sendSocketNotification("TRANSFER", payload)
break
case "SPOTIFY_SHUFFLE":
this.clickShuffle()
break
case "SPOTIFY_REPEAT":
this.clickRepeat()
break
case "SPOTIFY_TOGGLE":
this.clickPlay()
break
}
},
socketNotificationReceived: function(noti, payload) {
switch(noti) {
case "INITIALIZED":
break
case "CURRENT_PLAYBACK":
if (
(this.config.allowDevices.length === 0)
|| (this.config.allowDevices.length >= 1 && this.config.allowDevices.includes(payload.device.name))
) {
this.updateCurrentPlayback(payload)
} else {
this.currentPlayback = null
this.updateDom()
}
break
case "CURRENT_PLAYBACK_FAIL":
this.updateNoPlayback()
}
if (noti.search("DONE_") > -1) {
this.sendNotification(noti)
}
},
onStart: function() {
if (!this.config.onStart) return
this.sendSocketNotification("ONSTART", this.config.onStart)
},
updateNoPlayback: function() {
var dom = document.getElementById("SPOTIFY")
dom.classList.add("inactive")
},
updateCurrentPlayback: function(current) {
if (!current) return
if (!this.currentPlayback) {
this.updateSongInfo(current)
this.updatePlaying(current)
this.updateDevice(current)
this.updateShuffle(current)
this.updateRepeat(current)
this.updateProgress(current)
} else {
if (this.currentPlayback.is_playing !== current.is_playing) {
this.updateSongInfo(current)
this.updatePlaying(current)
}
if (this.currentPlayback.item.id !== current.item.id) {
this.updateSongInfo(current)
}
if (this.currentPlayback.device.id !== current.device.id) {
this.updateDevice(current)
}
if (this.currentPlayback.repeat_state !== current.repeat_state) {
this.updateRepeat(current)
}
if (this.currentPlayback.shuffle_state !== current.shuffle_state) {
this.updateShuffle(current)
}
if (this.currentPlayback.progress_ms !== current.progress_ms) {
this.updateProgress(current)
}
}
this.currentPlayback = current
},
updateProgress: function(
current,
end = document.getElementById("SPOTIFY_PROGRESS_END"),
curbar = document.getElementById("SPOTIFY_PROGRESS_CURRENT"),
now = document.getElementById("SPOTIFY_PROGRESS_BAR_NOW")
) {
var msToTime = (duration) => {
var ret = ""
var milliseconds = parseInt((duration%1000)/100)
, seconds = parseInt((duration/1000)%60)
, minutes = parseInt((duration/(1000*60))%60)
, hours = parseInt((duration/(1000*60*60))%24)
if (hours > 0) {
hours = (hours < 10) ? "0" + hours : hours
ret = ret + hours + ":"
}
minutes = (minutes < 10) ? "0" + minutes : minutes
seconds = (seconds < 10) ? "0" + seconds : seconds
return ret + minutes + ":" + seconds
}
var songDur = current.item.duration_ms
var cur = current.progress_ms
var pros = (cur / songDur) * 100
end.innerHTML = msToTime(songDur)
curbar.innerHTML = msToTime(cur)
now.style.width = pros + "%"
},
updateShuffle: function(newPlayback) {
var shuffle = document.getElementById("SPOTIFY_CONTROL_SHUFFLE")
var si = document.createElement("span")
si.className = "iconify"
si.dataset.icon = "mdi:shuffle"
if (newPlayback.shuffle_state) {
shuffle.className = "on"
si.dataset.icon = "mdi:shuffle"
} else {
shuffle.className = "off"
si.dataset.icon = "mdi:shuffle-disabled"
}
shuffle.innerHTML = ""
shuffle.appendChild(si)
},
updateRepeat: function(newPlayback) {
var repeat = document.getElementById("SPOTIFY_CONTROL_REPEAT")
var ri = document.createElement("span")
ri.className = "iconify"
ri.dataset.inline = "false"
repeat.className = newPlayback.repeat_state
const ris = {
"off" : "mdi:repeat-off",
"track" : "mdi:repeat-once",
"context" : "mdi:repeat"
}
ri.dataset.icon = ris[newPlayback.repeat_state]
repeat.innerHTML = ""
repeat.appendChild(ri)
},
updateDevice: function(newPlayback) {
var device = document.querySelector("#SPOTIFY_DEVICE .text")
device.textContent = newPlayback.device.name
this.sendNotification("SPOTIFY_UPDATE_DEVICE", newPlayback.device)
},
updatePlaying: function(newPlayback) {
var s = document.getElementById("SPOTIFY")
var p = document.getElementById("SPOTIFY_CONTROL_PLAY")
var pi = document.createElement("span")
pi.className = "iconify"
pi.dataset.inline = "false"
if(newPlayback.is_playing) {
s.classList.add("playing")
s.classList.remove("pausing")
s.classList.remove("inactive")
pi.dataset.icon = "mdi:play-circle-outline"
p.className = "playing"
} else {
s.classList.add("pausing")
s.classList.remove("playing")
s.classList.remove("inactive")
pi.dataset.icon = "mdi:pause-circle-outline"
p.className = "pausing"
}
p.innerHTML = ""
p.appendChild(pi)
this.sendNotification("SPOTIFY_UPDATE_PLAYING", newPlayback.is_playing)
},
updateSongInfo: function(newPlayback) {
if (!newPlayback) return
if (!newPlayback.item) return
var sDom = document.getElementById("SPOTIFY")
sDom.classList.remove("noPlayback")
var cover_img = document.getElementById("SPOTIFY_COVER_IMAGE")
cover_img.src = newPlayback.item.album.images[0].url
var back = document.getElementById("SPOTIFY_BACKGROUND")
back.style.backgroundImage = `url(${newPlayback.item.album.images[0].url})`
var title = document.querySelector("#SPOTIFY_TITLE .text")
title.textContent = newPlayback.item.name
var artist = document.querySelector("#SPOTIFY_ARTIST .text")
var artists = newPlayback.item.artists
var artistName = ""
for (var x = 0; x < artists.length; x++) {
if (!artistName) {
artistName = artists[x].name
} else {
artistName += ", " + artists[x].name
}
}
artist.textContent = artistName
this.sendNotification("SPOTIFY_UPDATE_SONG_INFO", newPlayback.item)
},
clickPlay: function() {
if (this.currentPlayback.is_playing) {
this.sendSocketNotification("PAUSE")
} else {
this.sendSocketNotification("PLAY")
}
},
clickRepeat: function() {
var c = this.currentPlayback.repeat_state
var n = ""
if (c === "off") n = "track"
if (c === "track") n = "context"
if (c === "context") n = "off"
this.sendSocketNotification("REPEAT", n)
},
clickShuffle: function() {
this.sendSocketNotification("SHUFFLE", !this.currentPlayback.shuffle_state)
},
clickBackward: function() {
if (this.currentPlayback.progress_ms < 3000) {
this.sendSocketNotification("PREVIOUS")
} else {
this.sendSocketNotification("REPLAY")
}
},
clickForward: function() {
this.sendSocketNotification("NEXT")
},
getDom: function(){
var m = document.createElement("div")
m.id = "SPOTIFY"
if (this.config.style !== "default") {
m.classList.add(this.config.style)
}
if (this.config.control !== "default") {
m.classList.add(this.config.control)
}
m.classList.add("noPlayback")
var back = document.createElement("div")
back.id = "SPOTIFY_BACKGROUND"
m.appendChild(back)
var fore = document.createElement("div")
fore.id = "SPOTIFY_FOREGROUND"
var cover = document.createElement("div")
cover.id = "SPOTIFY_COVER"
var cover_img = document.createElement("img")
cover_img.id = "SPOTIFY_COVER_IMAGE"
cover_img.src = "./modules/MMM-Spotify/resources/spotify-xxl.png"
cover.appendChild(cover_img)
fore.appendChild(cover)
var misc = document.createElement("div")
misc.id = "SPOTIFY_MISC"
var info = document.createElement("div")
info.id = "SPOTIFY_INFO"
var title = document.createElement("div")
title.id = "SPOTIFY_TITLE"
var ti = document.createElement("span")
ti.className = "iconify"
ti.dataset.icon = "mdi:music"
ti.dataset.inline = "false"
title.appendChild(ti)
var tt = document.createElement("span")
tt.className = "text"
tt.textContent = ""
title.appendChild(tt)
var artist = document.createElement("div")
artist.id = "SPOTIFY_ARTIST"
var ai = document.createElement("span")
ai.className = "iconify"
ai.dataset.icon = "ic-baseline-person"
ai.dataset.inline = "false"
artist.appendChild(ai)
var at = document.createElement("span")
at.className = "text"
at.textContent = ""
artist.appendChild(at)
var device = document.createElement("div")
device.id = "SPOTIFY_DEVICE"
var di = document.createElement("span")
di.className = "iconify"
di.dataset.icon = "ic-baseline-devices"
di.dataset.inline = "false"
device.appendChild(di)
var dt = document.createElement("span")
dt.className = "text"
dt.textContent = ""
device.appendChild(dt)
var progress = document.createElement("div")
progress.id = "SPOTIFY_PROGRESS"
var currentTime = document.createElement("div")
currentTime.id = "SPOTIFY_PROGRESS_CURRENT"
currentTime.innerHTML = "--:--"
var songTime = document.createElement("div")
songTime.id = "SPOTIFY_PROGRESS_END"
songTime.innerHTML = "--:--"
var time = document.createElement("div")
time.id = "SPOTIFY_PROGRESS_TIME"
time.appendChild(currentTime)
time.appendChild(songTime)
progress.appendChild(time)
var bar = document.createElement("div")
bar.id = "SPOTIFY_PROGRESS_BAR"
var barNow = document.createElement("div")
barNow.id = "SPOTIFY_PROGRESS_BAR_NOW"
bar.appendChild(barNow)
progress.appendChild(bar)
var control = document.createElement("div")
control.id = "SPOTIFY_CONTROL"
var shuffle = document.createElement("div")
shuffle.id = "SPOTIFY_CONTROL_SHUFFLE"
shuffle.addEventListener("click", ()=>{this.clickShuffle()})
shuffle.className = "off"
var si = document.createElement("span")
si.className = "iconify"
si.dataset.icon = "mdi:shuffle"
si.dataset.inline = "false"
shuffle.appendChild(si)
var repeat = document.createElement("div")
repeat.id = "SPOTIFY_CONTROL_REPEAT"
repeat.addEventListener("click", ()=>{this.clickRepeat()})
var ri = document.createElement("span")
ri.className = "iconify"
ri.dataset.inline = "false"
repeat.className = "off"
ri.dataset.icon = "mdi:repeat-off"
repeat.appendChild(ri)
var backward = document.createElement("div")
backward.id = "SPOTIFY_CONTROL_BACKWARD"
backward.addEventListener("click", ()=>{this.clickBackward()})
backward.innerHTML = `<span class="iconify" data-icon="mdi:skip-previous" data-inline="false"></span>`
var forward = document.createElement("div")
forward.id = "SPOTIFY_CONTROL_FORWARD"
forward.innerHTML = `<span class="iconify" data-icon="mdi:skip-next" data-inline="false"></span>`
forward.addEventListener("click", ()=>{this.clickForward()})
var play = document.createElement("div")
play.id = "SPOTIFY_CONTROL_PLAY"
play.addEventListener("click", ()=>{this.clickPlay()})
var pi = document.createElement("span")
pi.className = "iconify"
pi.dataset.inline = "false"
//pi.dataset.icon = (this.currentPlayback.is_playing) ? "mdi:play-circle-outline" : "mdi:pause-circle-outline"
pi.dataset.icon = "mdi:play-circle-outline"
play.appendChild(pi)
info.appendChild(title)
info.appendChild(artist)
info.appendChild(device)
misc.appendChild(info)
misc.appendChild(progress)
control.appendChild(shuffle)
control.appendChild(backward)
control.appendChild(play)
control.appendChild(forward)
control.appendChild(repeat)
misc.appendChild(control)
fore.appendChild(misc)
m.appendChild(fore)
return m
},
})