forked from skuethe/MMM-Spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
319 lines (307 loc) · 10.9 KB
/
node_helper.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
//
// Module : MMM-Spotify
//
"use strict"
const fs = require("fs")
const path = require("path")
const Spotify = require("./Spotify.js")
var NodeHelper = require("node_helper")
module.exports = NodeHelper.create({
start: function () {
this.config = null
this.spotifyConfig = null
this.spotify = null
this.timer = null
this.firstStart = true
this.unallowedDevice = false
this.suspended = false
},
doSpotifyConfig: function (configuration, account) {
if (!isNaN(account) && Array.isArray(configuration)) {
this.sendSocketNotification("CURRENT_ACCOUNT", account)
return configuration[account] // only wanted account or first
}
if (Array.isArray(configuration)) {
let found
configuration.forEach((jsAccount,number) => {
if (jsAccount.USERNAME == account) found = number
})
if (typeof found === "undefined") found = 0
this.sendSocketNotification("CURRENT_ACCOUNT", found)
return configuration[found]
}
// not update required not an array (single account)
return configuration
},
initAfterLoading: function (config, account) {
this.suspended = false
this.config = config
if (!account) {
account = ((typeof this.config.accountDefault !== "undefined") ? this.config.accountDefault : this.config.defaultAccount) // check against both config settings for backwards compatibility since changes in version 2.0.2
console.log("[SPOTIFY] MMM-Spotify Version:", require('./package.json').version)
}
let file = path.resolve(__dirname, "spotify.config.json")
if (fs.existsSync(file)) {
try {
this.spotifyConfig = this.doSpotifyConfig(JSON.parse(fs.readFileSync(file)), account)
} catch (e) {
return console.log("[SPOTIFY] ERROR: spotify.config.json", e.name)
}
if (!this.spotifyConfig) return console.log("[SPOTIFY] ERROR: Account not found")
this.spotify = new Spotify(this.spotifyConfig, this.config.debug)
}
else return console.log("[SPOTIFY] ERROR: spotify.config.json file missing !")
this.updatePulse().then(() => {
if (this.config.debug) console.log("[SPOTIFY] Started with Account:", (this.spotify.config.USERNAME ? this.spotify.config.USERNAME : "default"))
if (this.firstStart && this.config.onStart) this.onStart()
this.firstStart = false
})
},
updatePulse: async function () {
let idle = false
if (!this.spotify) return console.log("[SPOTIFY] updatePulse ERROR: Account not found")
try {
let result = await this.updateSpotify(this.spotify)
this.sendSocketNotification("CURRENT_PLAYBACK", result)
if (this.unallowedDevice) idle = true
} catch (e) {
idle = true
this.sendSocketNotification("CURRENT_NOPLAYBACK")
}
// Only re-run if moudle is NOT suspended
// This breaks multi module instances, but saves performance and power consumption, so we reduce heat
if (!this.suspended) {
this.timer = setTimeout(() => {
this.updatePulse()
}, idle ? this.config.idleInterval : this.config.updateInterval)
}
},
updateSpotify: function (spotify) {
return new Promise((resolve, reject) => {
spotify.getCurrentPlayback((code, error, result) => {
if (result === "undefined" || code !== 200) {
reject();
} else {
resolve(result);
}
})
})
},
onStart: function () {
let onStart = this.config.onStart
if (onStart.deviceName) this.spotify.transferByName(onStart.deviceName)
setTimeout(() => {
onStart.position_ms = 0
if (onStart.search) {
var param = {
q: onStart.search.keyword,
type: onStart.search.type,
}
var condition = {
random: onStart.search.random,
autoplay: true,
}
this.searchAndPlay(param, condition)
} else if (onStart.spotifyUri.match("track")) {
this.spotify.play({uris: [onStart.spotifyUri]})
} else if (onStart.spotifyUri) {
this.spotify.play({context_uri: onStart.spotifyUri})
}
}, 5000)
},
account: function(account) {
this.sendSocketNotification("CURRENT_NOPLAYBACK")
clearTimeout(this.timer)
this.timer= null
this.spotifyConfig = null
this.spotify = null
this.initAfterLoading(this.config, account)
},
getAccounts: function() {
let file = path.resolve(__dirname, "spotify.config.json")
if (fs.existsSync(file)) {
try {
let result = []
let configuration = JSON.parse(fs.readFileSync(file))
if (Array.isArray(configuration)) {
configuration.forEach((jsAccount,number) => {
let accountEntry = { "name": jsAccount.USERNAME, "id": number }
result.push(accountEntry)
})
if (typeof result !== "undefined" && result.length > 0) this.sendSocketNotification("LIST_ACCOUNTS", result)
}
} catch (e) {
return console.log("[SPOTIFY] ERROR fetching accounts from spotify.config.json", e.name)
}
}
},
socketNotificationReceived: function (noti, payload) {
var self = this
if (noti == "INIT") {
this.initAfterLoading(payload)
this.getAccounts()
this.sendSocketNotification("INITIALIZED")
return
}
if (noti == "ACCOUNT") {
this.account(payload)
}
if (noti == "GET_ACCOUNTS") {
this.getAccounts()
}
if (noti == "UNALLOWED_DEVICE") {
this.unallowedDevice = payload
}
if (noti == "SUSPENDING") {
this.suspended = true
}
if(this.spotify && !this.unallowedDevice){
if (noti == "GET_DEVICES") {
this.spotify.getDevices((code, error, result) => {
this.sendSocketNotification("LIST_DEVICES", result)
})
}
if (noti == "PLAY") {
this.spotify.play(payload, (code, error, result) => {
if ((code !== 204) && (code !== 202)) {
if ((typeof result !== "undefined") && result.error && result.error.reason == "NO_ACTIVE_DEVICE") {
// Spotify is in "disconnected" mode. We need to pass a device ID to make it start playing a song.
if (self.config.defaultDevice) {
// User has defined a default device name, let's try to activate it and retry
self.spotify.transferByName(self.config.defaultDevice, (code, error, result) => {
if (code === 204) {
self.spotify.play(payload, (code, error, result) => {
if ((code !== 204) && (code !== 202)) {
console.log("[SPOTIFY] There was a problem during playback")
console.log("[SPOTIFY] API response:", result)
return
}
self.sendSocketNotification("DONE_PLAY", result)
})
} else {
console.log("[SPOTIFY] There was a problem to activate your configured default device:", self.config.defaultDevice)
console.log("[SPOTIFY] API response:", result)
}
})
} else {
console.log("[SPOTIFY] You do not have an active device. Please start playback from another device or define the \"defaultDevice\" config option.")
}
} else {
console.log("[SPOTIFY] There was a problem during playback")
console.log("[SPOTIFY] API response:", result)
}
return
}
this.sendSocketNotification("DONE_PLAY", result)
})
}
if (noti == "PAUSE") {
this.spotify.pause((code, error, result) => {
this.sendSocketNotification("DONE_PAUSE", result)
})
}
if (noti == "NEXT") {
this.spotify.next((code, error, result) => {
this.sendSocketNotification("DONE_NEXT", result)
})
}
if (noti == "PREVIOUS") {
this.spotify.previous((code, error, result) => {
this.sendSocketNotification("DONE_PREVIOUS", result)
})
}
if (noti == "VOLUME") {
this.spotify.volume(payload, (code, error, result) => {
this.sendSocketNotification("DONE_VOLUME", result)
})
}
if (noti == "TRANSFER") {
this.spotify.transferByName(payload, (code, error, result) => {
this.sendSocketNotification("DONE_TRANSFER", result)
})
}
if (noti == "TRANSFERBYID") {
this.spotify.transfer(payload, (code, error, result) => {
this.sendSocketNotification("DONE_TRANSFERBYID", result)
})
}
if (noti == "REPEAT") {
this.spotify.repeat(payload, (code, error, result) => {
this.sendSocketNotification("DONE_REPEAT", result)
})
}
if (noti == "SHUFFLE") {
this.spotify.shuffle(payload, (code, error, result) => {
this.sendSocketNotification("DONE_SHUFFLE", result)
})
}
if (noti == "REPLAY") {
this.spotify.replay((code, error, result) => {
this.sendSocketNotification("DONE_REPLAY", result)
})
}
}
if (noti == "SEARCH_AND_PLAY") {
this.searchAndPlay(payload.query, payload.condition)
return
}
},
searchAndPlay: function (param, condition) {
if (!param.type) {
param.type = "artist,track,album,playlist"
} else {
param.type = param.type.replace(/\s/g, '')
}
if (!param.q) {
param.q = "something cool"
}
var pickup = (items, random, retType) => {
var ret = {}
var r = (random)
? items[Math.floor(Math.random() * items.length)]
: items[0]
if (r.uri) {
ret[retType] = (retType == "uris") ? [r.uri] : r.uri
return ret
} else {
console.log("[SPOTIFY] Unplayable item: ", r)
return false
}
}
this.spotify.search(param, (code, error, result) => {
var foundForPlay = null
if (code == 200) {
const map = {
"tracks": "uris",
"artists": "context_uri",
"albums": "context_uri",
"playlists": "context_uri"
}
//console.log(result)
for (var section in map) {
if (map.hasOwnProperty(section) && !foundForPlay) {
var retType = map[section]
if (result[section] && result[section].items.length > 0) {
foundForPlay = pickup(result[section].items, condition.random, retType)
}
}
}
//console.log(foundForPlay)
if (foundForPlay && condition.autoplay) {
this.spotify.play(foundForPlay, (code, error, result) => {
if (code !== 204) {
return
}
this.sendSocketNotification("DONE_SEARCH_AUTOPLAY", result)
})
} else {
// nothing found or not play.
this.sendSocketNotification("DONE_SEARCH_NOTHING")
}
} else {
//console.log(code, error, result)
this.sendSocketNotification("DONE_SEARCH_ERROR")
}
})
}
})